在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):tgriesser/cypress-graphql-mock开源软件地址(OpenSource Url):https://github.com/tgriesser/cypress-graphql-mock开源编程语言(OpenSource Language):TypeScript 62.9%开源软件介绍(OpenSource Introduction):cypress-graphql-mockAdds commands for executing a mocked GraphQL server using only the client Installation
in Cypress' import "cypress-graphql-mock"; InstructionsAdds The const schema = fs.readFileSync("../../app-schema.graphql", "utf8");
// alternatively, using a dumped introspection query:
// const schema = require('../../dumped-schema.json')
beforeEach(() => {
cy.server();
cy.mockGraphql({ schema });
}); Actually it is not possible to use module.exports = (on, config) => {
on("task", {
getSchema() {
return fs.readFileSync(
path.resolve(__dirname, "../../app-schema.graphql"),
"utf8"
);
}
});
}; And then in the code you will be able to beforeEach(() => {
cy.task("getSchema").then(schema => {
cy.mockGraphql({
schema,
operations: { ... }
});
});
}); By default, it will use the beforeEach(() => {
cy.server();
cy.mockGraphql({
schema,
endpoint: "/gql"
});
}); It takes an "operations" object, representing the named operations of the GraphQL server. This is combined with the "mocks" option, to modify the output behavior per test. The For example, if we have a query called "UserQuery" and wanted to explicitly force a state where a viewer is null (logged out), it would look something like: .mockGraphqlOps({
operations: {
UserQuery: {
viewer: null
}
}
}) ExamplesReal application exampleSimple mutationJust return mutation result. Make sure that mostly always you will need to duplicate mutation name 1st time as operation key, and 2nd as return data object key. cy.server();
cy.mockGraphql({ schema });
cy.mockGraphqlOps({
operations: {
userNameChange: {
userNameChange: {
name: "New user name"
}
}
}
}); It is also possible to pass a function to simulate dynamic resolver. cy.server();
cy.mockGraphql({ schema });
cy.mockGraphqlOps({
operations: {
userNameChange: variables => ({
userNameChange: {
viewer: {
name: variables.name
}
}
})
}
}); DelayIn order to test asynchronous behavior sometimes you will need to delay your graphql request. This can be done with a help of cy.mockGraphqlOps({
operations: {
delay: 1000, // 1 second
userNameChange: new GraphQLError("Your message goes here")
}
}); Error handlingimport { GraphQLError } from "graphql";
cy.mockGraphqlOps({
delay: 1000, // wait 1 second
operations: {
userNameChange: new GraphQLError("Your message goes here")
}
}); It is also possible to throw error from the function. Just cy.mockGraphqlOps({
operations: {
userNameChange: (variables) => {
if (!variables.name) {
throw new GraphQLError("Name is required")
}
}
}
}); LicenseMIT |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论