使用函数模拟多个模块

Mocking many modules using a function

我已经为经常调用的端点制作了模拟,但不想为每个测试多次调用 jest.mock。为此,我创建了以下函数:

// src/endpoints/__utils__/mockEndpoints.ts
export function mockEndpoints(): void {
  jest.mock('../do-log-in');
  jest.mock('../get-api-key');
  jest.mock('../get-rsa-key');
  // etc
}

不幸的是,在我的测试中调用此函数无效。 jest.mock 里面的路径是相对于文件的路径。 我用 require.resolve 检查了它们,它们确实解析到了正确的位置。 使用 jest.mock(require.resolve(path)) 也没有用。 我还尝试将 mockEndpoints jest 作为参数,以确保它使用的是正确的实例。但这也没有用

我在文档中发现了以下注释:

Note: In order to mock properly, Jest needs jest.mock('moduleName') to be in the same scope as the require/import statement.

读到这里,我尝试在包含 mockEndpoints() 的文件中导入我想要模拟的所有模块,然后确保该文件是我的测试文件的第一个导入。不幸的是,这也没有用。

我的测试文件看起来有点像这样:

import { mockEndpoints } from '../endpoints/__utils__/mockEndpoints';

mockEndpoint(); // does not do anything

// the following *does* work:
// jest.mock('../endpoints/do-log-in');
// jest.mock('../endpoints/get-api-key');
// jest.mock('../endpoints/get-rsa-key');
// etc

describe('test cases', () => {
  /* test cases that indirectly use the mocked endpoints */
})

此时我不确定如何继续实现这一目标。感谢帮助

这里的问题是我没有在导入之前调用 jest.mock。在测试文件中使用 jest.mock 时,jest 会自动将其提升到顶部(即,在被模拟的导入之前)。当 jest.mock 像我一样包含在函数中时,Jest 无法自动执行此操作。

只需导入这个函数,调用它,然后再导入其他东西就可以了。


替代解决方案使用 setupFiles:

我可以通过将代码放在我的 mockEndpoints 函数中的一个文件中,并将该文件的路径放在我的 jest 配置中的 setupFiles 选项中来实现相同的效果。例如:

// src/endpoints/__utils__/mockEndpoints.ts
jest.mock('../endpoints/do-log-in');
jest.mock('../endpoints/get-api-key');
jest.mock('../endpoints/get-rsa-key');
// jest.config.ts
export default {
  setupFiles: ['./src/endpoints/__utils__/mockEndpoints.ts'],
};