Hi there, I'm new to Cypress and facing a challenge that I hope you can help me with.
In my test file, I have the following code:
cy.SetupClassificationsStubFixture(1);
This code refers to a custom command that I've created:
Cypress.Commands.add("SetupClassificationsStubFixture", (amount) => {
cy.fixture('ClassificationsStub.json').then(classificationsStub => {
const slicedClassifications = classificationsStub.slice(0, amount)
cy.intercept('GET', '**/api/classifications', slicedClassifications)
}).as('classifications')
cy.fixture('ClassificationsStub.json').then(classificationsStub => {
const slicedClassifications = classificationsStub.slice(0, amount)
cy.intercept('GET', '**/api/classifications/*', slicedClassifications)
}).as('classifications')
});
With this code, I am intercepting 2 API requests:
- ** /api/classifications
- ** /api/classifications/ *
The issue is that both responses are returning with brackets '[]'. I tried creating a second fixture file without brackets, but the slice function did not work as expected.
So, what I need is for the second intercepted response to be like this:
{
"id": "9d4a9c14-ef37-4a64-a1eb-63ab45cdf530",
"name": "CypressTest",
"userRole": "Editor",
"childClassifications": []
}
But currently, it is returning like this:
[
{
"id": "9d4a9c14-ef37-4a64-a1eb-63ab45cdf530",
"name": "CypressTest",
"userRole": "Editor",
"childClassifications": []
}
]
How can I get rid of the brackets in the array? Thank you!
UPDATE: I am attempting the following solution now:
Cypress.Commands.add("SetupClassificationsStubFixture", (amount) => {
cy.fixture('ClassificationsStub.json').then(classificationsStub => {
const slicedClassifications = classificationsStub.slice(0, amount)
cy.intercept('GET', '**/api/classifications', slicedClassifications)
}).as('classifications')
cy.fixture('ClassificationsStub.json').then(classificationsStub => {
const slicedClassifications = classificationsStub.slice(0, amount)
const arr2 = slicedClassifications
const obj5 = Object.fromEntries(arr2)
cy.intercept('GET', '**/api/classifications/*', obj5)
}).as('classification')
});
However, this is resulting in an empty .json file. The response is not just '{}'