Getting ReferenceError: You are trying to import a file after the Jest environment has been torn down. in test suite

Viewed 953

I have a folder called test and inside of it I have subfolders with *.test.js files. I'm trying to run all test files using JEST but I'm having this error ReferenceError: You are trying to import a file after the Jest environment has been torn down. .

To get the all path files I'm using fs.readdir with following code:

const readPathFiles = new Promise((resolvePath, err) => fs.readdir(__dirname, (err, files) => {

  const folders = files.filter(name => fs.lstatSync(`${__dirname}/${name}`).isDirectory())

  const resolvers = folders.map(folderName => {

    return new Promise((resolveFiles, err) => fs.readdir(`${__dirname}/${folderName}`, (err, content) => {

      const files = content.filter(name => !fs.lstatSync(`${__dirname}/${folderName}/${name}`).isDirectory())

      const filesPath = files.map(tests => {
        return `./${folderName}/${tests}`
      })

      resolveFiles(filesPath)
    })

    )
  })

  Promise.all(resolvers).then(data => resolvePath(data))

}))

With that promise, I'm using the then() to get the path after the Pomise has resolved. I'm using it whit the following code:

describe('TESTS', () => {

  readPathFiles.then(data => {
    data.forEach(file => {
      describe(`\n ${file}`, () => {
        require(file)
      })
    })
  })

})

I'm running the code with yarn run jest and the console shows me the error:

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

      37 |   readPathFiles.then(data => {
      38 |     data.forEach(file => {
    > 39 |       describe(`\n ${file}`, () => {
         |         ^
      40 |         require(file)
      41 |       })
      42 |     })

      at Suite.describe (__tests__/index.test.js:39:9)
      at data.forEach.file (__tests__/index.test.js:38:7)

How could I run a suite of files test with JEST ? There is a good way to run it?

0 Answers
Related