I'm writing some cypress tests for a library I'm developing, It is meant to be injected as <script src="cdn_url"></script>
and I want to test some of its features, so I'm testing the modules, but I'm having problems with getting the window object from my module.
I have a module that does:
function myFunction() {
console.log(window.data)
}
so I created a fake index.html that will work as my test page.
In my test I do:
const data = require('../fixtures/data'); -- a json with some data
import { myFunction } from '../../src/index';
describe('My function test', () => {
beforeEach(() => {
Cypress.Cookies.debug(true);
cy.visit('../cypress/fixtures/index.html');
cy.clearCookies();
})
it('My test', () => {
cy.on('window:before:load', (win) => {
Object.defineProperty(win, 'data', data)
})
myFunction;
expect(true).to.equal(true)
})
})
When I execute the test myFunction gets executed but window.data is undefined. I get that cypress runs in a different context, but I don't know how to make myFunction run in the same context and being able to have access to window.data
I also tried adding <script> window.data = { value: myValue } </script>
to my index.html test page but didn't work, so I understand that myFunction is running in a different context.
Can you please help me on how to make that module work in the same context as my test suite?
Aucun commentaire:
Enregistrer un commentaire