MagicMirrorOrg / MagicMirror

MagicMirror² is an open source modular smart mirror platform. With a growing list of installable modules, the MagicMirror² allows you to convert your hallway or bathroom mirror into your personal assistant.
http://magicmirror.builders
MIT License
19.84k stars 4.21k forks source link

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

Closed sdetweil closed 2 weeks ago

sdetweil commented 3 weeks ago

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 commented 3 weeks ago

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 commented 3 weeks ago

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 commented 3 weeks ago

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 commented 3 weeks ago

ok, thx

sdetweil commented 2 weeks ago
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)

khassel commented 2 weeks ago

In the browser environment, global variables are attached to thewindowobject.

so for code running in browser omit the global:

if (window.name === "jsdom")` {
  console.log("running in test");
}
sdetweil commented 2 weeks ago

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 commented 2 weeks ago

code in pr #3630