Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest - one function mocks file for multiple test files

I want to have something like this:

mockFunctions.ts

jest.mock('../utils', () => {
  return {
     getNumbers: () => [1,2,3]
  }
})

__tests__/test1.ts

---import from mockFunctions---
...
it('After adding another number array has more elements', () => {
   const numbers = <get these numbers using mock function>
   expect([...numbers, 11]).toHaveLength(4);
})

__tests__/test2.ts

---import from mockFunctions---
...
it('After removing a number, array has less elements', () => {
   const numbers = <get these numbers using mock function>
   expect(numbers.filter(x => x>1)).toHaveLength(2);
})

Is it possible to have one file where mocked functions are implemented, and then import them in multiple tests files?

like image 311
stackuba Avatar asked Nov 20 '20 13:11

stackuba


People also ask

Does Jest reset mocks between files?

You don't have to reset the mocks, as the test are run in parallel, every test file run in its own sandboxed thread. Even mocking JavaScript globals like Date or Math. random only affects the actual test file.

How do you mock a function in another Jest?

To change the mock implementation of a function with Jest we use the mockImplementation() method of the mocked function. The mockImplementation() method is called with the new implementation as its argument. The new implementation will then be used in place of the previous one when the mock is called.

Where do you put Jest mocks?

If the module you are mocking is a Node module (e.g.: lodash ), the mock should be placed in the __mocks__ directory adjacent to node_modules (unless you configured roots to point to a folder other than the project root) and will be automatically mocked. There's no need to explicitly call jest.

Are Jest mocks Global?

If you need to mock a global variable for all of your tests, you can use the setupFiles in your Jest config and point it to a file that mocks the necessary variables. This way, you will have the global variable mocked globally for all test suites.


1 Answers

There are some alternative to accomplish this:

  1. Add __mocks__ directory inside utils folder. See https://jestjs.io/docs/en/manual-mocks

utils/index.js

export const  getNumbers= () => [1, 2, 3, 4, 5];

->utils/__mocks__/index.js

export const  getNumbers= () => [3, 4];

jest.config.js

{ 
   "setupFilesAfterEnv": [
      "<rootDir>/jestSetup.js"
    ]
}

jestSetup.js

jest.mock("../utils"); // will apply to all tests
  1. Add mock definition directly in jestSetup.js

jest.config.js

{ 
   "setupFilesAfterEnv": [
      "<rootDir>/jestSetup.js"
    ]
}

jestSetup.js

  jest.mock("../utils", () => ({
     getNumbers: () => [3, 4]
  }));

or create a file with mocks

mocks.js

  jest.mock("../utils", () => ({
     getNumbers: () => [3, 4]
  }));

jestSetup.js

  import './mocks.js'

If you don't want to use mocks on specific test, you can call:

jest.unmock('../utils')

See: https://jestjs.io/docs/en/jest-object#jestunmockmodulename

like image 131
lissettdm Avatar answered Oct 23 '22 20:10

lissettdm