jest

debug jest on windows

node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand

then click Inspect on the target in chrome://inspect

cannot find module @babel/runtime

enzyme dump html

Dump the current wrapper or results from find to console with .html()

https://airbnb.io/enzyme/docs/api/ShallowWrapper/html.html

mocking react-router

Use hooks and mock them instead;

jest.mock('react-router-dom', () => ({
  ...jest.requireActual('react-router-dom'),
  useParams: () => ({
    myRoute: 'some-route-value',
  }),
}));

mocking a dependency function imported by a module under test

If we have a module:

someModule.js

export const someFunction = () => {
  console.log("does something");
};

which is a dependency of another module under test:

moduleUnderTest.js

import { someFunction } from "./someModule";

export const underTest = () => {
  // ...
  someFunction();
  // ...
};

then, in our test, we can mock and inspect the dependency:

moduleUnderTest.test.js

import { underTest } from "./moduleUnderTest";
import { someFunction } from "./someModule"; // this bit was weird

jest.mock("./someModule");

describe("underTest", () => {
  it("called someFunction", () => {
    underTest();
    expect(someFunction).toHaveBeenCalled();
  });
});

mocking functions with spyOn

myModule.js

export const myFunction = () => "hello";

myModule.test.js

import * as myModule from "./myModule";

jest.spyOn(myModule, "myFunction").mockImplementation(() => "goodbye");

find skipped tests

npm i -D jest-skipped-reporter
npm run test -- --reporters jest-skipped-reporter

force luxon to a specific time in utc

Mock luxon’s time to be 2021-02-25 UTC:

Settings.now = () => new Date(Date.UTC(2021, 1, 25, 0, 0, 0)).valueOf();

mocking an internally called function

Problem

Mocking a function that is called within the same module and asserting on the caller does not work - the original function will be called and not the mock.

myModule.ts

export const myFunction = () => {
  return "not mocked";
};

export const myCallingFunction = () => {
  return myFunction();
};

myModule.test.ts

import * as myModule from "./myModule";

beforeEach(() => {
  jest.spyOn(myModule, "myFunction").mockReturnValue("mocked");
});

it("should mock", () => {
  const result = myModule.myFunction();
  expect(result).toBe("mocked");
});
it("should mock caller", () => {
  const result = myModule.myCallingFunction();
  expect(result).toBe("mocked");
});

Fails with:

 FAIL  src/myModule.test.ts
  ✓ should mock (3 ms)
  ✕ should mock caller (3 ms)

  ● should mock caller

    expect(received).toBe(expected) // Object.is equality

    Expected: "mocked"
    Received: "not mocked"

      11 | it("should mock caller", () => {
      12 |   const result = myModule.myCallingFunction();
    > 13 |   expect(result).toBe("mocked");
         |                  ^
      14 | });
      15 |

      at Object.<anonymous> (src/machines/myModule.test.ts:13:18)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total

Solution

To fix, import the module into itself and reference the import in the caller:

myModule.ts - fixed

import * as self from "./myModule";

export const myFunction = () => {
  return "not mocked";
};

export const myCallingFunction = () => {
  return self.myFunction();
};

Passes:

 PASS  src/myModule.test.ts
  ✓ should mock (3 ms)
  ✓ should mock caller (1 ms)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total

Other options include switching to class or splitting into separate files.

See facebook/jest#936, specifically the summary, and then using a self reference

extending stacktrace length

Jest cutting off stack traces too early when tests fail? Make them longer with:

Error.stackTraceLimit = 50;

using typescript with jest

Run in shell:

npm i --save-dev jest \
    @types/jest \
    babel-jest \
    @babel/core \
    @babel/preset-env \
    @babel/preset-typescript

Save to babel.config.js:

module.exports = {
  presets: [
    ["@babel/preset-env", { targets: { node: "current" } }],
    "@babel/preset-typescript",
  ],
};