Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

jest with esm is running in shared scope #492

Closed
dnalborczyk opened this issue Jul 4, 2018 · 3 comments
Closed

jest with esm is running in shared scope #492

dnalborczyk opened this issue Jul 4, 2018 · 3 comments

Comments

@dnalborczyk
Copy link
Contributor

dnalborczyk commented Jul 4, 2018

node v10.5.0
esm: v3.0.62
jest: v23.3.0

problem: jest test files are running individually in their own scope, though when to-be-tested-modules files are being loaded with esm the scope is being shared.

repro:

https://github.com/dnalborczyk/esm-jest-repro/tree/jest-scope

edit: added repro

@jdalton
Copy link
Member

jdalton commented Jul 4, 2018

Ah yes, this would make sense because we are sharing the context 😋
It can be adjusted to be individualized though.

Update:

Ok so in digging into this a little more the issue isn't about scopes or contexts it's about shared Module._cache data. The esm loader will share cache data per identically configured instance. I can see now though that each instance should have their own module cache.

Update:

Ok so digging into it even more we don't share Module._cache between instances explicitly it's the fact that our internal Module._cache is initialized with that of CJS (the real Module._cache). Jest must not be clearing the real Module._cache which is why ours is allowed to persist.

Deferring to the builtin CJS cache is a good thing that helps with CJS interop. I don't want to remove that so instead I can detect if esm is running in jest and start fresh in that case only. When running in jest there is a global free variable jest that I can detect.

@jdalton jdalton added the bug label Jul 6, 2018
@jdalton jdalton closed this as completed in 25e9c87 Jul 6, 2018
@dnalborczyk
Copy link
Contributor Author

incredible work!! thanks for the updates! much appreciated!

the bug is fixed in master, tests are passing nicely! 🎉 🎉 🎉

@jdalton
Copy link
Member

jdalton commented Jul 6, 2018

v3.0.63 is released 🎉

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

2 participants