I would like to write reusable "before" section, but has failed to pass variable from main file (A.js) to imported code (HelloUtil.js). Thanks in advance for any suggestions given.
A.js
import * as UTIL from "./HelloUtil"
let variableFileA = "Hello"
// I hope the imported 'before' section is able to get 'variableFileA'
describe(`Do something`, () => {
it('A "It" section', () => {
cy.log("....")
});
})
HelloUtil.js
before('Reusable "before" 1', () => {
cy.log("lets begin....")
});
before('Reusable "before" 2', () => {
cy.log("print variable:"+variableFileA)
});
The result that I received:
No, this is not what's going to work.
You'd need to define
variableFileA
variable inHelloUtil.js
if you want to use the variable in there. Or you can pass the variable as an argument. But you are not doing either.What can work is this:
utils.js
test.js
If you want to pass some argument, you can do it like so:
utils.js
test.js
And the result from the test runner: