-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent Cypress from resolving stubs with cy.route #4163
Comments
Hey @toddgallimore, controlling responses in this manner is something we're planning on adding as part of #687. |
Excellent. It's on the roadmap. Keep up the good work! Is there anything you can suggest for the time being? I found a way by stubbing (I'll leave this here for other people's reference, or for someone smarter than me to improve on this and get closer to the requirements). export default function deferrer() {
const deferred = {};
deferred.promise = new Promise((resolve, reject) => {
deferred.resolve = resolve;
deferred.reject = reject;
});
return deferred;
}
export default function FetchStub(cy, windw) {
this.deferred = deferrer();
this.stub = cy.stub(windw, 'fetch')
.returns(this.deferred.promise);
this.resolve = function (response) {
this.deferred.resolve({
text: () => JSON.stringify(response),
json: () => response,
ok: true
});
};
this.reject = function (error) {
this.deferred.reject({
text: () => JSON.stringify(error),
json: () => error,
ok: false
});
};
} which you can then use like this: let stubFetch;
beforeEach(() => {
cy.visit(url, {
onBeforeLoad(windw) {
stubFetch = new FetchStub(cy, windw);
}
});
});
it('should do something', () => {
stubFetch.resolve({ ...someResponse });
expect(something).toEqual(somethingElse);
});
it('should do else', () => {
stubFetch.reject({ ...someError });
expect(something).toEqual(somethingElse);
}); |
I would suggest an api such as it('should show loader while loading then show content', () => {
// Stub out the normal route with a Promise Completion Source
cy.route("GET", "/some/url").defer().as("someUrlPromise")
// or
cy.routeDeferred("GET", "/some/url").as("someUrlPromise"); // could be just cy.route
// Check loading state
cy.get("loader").should("exist");
// Complete request
// this could be cy.getRoute(), or just cy.route("@xxx")
cy.get("@someUrlPromise").resolve({
status: xxx,
headers: {},
body: "@someUrlPromiseResponseBodyFixture"
})
// Check Loaded State
cy.get("loader").should("not.exist");
}); Anyhow, just my 2 cents. It's a pain to need to add random wait times (2-5 seconds) in order to have non flakey tests, as they quickly add up! |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
The features requested in this issue are now possible as part of
Please see the If you encounter any issues or unexpected behavior while using |
Feature Request
I'd like to be able to test loading states easier, by deciding myself when requests are resolved.
Current behaviour:
I can't currently find a way to prevent a
cy.route
from resolving. It seems closely related to this issue, although that is very old and has been closed with comments to say it's been included, but I can't find any docs for it.I have looked everywhere and also asked questions in the Gitter forum.
I'd like to be able to prevent
cy.route
from resolving, until I tell it, so I can test the loading the state.Desired behavior:
Something like this:
It does feel like Cypress should already be able to handle this (because it's awesome). But I can't find any docs for it and I'm struggling to find help elsewhere.
Versions
Cypress
^3.2.0
Node
10.15.3
Chrome
74
The text was updated successfully, but these errors were encountered: