我在这里使用这个包:
npm i @iam4x/cypress-graphql-mock
将此行添加到“support/commands.js”
import "@iam4x/cypress-graphql-mock";
转到您的 graphiql 游乐场并下载您的架构
将任务命令添加到“plugins/index.js”(记住更改您之前下载的架构文件的路径)
module.exports = (on, config) => {
on("task", {
getSchema() {
return fs.readFileSync(
path.resolve(__dirname, "../../../schema.graphql"),
"utf8"
);
}
});
};
- 使用加载的模式编写测试
beforeEach(() => {
cy.server();
cy.task("getSchema").then(schema => {
cy.mockGraphql({
schema
});
});
});`
describe("Login Form", () => {
it("should redirect after login", () => {
cy.mockGraphqlOps({
operations: {
Login: {
login: {
jwt: "some-token",
user: {
id: "5d5a8e1e635a8b6694dd7cb0"
}
}
}
}
});
cy.visit("/login");
cy.getTestEl("email-input").type("Max Mustermann");
cy.getTestEl("password-input").type("passwort");
cy.getTestEl("submit").click();
cy.getTestEl("toolbar-title").should("exist");
});
})
- 访问原始存储库以获得进一步的解释,因为我发现它不那么令人困惑。您安装的包只是这个包的一个工作分支:
https ://github.com/tgriesser/cypress-graphql-mock