-
-
Save elrikdante/c9082aaa10c9b1cd7bc4 to your computer and use it in GitHub Desktop.
Example of ignoring hspec's beforeAll hook context
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Concurrent (threadDelay) | |
import Test.Hspec | |
import qualified Test.Hspec.Core.Hooks as Hooks | |
main :: IO () | |
main = hspec spec | |
-- | When a hook is set up with `beforeAll`, that changes the type | |
-- of all hspec `it`s to take the value supplied by `beforeAll`. | |
-- | |
-- In some cases, we just want to use `beforeAll`/`afterAll` to have | |
-- a process (e.g. a web server) running during the tests, and the | |
-- individual `it`s may not care about the value supplied by `beforeAll`. | |
-- | |
-- In such cases, this function can be used to allow to use plain `it`s | |
-- within a `describe` block that would usually mandate all `it`s to | |
-- accept the `a` that `beforeAll` provided. | |
ignoreHookContext :: SpecWith () -> SpecWith a | |
ignoreHookContext = Hooks.aroundWith (\actionRunner -> const (actionRunner ())) | |
spec :: Spec -- == SpecWith () | |
spec = | |
beforeAll (putStrLn "beforeAll hook" >> threadDelay 2000000 >> return (3 :: Int)) | |
. afterAll (\i -> putStrLn ("afterAll hook: " ++ show (i::Int))) | |
$ do | |
describe "section using the hook return value" $ do | |
it "does X" $ \i -> do | |
putStrLn $ "done, with access to value " ++ show i | |
ignoreHookContext $ do | |
describe "section NOT using the hook return value" $ do | |
it "does Y" $ | |
putStrLn "done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment