Jest Cheat Sheet In NodeJS
wes5510
Posted on August 19, 2020
Thanks for visiting this post.
All feedbacks on this post are always welcome.
-
Mocking a function in the module
When testing the
doSomething1()
function, If thedoSomething1()
function inctrl.js
module invokesdoSomething2()
in the same module, you can do as follows.-
ctrl.js
exports.doSomething1 = (a) => { ... const result = exports.doSomething2(); };
-
ctrl.test.js
There is
jest.fn()
, but if you usejest.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(); ... }); }); });
-
-
Mocking a variable in the module
If you are mocking the
PREFIX
variable inmodule1.js
, you can do it as follows.-
module1.js
exports.PREFIX = 'pre'; exports.doSomething = a => `${exports.PREFIX}_${a}`;
-
module1.test.js
describe('module', () => { describe('doSomething', () => { let oriPrefix; beforeAll(() => { oriPrefix = module1.PREFIX; }); afterAll(() => { module1.PREFIX = oriPrefix; }); test('test', () => { module1.PREFIX = '1'; ... }); }); });
-
-
Testing a async function
When testing the
doSomething()
function inctrl.js
that includes the callback function, do as follows.-
ctrl.js
exports.doSomething = async () => {...};
-
ctrl.test.js
const ctrl = require('./ctrl'); describe('ctrl', () => { describe('doSomething', () => { test('...', async () => { const result = await ctrl.doSomething(); ... }); }); });
-
-
Testing a callback function(
res.json()
, ...)-
ctrl.js
exports.doSomething = (e) => { e.on('stop', () => { ... }); };
-
ctrl.test.js
const ctrl = require('./ctrl'); describe('ctrl', () => { describe('doSomething', () => { test('...', done => { const e = { on: jest.fn(() => { ... done(); }); }; ctrl.doSomething(e); }); }); });
-
-
Testing
throw new Error()
When checking whether the
doSomething()
function inctrl.js
throws an error, do as follows.-
ctrl.js
exports.doSomething = () => { throw new Error('?'); };
-
ctrl.test.js
const ctrl = require('./ctrl'); describe('ctrl', () => { describe('doSomething', () => { test('...', () => { expect(() => ctrl.doSomething()).toThrow(Error); }); }); });
If
doSomething()
function is asynchronous, you can check whether an error is thrown as follows.-
ctrl.js
exports.doSomething = async () => { ... throw Error('?'); };
-
ctrl.test.js
const ctrl = require('./ctrl'); describe('ctrl', () => { describe('doSomething', () => { test('...', async () => { await expect(ctrl.doSomething()).rejects.toThrow(Error); }); }); });
-
Posted on August 19, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.