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.

Doubt: load multiple fixtures once per Cypress execution

See original GitHub issue

I have 3 sites, and each site has 3 envs, each with their config.json and fixtures.json. However, part of the fixture is common, like routes which contains endpoints. Following the DRY pattern, I refactored the common bits to a routes.json.

Screenshot 2019-06-17 at 18 37 26

I basically want to load multiple fixtures…

  • avoiding “pyramid of doom of fixtures”, as in this example
  • if possible, once per Cypress execution (it’s a bit of a waste on a before()), unlike this example

Ideally I would be able to access each fixture file independently like:

cy.visit(routes.account.edit)
...
cy.get(selectorNotification).should("contain", keywords.dialog.success)

Given a file like:

// routes.json
{
  "account": {
    "edit": "/account/edit"
  },
  "users": {
    "list": "/users",
    "search": "/user/search"
  },
  ...
} 

I’ve tried a mix of cy.fixture, Cypress.config, fs.readFile, cy.readFile to no avail and now I’m too biased to think out of the box.

Please, can you advise me a solution? I feel like I’m over-engineering it.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
deventuallycommented, Apr 14, 2020

Another solution could be to collect fixtures in a Promise The paths parameter is an array of paths to fixtures

const fixtures = (paths) => Promise.all(paths.map(p => cy.fixture(p)))

Use like this:

fixtures(['path/1', 'path/2']).then(filesContents => {
  console.log(filesContents)
})
4reactions
dialexcommented, Jun 18, 2019

This works:

const configHelper = new ConfigHelper()
let routes, keywords, fixtures
before(() => {
  cy.fixture(configHelper.getRoutesPath()).then(c => {
    routes = c
  })
  cy.fixture(configHelper.getKeywordsPath()).then(c => {
    keywords = c
  })
  cy.fixture(configHelper.getFixturesPath()).then(c => {
    fixtures = c
  })
})

describe("Example component under test", function() {
  it("expects a certain behaviour", function() {
    expect(routes).to.be.not.undefined
    expect(keywords).to.be.not.undefined
    expect(fixtures).to.be.not.undefined
  })
})

But it’s annoying having to copy that whole block of boilerplate to every test file.

Read more comments on GitHub >

github_iconTop Results From Across the Web

fixture | Cypress Documentation
Loaded just once. Please keep in mind that fixture files are assumed to be unchanged during the test, and thus Cypress loads them...
Read more >
How to run the single test with different data set in parallel by ...
I just have the below spec file and It contains one It (Test case) and It will run multiple times based on the...
Read more >
Load Fixtures from Cypress Custom Commands
This blog post multiples ways to pick a random item from a fixture file, and then reuse that picked item in multiple Cypress...
Read more >
End-To-End Tutorial For Pytest Fixtures With Examples
Fixtures are a set of resources that have to be set up before and cleaned up once the Selenium test automation execution is...
Read more >
What I've Learned Using Cypress.io for the Past Three Weeks
First place the image you would like to upload into the fixture folder, and then you would load the image data with cy.fixture...
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 Reddit Thread

No results found

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