nitrogql logonitrogql

@nitrogql/jest-transform reference

@nitrogql/jest-transform is a Jest transformer that lets you load .graphql files from your code during testing. This package is suited for projects that use Jest to test client-side code that uses GraphQL.

Example

jest.config.js

{
  "transform": {
    "^.+\.graphql$": ["@nitrogql/jest-transform", {
      "configFile":  path.resolve(__dirname, "nitrogql.config.js")
    }]
  }
}

options

options.configFile

Path to the configuration file.

options.additionalTransformer

Additional transformer to apply to the generated source code. Can be a string or an array of two elements: the transformer name and the transformer configuration. See below for more information.

options.additionalTransformerFilenameSuffix

Suffix to add to filename when passing code to the additional transformer. Defaults to ".js".

CommonJS Support

@nitrogql/jest-transform is only capable of transforming GraphQL files to ES modules. If you need CommonJS support, use the additionalTransformer option to apply another transformer that can convert ES modules to CommonJS.

Example setup with ts-jest:

{
  "transform": {
    "^.+.tsx?": ["ts-jest", { isolatedModules: true }],
    "^.+\.graphql$": ["@nitrogql/jest-transform", {
      "configFile":  path.resolve(__dirname, "nitrogql.config.yml"),

      // Use the additionalTransformer option to apply ts-jest to the output.
      "additionalTransformer": ["ts-jest", { isolatedModules: true }],

      // ts-jest expects .ts files, so we need to change the file extension
      // by applying the additionalTransformerFilenameSuffix option.
      "additionalTransformerFilenameSuffix": ".ts"
    }]
  },
}