Tests and Commands do not have access to common JS modules
See original GitHub issueCurrent behavior:
It is not possible to share Modules between custom commands and tests.
Desired behavior:
Variables set into modules from custom commands should be able to be read from tests.
Test code to reproduce
Is there a way to share JS modules between custom commands and Cypress tests ? Right now, when I try to share a variable defined in a JS module, set the value of the variable in the custom command and try to read it in a test, I get undefined ? For example,
jsmodule.js
export const A = {
a: null,
get a() {
return this.a;
},
set a(a) {
this.a = a;
},
};
commands.js
import {A} from './jsmodule.js';
Cypress.Commands.add('testing', () => {
A.a = 5;
});
Spec file
import {A} from './jsmodule.js'
beforeEach(function() {
cy.testing();
});
it('print',() => {
console.log(A.a);//undefined !!!
});
Versions
3.8.0 Electron
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (3 by maintainers)
Top Results From Across the Web
CommonJS modules | Node.js v19.3.0 Documentation
CommonJS modules are the original way to package JavaScript code for Node.js. Node.js also supports the ECMAScript modules standard used by browsers and ......
Read more >How the module system, CommonJS & require works
In this article we will learn about how the Node.js module system works, how you can organize your modules and what does the...
Read more >Mocking ES and CommonJS modules with jest.mock() - Medium
Using Mocks gives us control over external dependencies, for the duration of the test we can replace code we do not have control...
Read more >Error [ERR_REQUIRE_ESM]: require() of ES Module not ...
The node-fetch latest version doesn't use the require() syntax to import the package. You need to go to your package.json and type
Read more >CommonJS vs ES Modules in Node.js - A Detailed Comparison
Second, CommonJS modules can only be accessed from within the context of a Node.js application. CommonJS modules do not have exports or ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here is what is going on, and I do not consider this a bug, just the way bundling happens.
You can find the supporting code in https://github.com/cypress-io/cypress-test-tiny/commit/08172373ff30d0e78544cc240c0a88155ecf035f
Anything bundled from the support file gets its own bundle. Anything imported from the spec file gets into second bundle. These two bundles are served for the spec
If you open the
support
bundle you will see theUsername
object from “globals”If you open the spec bundle you will ALSO see the
Username
object` from “globals”Each bundle has its own copy of “Username” imported from the file, changing a property in one does not change it in the other.
If you want to share data and object references between the support bundle nd the spec bundle, do it through the
window
object.I’m not sure if I can write here. BTW… I’m trying to make a custom reporter with metadata added through custom methods called from tests. I saw the allure test report which use an Allure object shared between the test and the reporter but I can’t reproduce his behaviour. I also tried to create a plugin according to the post opening this thread but it doesn’t work. It seems that the global object of the reporter, the plugin and test execution are different so it’s not possibile to share data. Is this correct? There is a way to achieve data sharing between these items? Thanks in advance