Resources

For links around the frameworks and using them, see Test Frameworks guide.

Spec file

In a script foo.spec.js or foo.test.js.

There are difference places to put tests:

  • spec
  • tests
  • __tests__
  • the same directory as script it tests.

Sample:

  • foo.spec.js
      import foo from "../foo.js"
    
      describe('#foo', () => {
        it('returns a correct foo when given a number', () => {
          const result = foo(12)
    
          expect(result).toEqual({ 'a': 12 })
        })
      })
    

Methods

  • toBe - for primitives.
  • toEqual - for data structures.
  • toDeepEqual - nested data structures (?)

Frameworks

See example of tests in my projects, to get an idea of syntax in larger scripts.

Mocha

See src/test/ directory in auto-commit-msg, a TypeScript application.

Jest

The default view is short, so you might want to see a list of all tests that ran:

$ jest --verbose

Get a test coverage report:

$ jest --coverage

Target specific module or directory:

$ jest tests/foo.spec.js
$ jest tests/buzz

Watch

See also tests/unit/ in my badge-generator repo, a Vue application.

In that project at least, the default is to run interactive test mode - you get prompted which tests you want to run.

If use--watchAll flag, then the tests will rerun on changes and stay in interactive mode.

For builds and CI, use the --watchAll=false flag. The react-scripts package uses Jest internally so this used as an example.

$ react-scripts test --env=jsdom --watchAll=false