Skip to content
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

in test mode is there a way for browser side to know in test? #3610

Closed
sdetweil opened this issue Oct 29, 2024 · 8 comments
Closed

in test mode is there a way for browser side to know in test? #3610

sdetweil opened this issue Oct 29, 2024 · 8 comments

Comments

@sdetweil
Copy link
Collaborator

in helper side one can test the process.env.JEST_WORKER_ID
not available in module side

I am helping user with new compliments parm test

@khassel
Copy link
Collaborator

khassel commented Oct 29, 2024

from tests/e2e/helpers/global-setup.js:

exports.getDocument = () => {
	return new Promise((resolve) => {
		const url = `http://${config.address || "localhost"}:${config.port || "8080"}`;
		jsdom.JSDOM.fromURL(url, { resources: "usable", runScripts: "dangerously" }).then((dom) => {
			dom.window.name = "jsdom";
			global.window = dom.window;
			// Following fixes `navigator is not defined` errors in e2e tests, found here
			// https://www.appsloveworld.com/reactjs/100/37/mocha-react-navigator-is-not-defined
			global.navigator = {
				useragent: "node.js"
			};
			dom.window.fetch = fetch;
			dom.window.onload = () => {
				global.document = dom.window.document;
				resolve();
			};
		});
	});
};

we are setting here dom.window.name = "jsdom"; so this could maybe used in the e2e-tests.

@sdetweil
Copy link
Collaborator Author

I meant the code running inside MM.. a module

to test the refreshable compliments remote file and have it locally we need to 'change' the configured url on the fly to it will pull a different file. but i have another way... if the url starts with tests and there is another parm (url2...)

@khassel
Copy link
Collaborator

khassel commented Oct 29, 2024

I meant the code running inside MM.. a module

and I meant you maybe can use

if (global.window.name === "jsdom")` {
  console.log("running in test");
}

@sdetweil
Copy link
Collaborator Author

sdetweil commented Nov 1, 2024

ok, thx

@sdetweil sdetweil closed this as completed Nov 1, 2024
@sdetweil
Copy link
Collaborator Author

sdetweil commented Nov 8, 2024

if (global.window.name === "jsdom")` {
  console.log("running in test");
}

this didn't work... global not defined
inside the module.register for compliments
the /* global global */
didn't work either (as it did for Cron loaded via getScripts)

@sdetweil sdetweil reopened this Nov 8, 2024
@khassel
Copy link
Collaborator

khassel commented Nov 8, 2024

In the browser environment, global variables are attached to the window object.

so for code running in browser omit the global:

if (window.name === "jsdom")` {
  console.log("running in test");
}

@sdetweil
Copy link
Collaborator Author

sdetweil commented Nov 9, 2024

hm.. doesn't fail, but doesn't work

		// in test mode only
		if (window.name === "jsdom") {
			// check for (undocumented) remote file2 to change test new load
			if(this.config.remoteFile2!== null && this.config.remoteFileRefreshInterval!==null){
	 			 console.log("running in test");
	 			 this.config.remoteFile=this.config.remoteFile2
			}
		}

for changing the remote file to prove that the cycle and fetch work , only wanted to fake a new file in test mode
if I comment out the if jsdom, then testcase works.
but fails with it enabled.

in test and in non-test window.name is empty
console.log("name='+window.name+"'")
results in name=''

this is in the getDom() callback

also empty outside the module register

console.log("window name='"+window.name+"'")
const compliments_test_mode=(window.name=='jsdom')?true:false

Module.register("compliments", {

@sdetweil sdetweil closed this as completed Nov 9, 2024
@sdetweil
Copy link
Collaborator Author

sdetweil commented Nov 9, 2024

code in pr #3630

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants