Jest

To run only one single test use test.only('test', () => {

Running test

# run all tests if you have {"scripts":{"test":"jest"}} in package.json
yarn test
# run single file
node node_modules/.bin/jest --runInBand test/initialise.test.js

# debug in chrome chrome://inspect
node --inspect-brk node_modules/.bin/jest --runInBand test/initialise.test.js

# watch for file changes, like guard file listener
node --inspect node_modules/.bin/jest --watch

Mocking functions

const mockCallback = jest.fn(x => 42 + x)
some_func_to_call(mockCallback)
expect(mockCallback.mock.calls.length).toBe(2) // called twice
expect(mockCallback.mock.calls[0][0]).toBe(0) // first call first argument be
expect(mockCallback.mock.calls[0].value).toBe(0) // first call result

Instances

const myMock = jest.fn()
const a = new myMock()
const b = {}
const bound = myMock.bind(b)
bound()

console.log(myMock.mock.instances) // <a> <b>

To mock stylesheet files https://jestjs.io/docs/en/webpack

// package.json
{
  "jest": {
    "moduleNameMapper": {
      "\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"
    }
  },
}

JSDOC

https://jsdoc.app/index.html#block-tags

  • start with two stars /**