-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ReferenceError: requirejsVars is not defined #1914
Comments
Also tried adding |
Also important
|
FYI we don't use AMD/requirejs for our tests, but a lib we use in our testing also supports AMD style tests, so it relies on |
The code in question very likely relies on |
While this is a real issue, our team at least has found a workaround to avoid the |
@xjamundx |
I worked around this problem with the following hack:
This only works because my code does not actually use require.js bundle loader in node.js Also if any other package needs to use vm.runInThisContext during the test it will be broken as well. |
@bd82 nice one. had the same problem, your hack worked for me...maybe this should be investigated by the jest core team |
Glad it helped you @digitalkaoz. |
I'm not really sure we can do anything here. PRs trying to fix it has been ignored for more than 2 years... Trying to support an unsupported library that does crazy stuff seems like it wouldn't be the way to best spend our time. If someone else is able to fix it though, I'll be happy to help it get landed |
Maybe just wait a while longer until nobody would use require.js anymore? 😄 |
Yeah, I'm leaning towards |
Yeah, let's not spend time on this from our side. If somebody feels strongly and can find a clean fix, send a PR and tests, that is obviously always welcome. |
Ok, so I know this is a hack. But I did figure out a way to make this work. /*
THIS IS A HOOK where code is called before the Jest ENV is loaded.
Therefore we can pull in real node modules without JEST'S shenanigans. This is useful for us to load
things like Cesium which need requirejs and can not be loaded in Jests environment.
!!!!!!!!!!!!!!HACK!!!!!!!!!!!!!!!
So here we hack load Cesium which loads fine in pure node. Then inject it
into a global object which Jest exposes to the test environment to allow us to
utilize it and further inject it partially in mocks.
*/
module.exports = function setupGlobal() {
process.$Cesium = require('cesium');
}; Use your jest.config to point to the above file via https://jestjs.io/docs/en/configuration#globalsetup-string . Apologies for the update/edits spam, fat fingered.. doh. |
In your mock for cesium you can now
|
Note this only works for all tests if I tried setupFiles hoping this would inject a preloaded requirejs module into the child process prior to Jests module / require environment being loaded. However this is not the case and It would be nice if there was another step to hook into for each child_process prior to to setupFiles where you have access to manipulating the real node environment without anything jest loaded. |
Actually it looks like
Negative, works for a single test jest env is still preprocessed before globals is called. |
@nmccready , I tried your solution for Cesium, which solved the
Did this not happen to you? |
Yeah so it works as long as the tests stay non-parallel. So you need to turn parallelization off on Jest. This sucks, but I resulted to making all Cesium tests being treated as Integration tests. So when running our test suite it runs normal tests all in parallel and another process for sync/integration tests. I think the confusion in here is Negative. So My thoughts were that Sorry for the confusion. |
This is what worked for me const debug = require('../_debug').spawn('jest:global');
/*
THIS IS A HOOK where code is called before the Jest ENV is loaded.
Therefore we can pull in real node modules without JEST'S BS. This is useful for us to load
things like Cesium which need requirejs and can not be loaded in Jests environment.
!!!!!!!!!!!!!!HACK!!!!!!!!!!!!!!!
So here we hack load Cesium which loads fine in pure node. Then inject it
into a global object which Jest exposes to the test environment to allow us to
utilize it and further inject it partially in mocks.
*/
module.exports = function globalSetup() {
process.$Cesium = require('cesium');
debug(() => process.$Cesium);
}; module.exports = {
globalSetup: '<rootDir>/__jest__/global.js',
....
} |
Looks like I re-answered this... oh well. CHeers |
Do you know of any way to add the |
@nmccready in your hack solution, you have two module.exports. One is to be saved as global.js. Where is the other one saved/saved as? |
I am not doing something right here because this is not working for me. I am using create react app with craco, and craco-cesium to load cesium into my project. I am trying to setup jest to start creating tests but the issue is Cesium is using requireJS. I added the following to my // package.json
...
"jest": {
"collectCoverageFrom": [
"src/**/*.{js,jsx,ts,tsx}', '!src/**/*.d.ts"
],
"globalSetup": "<rootDir>/setupTests.js"
}
... I setup the // setupTests.js
module.exports = async () => {
var cesium = require('cesium');
process.$Cesium = cesium
}; and I have the first basic test for valid rendering: // App.test.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
ReactDOM.unmountComponentAtNode(div);
}); however I get the error in one of my view inside import Cesium from "cesium";
...
TypeError: Cannot read property 'Ion' of undefined
...
Cesium.Ion.defaultAccessToken = '...'; reporting Cesium as undefined when calling it's functions. I tried to use // jest.config.js
const { createJestConfig } = require("@craco/craco");
const cracoConfig = require("../craco.config.js");
const jestConfig = createJestConfig(cracoConfig);
module.exports = jestConfig; but create react app doesn't pick up this file so I cannot try this to verify if this would work. |
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. |
Do you want to request a feature or report a bug?
bug
What is the current behavior?
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal repository on GitHub that we can
npm install
andnpm test
.jest.config.json
my.test.js
./node_modules/.bin/jest -c jest.config.json my.test.js
What is the expected behavior?
PASS
Run Jest again with
--debug
and provide the full configuration it prints. Please mention your node and npm version and operating system.The text was updated successfully, but these errors were encountered: