jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.12k stars 6.44k forks source link

[Bug]: Not able to access window in vm.runInContext #15108

Closed rahulmr-ibm closed 1 month ago

rahulmr-ibm commented 3 months ago

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
rahulmr-ibm commented 3 months ago

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

SimenB commented 3 months ago

You're trying to run vm inside of vm, which is not really supported. Why do you need to do that?

rahulmr-ibm commented 3 months ago

You're trying to run vm inside of vm, 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 ?

SimenB commented 3 months ago

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

rahulmr-ibm commented 3 months ago

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

github-actions[bot] commented 2 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] commented 1 month ago

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] commented 1 month ago

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] commented 2 weeks ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.