cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.72k stars 3.16k forks source link

Fixtures not working in beforeAll in 5.0.0 #8422

Closed AwarePL closed 4 years ago

AwarePL commented 4 years ago

Current behavior:

Using cy.fixture same as on code sample provided in docs/api i get following error: image

Test code to reproduce

const landingPageView = require("./pageObjects/landingPagePageObjects");
const landingPage = require("./selectors/landingPageSelectors");

describe("Landing Page Smoke Test", () => {
  beforeAll(() => {
    cy.fixture("consts")
    .then((consts) => {
      this.consts = consts
    })

    cy.visit("");
  });

  it("Check first section content, expect section title, subtitle, start page button", () => {
    landingPageView.checkSectionTitle(landingPage.sectionHero, this.consts.static.landingPage.sectionHeroH1[0]);
    landingPageView.checkSectionTitle(landingPage.sectionHero, this.consts.static.landingPage.sectionHeroH1[1]);
    landingPageView.checkSectionSubtitle(landingPage.sectionHero, this.consts.static.landingPage.sectionHeroP);
    landingPageView.checkSectionStartWebsiteButton(landingPage.sectionHero, this.consts.static.landingPage.startPageButton);
    landingPageView.checkSectionImages(landingPage.sectionHero, this.consts.static.landingPage.sectionHeroMobileAlt, this.consts.static.landingPage.sectionHeroTabletAlt, this.consts.static.landingPage.sectionHeroDesktopAlt);
  });
});

Versions

Chrome 84 Windows 10 Cypress 5.0.0

jennifer-shehane commented 4 years ago

If you want to share context across tests, you'll want to use the standard function definition, instead of arrow functions: https://on.cypress.io/variables-and-aliases#Avoiding-the-use-of-this

before(() => {
  this.consts = 'foo'
})

// function definition, not arrow function
it("test", function () {
  expect(this.consts).to.be.ok
})

I feel like this was likely a bug fix from the previous version. 🤔

AwarePL commented 4 years ago

sure, in my other project currently at 4.10 arrow function still works. You are saying it was faulty all along in prev versions?

example:

describe('sample End to end test suite', () => {
  before(() => {
    cy.fixture('testsParms').then((parms) => {
      this.parms = parms
    })
  })

  beforeEach(() => {
    cy.visit('')
  })

  it('test', () => {
    step1()
    step2(this.parms.fixture.node[0])
    })
kuceb commented 4 years ago

I'm guessing this is related to the 5.0 change making the default preprocessor webpack instead of browserify, which handles this differently. (in the working code, I think this is the global window). So yes, that code working in previous versions wasn't intended

AwarePL commented 4 years ago

OK, thanks for clarification. ;)