无法访问 jest.mock 函数中的变量

vnx*_*xyz 6 unit-testing mocking reactjs jestjs

请检查以下场景

const url = 'https://mock.com/mockpath';
jest.mock('../../src/somefile', () => {
  return ({
    getURL: jest.fn(() => url),
  });
});
Run Code Online (Sandbox Code Playgroud)

我收到错误

babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
Invalid variable access: url
Run Code Online (Sandbox Code Playgroud)

vnx*_*xyz 18

礼貌@skyboyer

只需将 url 重命名为mockUrl,任何以mock 开头的变量都可以在jest.mock 中访问。

@skyboyer再次感谢您的帮助。