json-bigint icon indicating copy to clipboard operation
json-bigint copied to clipboard

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

Open lvl99 opened this issue 4 years ago • 4 comments

I'm running my project within GCP Cloud Build and I'm getting the following error while running tests within Jest via NPM:

Step #1: ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.
Step #1: 
Step #1:       at number (node_modules/gcp-metadata/node_modules/json-bigint/lib/parse.js:208:44)
Step #1:       at value (node_modules/gcp-metadata/node_modules/json-bigint/lib/parse.js:397:41)
Step #1:       at object (node_modules/gcp-metadata/node_modules/json-bigint/lib/parse.js:367:27)
Step #1:       at value (node_modules/gcp-metadata/node_modules/json-bigint/lib/parse.js:389:16)
Step #1:       at Function.parse (node_modules/gcp-metadata/node_modules/json-bigint/lib/parse.js:410:14)

When I look at the lines in gcp-metadata/node_modules/json-bigint/lib/parse.js:208 it seems that there's a require('BigNumber') which I think is causing the above issue in my test:

https://github.com/sidorares/json-bigint/blob/master/lib/parse.js#L208

I'm also importing BigNumber within my own project (in fact, in the tests that this error occurs, it's for functionality that I've built that relies on BigNumber) so I'm in a weird tricky spot where a dependency is doing magical things (gcp-metadata is being invoked during the test /somehow/) and a dependency of that (json-bigint) has an error which is completely halting my CI deployment process.

It seems like BigNumber should just be required at the top of the script (where you have var BigNumber = null) and avoid the inline import with conditional check on line 208. The conditional check seems like it should only require BigNumber if _options.useNativeBigInt is false.

lvl99 avatar Apr 15 '21 09:04 lvl99

This is really killing me. No idea how I can resolve this! What I thought would be a simple deployment has turned into hair pulling bald head episode.

lvl99 avatar Apr 20 '21 12:04 lvl99

bumping as I have the same issue

janmarcano avatar Jan 24 '22 16:01 janmarcano

Yep, my Cloud Build is still broken...

lvl99 avatar Jan 24 '22 16:01 lvl99

I'm getting this error as well. Has anyone found a solution?

juni0r avatar Mar 25 '22 21:03 juni0r