joystick icon indicating copy to clipboard operation
joystick copied to clipboard

Add unit and integration tests for @joystick.js/node

Open rglover opened this issue 4 years ago • 1 comments

rglover avatar Oct 27 '21 16:10 rglover

Making a note of this for later so I can get it out of the test file:

jest.mock('../../node_modules/express', () => {
  const express = () => {
    return {
      use: jest.fn(),
      get: jest.fn(),
      post: jest.fn(),
      listen: () => {
        return {
          hello: true,
        }
      },
    };
  };

  express.json = () => jest.fn();
  express.static = () => jest.fn();

  return express;
});

In case you get confused, make sure to manually hoist this in the file above the import for the code being tested. If it's run after the import, the mock won't apply. This also applies to any standalone files you're using but not testing. For example:

jest.mock('../../node_modules/crypto-extra', () => {
  return {
    randomString: () => 'abc1234',
  };
});

jest.mock('../../node_modules/bcrypt', () => {
  return {
    hashSync: () => 'hashed$password',
    compareSync: () => {
      return true;
    },
  };
});

const app = (await import('./index')).default;
const generateId = (await import('../lib/generateId')).default;

generateId() here is used in the test file and relies on the crypto-extra package to generate IDs on user signup (indirectly inside of /app/index.js here).

rglover avatar Nov 30 '21 18:11 rglover