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

问题描述 投票:0回答:1

请检查以下情况

const mockURL = 'https://mock.com/mockpath';
jest.mock('../../src/somefile', () => {
  return ({
    getURL: jest.fn(() => mockURL ),
  });
});

我遇到错误

babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
Invalid variable access: mockURL 
reactjs unit-testing mocking jest
1个回答
0
投票
`jest.mock()` is not allowed to reference any out-of-scope variables.

无效的变量访问:mockURL

告诉您jest.mock()正在尝试访问mockURL,但不允许其访问范围外的变量。]​​>

您应该在mockURL函数调用中声明jest.mock()

jest.mock('../../src/somefile', () => {
  const mockURL = 'https://mock.com/mockpath';
  return ({
    getURL: jest.fn(() => mockURL ),
  });
});
© www.soinside.com 2019 - 2024. All rights reserved.