Jest transformer for .gql imports
npm install jest-graphql-transformerA Jest transformer that allows GraphQL queries in separate files, similar to the graphql-tag loader for Webpack, but adapted for Jest, which does not support Webpack loaders. This package provides the same transformation within Jest, enabling seamless GraphQL query imports.
---
Install the package using npm or yarn:
``sh`
npm install --save-dev jest-graphql-transformer
or
`sh`
yarn add --dev jest-graphql-transformer
---
#### 📌 In package.json
Add the following to your Jest configuration:
`json`
"jest": {
"transform": {
"\\.(gql|graphql)$": "jest-graphql-transformer",
".*": "babel-jest"
}
}
#### 📌 Or in jest.config.js / jest.config.ts
`javascript`
export default {
transform: {
'\\.(gql|graphql)$': 'jest-graphql-transformer',
},
};
Once you define the transform property, Jest's default transformations will be overridden.
If you need Babel for other files, make sure to include it:
`javascript``
export default {
transform: {
'\\.(gql|graphql)$': 'jest-graphql-transformer',
'.*': 'babel-jest', // Keep Babel for other files
},
};
---
MIT License © 2024 hamidyfine