Jest: ReferenceError: You are trying to `import` a file after the Jest environment has been torn down

Jest: ReferenceError: You are trying to `import` a file after the Jest environment has been torn down

我正在使用 Jest 对我的 Sails JS 应用程序进行单元测试。

测试通过并完成后,出现此错误:

Ran all test suites.

ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From test/controllers/foo/bar.test.js.

      at readdir (node_modules/readdirp/readdirp.js:49:25)
      at FSWatcher.<anonymous> (node_modules/chokidar/lib/nodefs-handler.js:355:5)
      at FSWatcher.Object.<anonymous>.NodeFsHandler._handleDir (node_modules/chokidar/lib/nodefs-handler.js:406:18)
      at FSWatcher.<anonymous> (node_modules/chokidar/lib/nodefs-handler.js:455:19)
      at FSWatcher.<anonymous> (node_modules/chokidar/lib/nodefs-handler.js:460:16)
/.../node_modules/readdirp/readdirp.js:49
    var api          =  require('./stream-api')();
                                               ^

TypeError: require(...) is not a function
    at readdir (/.../node_modules/readdirp/readdirp.js:49:48)
    at FSWatcher.<anonymous> (/.../node_modules/chokidar/lib/nodefs-handler.js:355:5)
    at FSWatcher.Object.<anonymous>.NodeFsHandler._handleDir (/.../node_modules/chokidar/lib/nodefs-handler.js:406:18)
    at FSWatcher.<anonymous> (/.../node_modules/chokidar/lib/nodefs-handler.js:455:19)
    at FSWatcher.<anonymous> (/.../node_modules/chokidar/lib/nodefs-handler.js:460:16)

我将 Jest 超时设置为 10000。

我也试过使用 Jest 假计时器。

我在发这个问题之前做了研究,但是没有找到好的解决方案。

我认为我的案例是针对帆的工作原理的,但我会添加修复以供参考:

NODE_ENV=test jest --runInBand

参考:https://jestjs.io/docs/cli#--runinband