Home > database >  Jest: ReferenceError: You are trying to `import` a file after the Jest environment has been torn dow
Jest: ReferenceError: You are trying to `import` a file after the Jest environment has been torn dow

Time:12-08

I'm using Jest to unit test my Sails JS application.

When the tests pass and finish, I'm getting this error:

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)

I set Jest timeout to 10000.

I also tried using Jest fake timers.

I've done the research before posting this question, but haven't found a good solution.

CodePudding user response:

I think my case is specific to how sails work, but I'll add the fix for reference:

NODE_ENV=test jest --runInBand

Ref: https://jestjs.io/docs/cli#--runinband

  • Related