Adding tests

haskell-template does not include tests by default (see Philosophy), but you may add them as follows:

  • Split any logic code out of Main.hs into, say, a Lib.hs.
  • Correspondingly, add other-modules: Lib to the β€œshared” section of your cabal file.
  • Add tests/Spec.hs (example below):
    module Main where
    
    import Lib qualified
    import Test.Hspec (describe, hspec, it, shouldContain)
    
    main :: IO ()
    main = hspec $ do
      describe "Lib.hello" $ do
        it "contains the world emoji" $ do
          toString Lib.hello `shouldContain` "🌎"
  • Add the tests stanza to the cabal file:
    test-suite tests
        import:         shared
        main-is:        Spec.hs
        type:           exitcode-stdio-1.0
        hs-source-dirs: tests
        build-depends:  hspec
  • Update hie.yaml accordingly; for example, by adding,
      - path: "tests"
        component: "test:tests"
  • Add the test command to the mission-control scripts section of the flake file
      mission-control.scripts = {
        test = {
          description = "Run all tests";
          exec = ''
             ghcid -c "cabal repl test:tests" -T :main
          '';
          };
      };
  • Commit your changes to Git, and test it out by running , test from the (reloaded) dev shell.
Links to this page
  • haskell-template
  • Philosophy

    Things like tests are not in the template repo, because I personally do not use it in every project created off this repo. Instead, a workflow like β€œHow to add tests” should be documented (eg.: Adding tests). The same goes for project documentation (which normally would use Emanote).