Reading fixture is throwing error TypeError: Converting circular structure to JSON

Wai Yan Hein

I am building a web application using React JS. I am writing integration tests using Cypress for my application. In my tests, I am reading JSON data from fixture files. When I read fixture file, it is throwing the following error.

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Window'
    --- property 'window' closes the circle

This is my code.

describe('Login', () => {
    it('renders login form render the form controls', () => {

    it('redirects to dashboard page when login is successful', () => {
        // TODO: use fixture
        // TODO: give alias
        // TODO: mock me endpoint
        const accessToken = 'fake access token'
        const loginResponseJson = cy.fixture('superadmin-login.json');

    })
})

This is my superadmin-login.json fixture file.

{
  "accessToken": "fake access token",
  "token": {
    "id": "c09dd33fbdcbf780ddb4d784002f1b4c8413cc298f2465f5f3e3c3481b2aa7f586d14f04af163a1c",
    "user_id": 31,
    "client_id": 1,
    "name": "User Access Token",
    "scopes": [],
    "revoked": false,
    "created_at": "2022-05-24 20:39:48",
    "updated_at": "2022-05-24 20:39:48",
    "expires_at": "2025-05-24T20:39:48.000000Z"
  }
}

What is wrong with my code and how can I fix it?

Fody

Reading a fixture file is an asynchronous operation, so this should work for you

cy.fixture('superadmin-login.json').then(loginResponseJson => {
  const token = loginResponseJson.token;
  ...
})

Note, Cypress has it's own type of a-synchronicity (command-queue model), so you can't do this either

// won't work
const loginResponseJson = await cy.fixture('superadmin-login.json')

You could require it at the top of the spec if you like to minimise .then() callbacks.

const loginResponseJson = require('./cypress/fixtures/superadmin-login.json')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TypeError: Converting circular structure to JSON - find error in json

[Vue warn]: Error in render: "TypeError: Converting circular structure to JSON

Express Middleware: ERROR: TypeError: Converting circular structure to JSON

Chrome sendrequest error: TypeError: Converting circular structure to JSON

ERROR TypeError: Converting circular structure to JSON --> starting at object with constructor 'FormGroup'

Error: Converting circular structure to JSON

Sequelize: TypeError: Converting circular structure to JSON

GraphCool TypeError: Converting circular structure to JSON

"TypeError: Converting circular structure to JSON" with GET request

"TypeError: Converting circular structure to JSON" with GET request

Elliptic : TypeError : Converting circular structure to JSON

Axios TypeError: Converting circular structure to JSON

Angular - TypeError: Converting circular structure to JSON

TypeError: Converting circular structure to JSON in nodejs

avoiding TypeError: Converting circular structure to JSON

Angular JS $scope.$apply giving interpolation error "TypeError: Converting circular structure to JSON"

redux-persist/createPersistoid: error serializing state TypeError: Converting circular structure to JSON

TypeError converting circular structure to json at JSON.stringify (<anonymous>)

how to solve (Uncaught TypeError: Converting circular structure to JSON)

Moving code to an exported function results in TypeError: Converting circular structure to JSON

TypeError: Converting circular structure to JSON in retrieving html string from mongodb

Firestore references create a "TypeError: Converting circular structure to JSON"

TypeError: Converting circular structure to JSON property 'name' closes the circle

TypeError: Converting circular structure to JSON when trying to POST request

TypeError: Converting circular structure to JSON with StorybookJS on React Native

Doing a proper MongoDB Query (TypeError: Converting circular structure to JSON)

VM1374:32 Uncaught TypeError: Converting circular structure to JSON(…)

Amazon Cognito confirmPassword fails with (TypeError: Converting circular structure to JSON)

TypeError: Converting circular structure to JSON --> starting at object with constructor 'ClientRequest'