Jest Cheat Sheet In NodeJS

Status
Publishing
Created By
Empty
Last Edited Time
8/19/2020, 3:18:00 PM
Description
Use cases when using NodeJs and Jest.
Tags
Unit Test
Jest
JavaScript
NodeJS
πŸ™‡
Thanks for visiting this post. All feedbacks on this post are always welcome. Please send your the feedback by lkh5510@gmail.com.
β€’
Mocking a function in the module
When testing the doSomething1() function, If the doSomething1() function in ctrl.js module invokes doSomething2() in the same module, you can do as follows.
β€’
ctrl.js
exports.doSomething1 = (a) => { ... const result = exports.doSomething2(); };
JavaScript
β€’
ctrl.test.js
There is jest.fn(), but if you use jest.spyOn(), you can even check the existence of a function.
const ctrl = require('./ctrl'); describe('ctrl', () => { afterEach(() => { /* * Initialize to the original module * so that the mock function created using spyOn * does not affect other tests. */ jest.restoreAllMocks(); }); describe('doSomething1', () => { test('...', () => { const doSomething2Mock = jest .spyOn(ctrl, 'doSomething2') .mockReturnedValue(1); const ret = ctrl.doSomething1(); ... }); }); });
JavaScript
β€’
Mocking a variable in the module
If you are mocking the PREFIX variable in module1.js, you can do it as follows.
β€’
module1.js
exports.PREFIX = 'pre'; exports.doSomething = a => `${exports.PREFIX}_${a}`;
JavaScript
β€’
module1.test.js
describe('module', () => { describe('doSomething', () => { let oriPrefix; beforeAll(() => { oriPrefix = module1.PREFIX; }); afterAll(() => { module1.PREFIX = oriPrefix; }); test('test', () => { module1.PREFIX = '1'; ... }); }); });
JavaScript
β€’
Testing a async function
When testing the doSomething() function in ctrl.js that includes the callback function, do as follows.
β€’
ctrl.js
exports.doSomething = async () => {...};
JavaScript
β€’
ctrl.test.js
const ctrl = require('./ctrl'); describe('ctrl', () => { describe('doSomething', () => { test('...', async () => { const result = await ctrl.doSomething(); ... }); }); });
JavaScript
β€’
Testing a callback function(res.json(), ...)
β€’
ctrl.js
exports.doSomething = (e) => { e.on('stop', () => { ... }); };
JavaScript
β€’
ctrl.test.js
const ctrl = require('./ctrl'); describe('ctrl', () => { describe('doSomething', () => { test('...', done => { const e = { on: jest.fn(() => { ... done(); }); }; ctrl.doSomething(e); }); }); });
JavaScript
β€’
Testing throw new Error()
When checking whether the doSomething() function in ctrl.js throws an error, do as follows.
β€’
ctrl.js
exports.doSomething = () => { throw new Error('?'); };
JavaScript
β€’
ctrl.test.js
const ctrl = require('./ctrl'); describe('ctrl', () => { describe('doSomething', () => { test('...', () => { expect(() => ctrl.doSomething()).toThrow(Error); }); }); });
JavaScript
If doSomething() function is asynchronous, you can check whether an error is thrown as follows.
β€’
ctrl.js
exports.doSomething = async () => { ... throw Error('?'); };
JavaScript
β€’
ctrl.test.js
const ctrl = require('./ctrl'); describe('ctrl', () => { describe('doSomething', () => { test('...', async () => { await expect(ctrl.doSomething()).toThrow(Error); }); }); });
JavaScript