detrohutt / babel-plugin-import-graphql

Enables import syntax for .graphql and .gql files
https://www.npmjs.com/package/babel-plugin-import-graphql
MIT License
314 stars 22 forks source link

Local package imports not resolved in jest tests #71

Open qroac opened 5 years ago

qroac commented 5 years ago

In several gql files, I use imports from local packages, holding the files for the different modules of my application.
The statements look like this:
#import "@gqlapp/my-package/someFolder/File.graphql"

Is there any trick to get this path resolved correctly for jest?
Building and running the app with babel and webpack works great, but as soon as I run my jest tests, I get the following error:

server: FAIL ../../modules/moduel-A/server-ts/__tests__/A.test.ts
server:   ● Test suite failed to run
server:     ENOENT: no such file or directory, open '/builds/repo/project-app/modules/module-A/common/graphql/@gqlapp/module-B-common/graphql/MyDefinition.graphql'
server:       at Object.getSources (node_modules/babel-plugin-import-graphql/build/customImport.js:33:40)
server:       at node_modules/babel-plugin-import-graphql/build/requireGql.js:108:37
server:           at Array.forEach (<anonymous>)
server:       at Object.processFragments (node_modules/babel-plugin-import-graphql/build/requireGql.js:107:60)
server:       at processDoc (node_modules/babel-plugin-import-graphql/build/requireGql.js:157:7)
server:       at requireGql (node_modules/babel-plugin-import-graphql/build/requireGql.js:57:13)
server:       at PluginPass.exit (node_modules/babel-plugin-import-graphql/build/index.js:78:53)
server:       at newFn (node_modules/@babel/traverse/lib/visitors.js:193:21)
server:       at NodePath._call (node_modules/@babel/traverse/lib/path/context.js:53:20)
server:       at NodePath.call (node_modules/@babel/traverse/lib/path/context.js:40:17)
server:       at NodePath.visit (node_modules/@babel/traverse/lib/path/context.js:97:8)
server:       at TraversalContext.visitQueue (node_modules/@babel/traverse/lib/context.js:118:16)
server:       at TraversalContext.visitMultiple (node_modules/@babel/traverse/lib/context.js:85:17)
server:       at TraversalContext.visit (node_modules/@babel/traverse/lib/context.js:144:19)
server:       at Function.traverse.node (node_modules/@babel/traverse/lib/index.js:94:17)
server:       at NodePath.visit (node_modules/@babel/traverse/lib/path/context.js:95:18)
server:       at TraversalContext.visitQueue (node_modules/@babel/traverse/lib/context.js:118:16)
server:       at TraversalContext.visitSingle (node_modules/@babel/traverse/lib/context.js:90:19)
server:       at TraversalContext.visit (node_modules/@babel/traverse/lib/context.js:146:19)
server:       at Function.traverse.node (node_modules/@babel/traverse/lib/index.js:94:17)
MLKiiwy commented 4 years ago

I confirm this issue, the moduleNameMapper configuration of jest is not applied to #import commands.

MLKiiwy commented 4 years ago

A workaround is to add another plugin to babel: https://www.npmjs.com/package/babel-plugin-module-name-mapper