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

missing process.env.xxx with jest and esm #480

Closed
dnalborczyk opened this issue Jun 26, 2018 · 3 comments
Closed

missing process.env.xxx with jest and esm #480

dnalborczyk opened this issue Jun 26, 2018 · 3 comments
Labels

Comments

@dnalborczyk
Copy link
Contributor

just for reference, I don't think the versions matter:

esm: v3.0.55
jest: v23.2.0
node: v10.5.0

I forked a project and converted the code incl. tests from ava to use esm and jest.

it seems that the "global" process object, and/or the process.env object is being cloned - or re-created? I haven't debugged it yet.

some tests are setting the "global" env variable prior running the test functions. if run without esm, it works, running with esm, fails.

not quite sure if this is a bug with jest or esm, or just intended behavior.

might be best explained with some code: https://github.com/dnalborczyk/esm-jest-repro

@jdalton
Copy link
Member

jdalton commented Jun 26, 2018

Hi @dnalborczyk!

This is because jest is mocking the process object so it's not the real one.

require("process") === process // will be `false` in the jest test code

It would be nice if the esm loader could have access to these mocked globals though. I'll see about adding an option to specify them.

@dnalborczyk
Copy link
Contributor Author

thanks @jdalton for the info! though I'm surprised that the test only doesn't work when esm is being required - but works without.

though your comment made me thinking that explicitly requiring process (then non-mocked?) maybe would do:

require = require('esm')(module)
const process = require('process')

process.env.test = 'some-value'

which made the scenario work!

not sure if anything should be done on esm's side of things?

@jdalton
Copy link
Member

jdalton commented Jun 27, 2018

Jest runs its code in its own unique vm context (different than the default vm context) so I can simulate this with the global at least by detecting that the context is different and running it in a similar one.

Update:

v3.0.56 is released 🎉

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

No branches or pull requests

2 participants