Coder Social home page Coder Social logo

patrickisgreat / testcafe-graphql-mock Goto Github PK

View Code? Open in Web Editor NEW

This project forked from abhinaba-ghosh/testcafe-graphql-mock

0.0 2.0 0.0 770 KB

custom plugin for testcafe to mock graphql server

Home Page: https://www.npmjs.com/package/testcafe-graphql-mock

License: MIT License

JavaScript 54.80% TypeScript 23.81% HTML 21.39%

testcafe-graphql-mock's Introduction

GraphQL Mocker logo

testcafe-graphql-mock

Build Status NPM release npm

simple testcafe commands for executing a mocked GraphQL server using only the client.

Installation

npm i -D testcafe-graphql-mock

API available

interface MockGraphQLOptions {
  schema: string | string[] | IntrospectionQuery;
  mock: IMocks;
  delay?: number;
}

mockGraphQL(options: MockGraphQLOptions, req, res);

Basic Usage

import { mockGraphQL } from 'testcafe-graphql-mock';

// define the schema
const schema = `
type Person {
  firstname: String!
  surname: String!
}

type Query {
  people: [Person]
}
`;

// define the mock
const mock = {
  Query: () => ({
    people: () => [
      {
        firstname: 'Lee',
        surname: 'Byron',
      },
    ],
  }),
};

// create traditional testcafe request mock
const requestMock = RequestMock()
  .onRequestTo({ url: 'http://localhost:3000/graphql', method: 'POST' })
  .respond(async (req, res) => {
    await mockGraphQL(
      {
        schema,
        mock,
      },
      req,
      res
    );
  });

// now call the testcafe request mock in fixures as request hooks
fixture(`GraphQL Mock test`)
  .page('http://localhost:3000/')
  .requestHooks(requestMock);

test('test graphql mock data', async (t) => {
  await t.click(Selector('button'));
  await expect(Selector('div')).contains('Lee Byron');
});

Read schema from .graphql file

You need to use graphQLSchemaFromFile method from the library.

import { graphQLSchemaFromFile } from 'testcafe-graphql-mock';

// use the graphql schema reader method in your request mocks
const requestMock = RequestMock()
  .onRequestTo({ url: 'http://localhost:3000/graphql', method: 'POST' })
  .respond(async (req, res) => {
    await mockGraphQL(
      {
        schema: graphQLSchemaFromFile(
          `${process.cwd()}/test/test-schema.graphql`
        ),
        mock,
      },
      req,
      res
    );
  });

Delay the GraphQL mocked response

use the delay (in milliseconds) parameter in mockGraphQL({}) options

const requestMock = RequestMock()
  .onRequestTo({ url: 'http://localhost:3000/graphql', method: 'POST' })
  .respond(async (req, res) => {
    await mockGraphQL(
      {
        schema,
        mock,
        delay: 5000,
      },
      req,
      res
    );
  });

License

MIT

Tell me your issues

you can raise any issue here

Contribution

Any pull request is welcome.

If this plugin helps you in your automation journey, choose to Sponsor

If it works for you , give a Star! ⭐

- Copyright © 2020- Abhinaba Ghosh

testcafe-graphql-mock's People

Contributors

abhinaba-ghosh avatar dependabot[bot] avatar patrickisgreat avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.