Javascript ReferenceError:您试图在Jest环境被破坏后“导入”文件
我有一个组件,它使用react native的Javascript ReferenceError:您试图在Jest环境被破坏后“导入”文件,javascript,reactjs,react-native,jestjs,enzyme,Javascript,Reactjs,React Native,Jestjs,Enzyme,我有一个组件,它使用react native的动画组件。我开始编写一个测试用例来模拟一个组件的onPress,该组件调用一个函数,该函数在其中设置了动画.timening,以及setState 运行jest可以很好地工作,但是测试从未停止运行,而且我以前编写的一个不相关的测试用例现在似乎从未通过(以前通过过) 运行jest--watch,我得到以下错误: ReferenceError: You are trying to `import` a file after the Jest enviro
动画组件。我开始编写一个测试用例来模拟一个组件的onPress
,该组件调用一个函数,该函数在其中设置了动画.timening
,以及setState
运行jest
可以很好地工作,但是测试从未停止运行,而且我以前编写的一个不相关的测试用例现在似乎从未通过(以前通过过)
运行jest--watch
,我得到以下错误:
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.
at Function.bezier (node_modules/react-native/Libraries/Animated/src/Easing.js:113:21)
at ease (node_modules/react-native/Libraries/Animated/src/Easing.js:34:24)
at TimingAnimation._easing (node_modules/react-native/Libraries/Animated/src/Easing.js:133:18)
at TimingAnimation.onUpdate (node_modules/react-native/Libraries/Animated/src/animations/TimingAnimation.js:107:45)
RUNS src/__tests__/SlideDownMenu.test.js
/home/nrion/Desktop/mobile-ui/PriceInsight_app/node_modules/react-native/Libraries/Animated/src/Easing.js:114
return _bezier(x1, y1, x2, y2);
^
TypeError: _bezier is not a function
at Function.bezier (/home/nrion/Desktop/mobile-ui/PriceInsight_app/node_modules/react-native/Libraries/Animated/src/Easing.js:224:12)
at ease (/home/nrion/Desktop/mobile-ui/PriceInsight_app/node_modules/react-native/Libraries/Animated/src/Easing.js:94:21)
at TimingAnimation._easing (/home/nrion/Desktop/mobile-ui/PriceInsight_app/node_modules/react-native/Libraries/Animated/src/Easing.js:255:16)
at TimingAnimation.onUpdate (/home/nrion/Desktop/mobile-ui/PriceInsight_app/node_modules/react-native/Libraries/Animated/src/animations/TimingAnimation.js:138:14)
at ontimeout (timers.js:386:11)
at tryOnTimeout (timers.js:250:5)
at Timer.listOnTimeout (timers.js:214:5)
链接到repl
环境:
- 操作系统:Linux 4.14
- 节点:6.14.2
- 纱线:1.7.0
- 净现值:3.10.10
- 看守员:没有找到
- Xcode:不适用
- Android Studio:未找到
好的,找到了解决方案
应该使用
jest.useFakeTimers()
注意:将上面的代码放在测试文件的import
部分之后。jest.useFakeTimers()
有了以上内容,理解这一点非常重要
useFakeTimers()用模拟函数模拟setTimeout和其他计时器函数
如果在一个文件或描述块中运行多个测试,则jest.useFakeTimers();可以在每次测试之前手动调用,也可以使用设置函数(如Beforeach)调用
不这样做将导致内部使用计数器无法重置。我在使用react native testing library
测试Apollo时遇到此问题
在本例中,在父组件和子组件中有两个查询。在子查询呈现和启动查询之前,需要解析父查询
解决方案是运行wait()
函数两次,而不是只运行一次。我假设双重火力是确保两个查询都运行所必需的。不过,错误消息非常不透明
test("...", () => {
const rr = render(...);
await wait();
await wait(); // Child query must also run
expect(...);
}
// Wait a tick so the query runs
// https://www.apollographql.com/docs/react/development-testing/testing/#testing-final-state
export async function wait(ms = 0) {
await act(async () => {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
});
}
在jest测试文件的末尾添加以下行,
毕竟,测试是书面的
afterAll(() => {
mongoose.connection.close()
})
并且每次只运行一个测试文件,例如
>jest--verbose--runInBand--filename.test.js
我使用的是@testing library/react native
,所以我在安装文件中使用了cleanup
//jest.setup.js
从'@testing library/react native'导入{cleanup};
每次之后(清理);
添加“测试环境”:“jsdom”
到jest
在package.json或jest.config.js中键入
“开玩笑”:{
“测试环境”:“jsdom”,
“预设”:“反应本机”,
...
摘自:我想对答案做出贡献
让我们看看错误消息
ReferenceError:您试图在Jest环境被破坏后“导入”文件。
拆掉意味着:Jest已经完成运行,并且在Jest拆掉或退出测试后,您的部分代码正在尝试执行。这在Javascript上非常常见,因为它是异步的
有时,它发生在执行承诺回调之后,例如:
import { someProcess} from 'a-library'
task.job().then(result => {
someProcess(result)
})
在上面的示例中,代码从a-library
如果来自task
对象的方法job
比jest执行其回调(then()调用)花费的时间更长将在jest外部运行,因为jest已继续。因此,当执行someProcess
时,它将从a-library
加载,因此jest将抱怨您试图在jest被撕裂后加载库
标记为解决方案的答案部分是正确的,因为调用jest.useFakeTimers()
将阻止您的代码等待您本应等待的n秒,从而调用setTime或类似命令,使您的代码人为地成为sincrhonoyus
让您的测试等待这些方法调用将有助于您更好地理解代码
对我有效的代码是
describe("Some test set " , ()=>{
let heavyWorkinService = library.workingService();
// We are marking the test function call as async
test(("Name of the test"), async ()=>{
// we are awaiting heavyWorkingService to done its job
await hevyWorkingService("some data")
})
})
在我的真实场景中,我的代码从firebase获取数据,因为我没有使用mock进行此测试,它在读取数据后尝试导入firebase。get()返回数据,因此将测试标记为async
,并使用wait
关键字调用方法解决了我的问题
当然,有很多方法可以处理此错误,只是想让您知道此错误背后的真正原因,以便为您的测试找到正确的解决方案!我们在哪里使用此方法?@dopatraman导入后,我在测试文件中使用了此方法。(正确吗?有效。)但请参见下面TARJU中关于其使用的内容。如果你有一个src_uuutests_uuuu\jestSetup.ts,最好将它设置在那里,它将对每个测试都有效。不幸的是,我在一个测试文件中使用了websocket Connenction,该文件不断失败,添加了--runInBand
参数,为我解决了这个问题。@Stan欢迎你,感谢feedb确认。此解决方案无法解决。我仍然有问题。您可能希望尝试jest--detectOpenHandles--runInBand