-
Notifications
You must be signed in to change notification settings - Fork 4
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
Error import Specmatic in Cypress tests #139
Comments
@tkrisztian95 Thank for sharing the details. This seems like an interesting use case. I am wondering why Cypress has to intercept the request and then pass it on to the Specmatic stub server. Can we not have the system under test directly talk to the Specmatic stub server? Can you please elaborate the test setup in below format, as per the component test anatomy? Can you please update the question sections highlighted as question marks?
This will help me first understand the context before I answer your quiestion. |
Hi @harikrishnan83! This is basically a use case with Cypress end-to-end tests, where we are rendering the UI in a headless browser and there is no actual backend running on the host. In tests we use For example: describe("Home page", () => {
beforeEach(() => {
cy.viewport("macbook-13");
cy.visit("localhost:5179");
let json = {
links: {},
data: {
foo: "bar"
}
};
// we set the response to be the json fixture
cy.intercept("api/v1/foo", {
statusCode: 200,
body: json
});
... See also docs about Cypress stub responses. So, instead of the static stubs baked in the tests, Specmatic could be there and provide the transient expectations for a functioning FE that Cypress can click and navigate through.
I think it solves the downside mentioned in the Cypress documentation itself:
But, the original problem is with the import, I cannot import the Specmatic NPM in the test files because of the error. So from the test file scope I don't have programmatic control over the Specmatic stub server. I need to start the stub server before all test manually, etc. And to answer your questions:
I don't know, the existing tests that we have use this approach. It might be configurable to route all requests to the http stub directly. Thank you for helping! |
I think I misunderstand the way how to work with Cypress and using the So, the additional For example this worked for me: cy.intercept("/api/v1/foo/list", (req) => {
// Replacing the server url to connect to the Specmatic Stub server instead
req.url = req.url.replace("localhost:5173", "localhost:9000");
// Adding the missing Authorization header to the outgoing request
req.headers['Authorization'] = 'Bearer token'
// Let send the request to the destination server (Specmatic)
req.reply();
}); Also for stub out responses in tests I put canned response files in a PS: Still not sure, if you want to import Specmatic in the testfile for dynamic stubbing using the |
Describe the bug
I'm trying to import Specmatic in a Cypress test, like
and I'm getting the following error while running the tests by
npx cypress run --browser electron
:Desktop:
Additional context
I'm trying to achieve that from a Cypress test, using the
cy.intercept()
andcy.reply()
functions, the tests are backed with the Specmatic stub and it returns the example/transient stubs always verified against the API specification.For example:
Should this work?
The text was updated successfully, but these errors were encountered: