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()
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();