graphql-codegen-typescript-operations-tester

graphql-code-generator plugin to generate test functions on operations

Usage no npm install needed!

<script type="module">
  import graphqlCodegenTypescriptOperationsTester from 'https://cdn.skypack.dev/graphql-codegen-typescript-operations-tester';
</script>

README

graphql-codegen-typescript-operations-tester

Version Test workflow Release workflow

Install

npm i -D @graphql-codegen/typescript @graphql-codegen/typescript-operations graphql-codegen-typescript-operations-tester

Example

codegen.yml

schema: ['./schema.graphql']

generated/tests.ts:
  documents: './documents.graphql'
  plugins:
    - typescript
    - typescript-operations
    - graphql-codegen-typescript-operations-tester
  config:
    prefix: test

schema.graphql

type Author {
  firstname: String
  lastname: String
  fullname: String
}

type Book {
  title: String
  author: Author
}

type Query {
  books: [Book]
}

documents.graphql

query getBooks($var1: String!) {
  books(var1: $var1) {
    title
    author {
      firstname
      lastname
      fullname
    }
  }
}

test.spec.ts

import { testGetBooksQuery } from './generated/tests.ts'
import { schema } from 'path/to/my/schema'

describe('Test something cool', () => {
  it('testGetBooksQuery should return something', async () => {
    const res = await testGetBooksQuery({ schema })
    expect(res.data?.books).toBeAwesome()
  })
})