手动模拟
Manual mocks are used to stub out functionality with mock data. For example, instead of accessing a remote resource like a website or a database, you might want to create a manual mock that allows you to use fake data. This ensures your tests will be fast and not flaky. For example, instead of accessing a remote resource like a website or a database, you might want to create a manual mock that allows you to use fake data. This ensures your tests will be fast and not flaky.
Mocking user modules
Manual mocks are defined by writing a module in a __mocks__/
subdirectory immediately adjacent to the module. For example, to mock a module called user
in the models
directory, create a file called user.js
and put it in the models/__mocks__
directory. Note that the __mocks__
folder is case-sensitive, so naming the directory __MOCKS__
will break on some systems. For example, to mock a module called user
in the models
directory, create a file called user.js
and put it in the models/__mocks__
directory.
The __mocks__
folder is case-sensitive, so naming the directory __MOCKS__
will break on some systems.
When we require that module in our tests (meaning we want to use the manual mock instead of the real implementation), explicitly calling jest.mock('./moduleName')
is required.
Mocking Node modules
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.mock('module_name')
. There's no need to explicitly call jest.mock('module_name')
.
Scoped modules (also known as scoped packages) can be mocked by creating a file in a directory structure that matches the name of the scoped module. Scoped modules can be mocked by creating a file in a directory structure that matches the name of the scoped module. For example, to mock a scoped module called @scope/project-name
, create a file at __mocks__/@scope/project-name.js
, creating the @scope/
directory accordingly.
If we want to mock Node's built-in modules (e.g.: fs
or path
), then explicitly calling e.g. jest.mock('path')
is required, because built-in modules are not mocked by default.