• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

tgriesser/cypress-graphql-mock: Adds commands for executing a mocked GraphQL ser ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

tgriesser/cypress-graphql-mock

开源软件地址(OpenSource Url):

https://github.com/tgriesser/cypress-graphql-mock

开源编程语言(OpenSource Language):

TypeScript 62.9%

开源软件介绍(OpenSource Introduction):

cypress-graphql-mock

Adds commands for executing a mocked GraphQL server using only the client

Installation

npm install cypress-graphql-mock

in Cypress' commands.js add:

import "cypress-graphql-mock";

Instructions

Adds .mockGraphql() and .mockGraphqlOps() methods to the cypress chain.

The .mockGraphql should be called in the Cypress before or beforeEach block config to setup the server. This method takes a schema, either in the form of one or more SDL files, or as the JSON result of an introspection query.

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 fs.readFileSync right in the cypress tests. So here you can create custom command. Add this to your cypress/plugins/index.js.

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 /graphql endpoint, but this can be changed depending on the expected server implementation.

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 .mockGraphqlOps() allows you to configure the mock responses at a more granular level

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
    }
  }
})

Examples

Real application example

Simple mutation

Just 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
        }
      }
    })
  }
});

Delay

In order to test asynchronous behavior sometimes you will need to delay your graphql request. This can be done with a help of delay option.

cy.mockGraphqlOps({
  operations: {
    delay: 1000, // 1 second
    userNameChange: new GraphQLError("Your message goes here")
  }
});

Error handling

import { 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 return or throw a GraphQLError.

cy.mockGraphqlOps({
  operations: {
    userNameChange: (variables) => {
      if (!variables.name) {
        throw new GraphQLError("Name is required")
      }
    }
  }
});

License

MIT




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap