[Bug]: Not able to access window in vm.runInContext
rahulmr-ibm opened this issue · 8 comments
Version
29.7.0
Steps to reproduce
Create these files, then npm I and npm test
Package.json
{
"name": "jsdom-tes",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "jest"
},
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0"
},
"dependencies": {
"jsdom": "^24.1.0"
}
}
My jest config file
const config = {
testEnvironment: 'jsdom',
setupFilesAfterEnv: [ "<rootDir>/jestSetup.js"],
testMatch: ["<rootDir>/test/__test__/mytest.test.js"],
};
module.exports = config;
Jest setup. file
const vm = require('node:vm');
const fs = require( "fs" );
const ctx = vm.createContext(global);
ctx.hello = "helloworld";
ctx.window = "window";
const sContents = 'var a =1'
try {
const result = vm.runInContext(`${sContents}; console.log(hello); console.log(window) `, ctx);
} catch(e) {
console.log(e.stack, 'err1');
console.log('x is', ctx.x)
}
whenever I run this code in Node 20, im getting reference errors, working correctly in Node 16
evalmachine.<anonymous>:12
; console.log(hello); console.log(window)
^
ReferenceError: window is not defined
Expected behavior
window and other Dom should be accessible for tests
Actual behavior
I'am working on a legacy project, I was running on node 16.
I tried to upgrade node to 20, and seeing these errors.
when run using vm.runInContext, not able to access window or document.
Additional context
No response
Environment
System:
OS: macOS 14.5
CPU: (12) arm64 Apple M3 Pro
Binaries:
Node: 20.14.0 - ~/.nvm/versions/node/v20.14.0/bin/node
npm: 10.7.0 - ~/.nvm/versions/node/v20.14.0/bin/npm
npmPackages:
jest: ^29.7.0 => 29.7.0
when we are doing vm.createContext(global), we are not able to access window in the js files, even though we have injected window into context
You're trying to run vm
inside of vm
, which is not really supported. Why do you need to do that?
You're trying to run
vm
inside ofvm
, which is not really supported. Why do you need to do that?
Im working on a legacy js project, we don't have export and import. so we are using vm.createcontext
to inject some js files globally before all tests runs, especially some class
.
Is there any better approach to do this.
Also what do you mean by vm inside vm ?
inject some js files globally before all tests runs, especially some class.
Is there any better approach to do this.
https://jestjs.io/docs/configuration#setupfiles-array should do what you want.
Also what do you mean by vm inside vm ?
Jest already runs all tests within separate vm contexts
inject some js files globally before all tests runs, especially some class.
Is there any better approach to do this.https://jestjs.io/docs/configuration#setupfiles-array should do what you want.
Also what do you mean by vm inside vm ?
Jest already runs all tests within separate vm contexts
Still we need to inject the class and singletons to the global object right ?. since we don't have exports and imports
for example
var myVar = {};
myVar.getName = function(){
return 'rahul mr'
}
it('hello this is my test', () => {
console.log('hello', myVar.getName());
})
For this to work , we are required to run vm.runincontext()
with global as context