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.

Cypress fixture returns an object that somehow is not an instanceof Object

See original GitHub issue

Current behavior

cy.fixture("example.json").then(fixture => console.log(fixture instanceof Object))

Logs false

console.log(require("../../fixtures/example.json") instanceof Object);

Logs true

This is a problem because we use Immer to mutate state but it doesn’t work on the fixture data, breaking our app in Cypress tests.

Desired behavior

cy.fixture("example.json").then(fixture => console.log(fixture instanceof Object))

Logs true

Test code to reproduce

https://github.com/jordyvandomselaar/Cypress-fixture-no-instanceof-object-bug

Versions

  1. “cypress”: “^6.5.0”,
  2. MacOS Catalina 10.15.6

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jennifer-shehanecommented, Feb 18, 2021
it('cy.fixture() - load a fixture', () => {
  cy.fixture("example.json").then((fixture) => {
    // logs false
    console.log("Cypress Fixture", fixture instanceof Object)
  })

  // logs true
  console.log("Require Fixture", require("../../fixtures/example.json") instanceof Object);
  
  // logs true
  console.log("Plain Object", {"key": "value"} instanceof Object);
})

The code for fixtures can be found here: https://github.com/cypress-io/cypress/blob/develop/packages/server/lib/fixture.js#L39:L39

For now I would recommend requiring the fixtures as you showed where it will be instanceof Object.

0reactions
bahmutovcommented, Apr 1, 2021

@jordyvandomselaar can I ask how you are using the object loaded from the fixture? Do you pass it to the application somehow? In the repo you provided you just have the spec code like

context('Files', () => {
  it('cy.fixture() - load a fixture', () => {
      cy.fixture("example.json").then(fixture => console.log(fixture instanceof Object))

      console.log(require("../../fixtures/example.json") instanceof Object);
      console.log({"key": "value"} instanceof Object);
  })
})

Which to me is fine, the above workarounds are enough. I would love to fix the application example if you can provide one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

fixture | Cypress Documentation
cy.fixture() yields the contents of the file. Formatting is determined by its file extension. The yielded subject is not updated if the contents...
Read more >
You provided an invalid object where a stream was expected ...
In my case I forgot to return an Observable in a switchMap. This caused that no Observable was received in the next RXJS...
Read more >
Angular / Angular guidelines and best practices / Testing
Fixture is created for the host component and fixture.componentInstance will not be an instance of the component you want to test, it will...
Read more >
Compare The Response To A Fixture - Gleb Bahmutov
Imagine a situation: you need to make an HTTP request from your Cypress test and compare the result to a JSON object stored...
Read more >
cli/types/cypress.d.ts - Fossies
1 /* eslint-disable @typescript-eslint/no-unused-vars */ 2 ... 668 /** 669 * Returns a boolean indicating whether an object is a DOM object.
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