In my Cypress test, I have a field containing a date, and I want it to be dynamic to the test run date.
I tried to handle it this way:
export const interceptGetEntity = () => {
cy.intercept('GET', 'my/api/entities/123145',
(req) => {
cy.fixture('get-entity').then((entity: Entity) => {
const now = new Date();
const thisMonth = now.getMonth();
const thisYear = now.getFullYear();
entity.steps.forEach((step, index) => {
step.effectiveDate = formatDateString(new Date(thisYear, thisMonth + index, 1));
})
req.reply(entity);
});
}).as('getEntity');
};
But then I get this error:
Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.
The command that returned the promise was:
cy.wait()
The cy command you invoked inside the promise was:
cy.fixture()
Because Cypress commands are already promise-like, you don't need to wrap them or return your own promise.
Cypress will resolve your command with whatever the final Cypress command yields.
The reason this is an error instead of a warning is because Cypress internally queues commands serially whereas Promises execute as soon as they are invoked. Attempting to reconcile this would prevent Cypress from ever resolving.Learn more
I, obviously, do not want apply this logic to every test but want to make it in 1 common place. Is there a better way to manipulate my fixture data?
Found a simple synchronous workaround
Instead of:
I am just fetching the JSON from the file with a simple
require: