question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tests and Commands do not have access to common JS modules

See original GitHub issue

Current 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:closed
  • Created 3 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
bahmutovcommented, May 5, 2020

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

Screen Shot 2020-05-05 at 12 16 30 PM

If you open the support bundle you will see the Username object from “globals”

Screen Shot 2020-05-05 at 12 16 44 PM

If you open the spec bundle you will ALSO see the Username object` from “globals”

Screen Shot 2020-05-05 at 12 17 01 PM

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.

// support file
import { Username } from './globals'

window.Username = Username

Cypress.Commands.add('printName', () => {
  console.log('command globals name: %s', Username.name)
})
// spec file
const Username = window.Username

describe('globals', () => {
  it('shows username', () => {
    console.log('in spec username is "%s"', Username.name)
    console.log('now changed it to "foo"')
    Username.name = 'foo'
    console.log('in spec username is "%s"', Username.name)

    cy.printName()
  })
})
Screen Shot 2020-05-05 at 12 23 51 PM
0reactions
gorrucommented, Nov 18, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found