DEV Community

wes5510
wes5510

Posted on • Edited on

4 2

Jest Cheat Sheet In NodeJS

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 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();
      };
      
    • 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();
                  ...
              });
          });
      });
      
  • 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}`;
      
    • 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 in ctrl.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 in ctrl.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);
              });
          });
      });
      

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →