无法访问 jest.mock 函数中的变量 [英] Not able to access variables in jest.mock function
本文介绍了无法访问 jest.mock 函数中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请检查以下场景
const url = 'https://mock.com/mockpath';
jest.mock('../../src/somefile', () => {
return ({
getURL: jest.fn(() => url),
});
});
我收到错误
babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
Invalid variable access: url
推荐答案
礼貌 @skyboyer
刚刚将 url 重命名为 mockUrl,任何以 mock 开头的变量都可以在 jest.mock 中访问.
Just renamed url to mockUrl, any variable starting with mock is accessible inside jest.mock.
@skyboyer 再次感谢您的帮助.
这篇关于无法访问 jest.mock 函数中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文