Working with Jest
Closed this issue · 4 comments
Hello thanks for this work. I am trying to to run tests with Jest but they are failing when i have an import statement inside my schema to import another graphql type def.
Any suggestions?
Thanks
Example:
Project root: /Users/<user>/Projects/graphql-data-sources/data-sources
.
In /Users/<user>/Projects/graphql-data-sources/data-sources/data-source-1/schema1.graphql
, at the beginning of our graphql schema we have:
# import { SomeType } from 'data-sources/data-source-2/schema2.graphql'
All good when we run our graphql server: the server runs from /Users/<user>/Projects/graphql-data-sources
and the import is resolved correctly as /Users/<user>/Projects/graphql-data-sources/data-sources/data-source-2/schema2.graphql
.
Issue when running our Jest tests:
FAIL data-sources/data-source-1/__tests__/schema1.test.ts
● Data Source 1 schema › encountered a declaration exception
ENOENT: no such file or directory, open '/Users/<user>/Projects/graphql-data-sources/data-sources/data-source-1/data-sources/data-source-2/schema2.graphql'
5 |
6 | describe('Data Source 1 schema', () => {
> 7 | const typeDefs = importSchema(
8 | resolve(__dirname, '..', 'schema1.graphql'),
9 | );
10 |
The issue happens because Jest tests run from /Users/<user>/Projects/graphql-data-sources/data-sources/data-source-1
and the import resolves to /Users/<user>/Projects/graphql-data-sources/data-sources/data-source-1/data-sources/data-source-2/schema2.graphql
which doesn't exist.
The Jest tests issue is fixed if we change the import to # import { SomeType } from '../data-source-2/schema2.graphql'
. In this case in fact the path that Jest tries to go to is /Users/<user>/Projects/graphql-data-sources/data-sources/data-source-1/../data-source-2/schema2.graphql
.
However, when we run the server the import is not working correctly anymore since now it tries to resolve to /Users/<user>/Projects/graphql-data-sources/data-sources/../data-source-2/schema2.graphql
which doesn't exist.
This seems related to the fact that the tests run a different location compared to the location from which the server runs and we don't seem to find a way to have both server and tests working with the same import. Any suggestions?
Thank you
You can try this transformer : https://github.com/kMeillet/jest-graphql-import/blob/master/transformer.js
Copy "transformer.js" into your project and update Jest configuration in "package.json" :
"jest": {
"transform": {
"\\.(gql|graphql)$": "./transformer.js",
".*": "babel-jest"
}
Thank you @kMeillet. Unfortunately the transformer didn't help.
However, jest is able to correctly resolve the import in our graphql schema if the path in the import makes sense from the test folder, so it looks like importSchema is working correctly.
Example: If this is the import
# import { SomeType } from '../data-source-2/schema2.graphql'
then the tests work correctly.
Our issue is that the relative path that makes the tests work ('../data-source-2/schema2.graphql'
) messes up the server which doesn't work anymore since it cannot find the relative path from its starting location.
I cannot make both jest and server happy!
Note that I'm doing this in my tests to import the schema:
const typeDefs = importSchema( resolve(__dirname, '..', 'schema2.graphql'), );
I was able to make this work by replacing
const typeDefs = importSchema( resolve(__dirname, '..', 'schema2.graphql'), );
with
const typeDefs = require('../schema2.graphql');
in my jest test and using @kMeillet's transformer.js
.
Thank you @kMeillet! 🍻