[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
With graphql cli plugin being enabled, when graphql e2e tests are ran, it couldn't recognize graphql fields and raise errors.
With graphql cli plugin being enabled, when graphql e2e tests are ran, it should recognize graphql fields and will not errors. It should behave like the fields are annotate with @Field()
Graphql cli plugin is useful in reducing boilerplate in defining graphql schema, but in tests it isn't working :(
Nest version: 7.0.8
For Tooling issues:
- Node version: 13.12.0
- Platform: Mac
Others:
With graphql cli plugin being enabled, when graphql e2e tests are ran, it couldn't recognize graphql fields and raise errors.
CLI plugins are designed to be used by the compiler as part of the build process. Jest framework, on the other hand, is using the ts-jest TypeScript preprocessor which transpiles files on the fly in the memory. It's not using Nest CLI at all, and thus, it can't use CLI plugins.
The solution for your issue is to precompile e2e tests + source files before you run them.
tsconfig.e2e-test.json in the root directory with the following content:{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "dist"]
}
package.json"build:e2e": "nest build -p tsconfig.e2e-test.json",
test/jest-e2e.json to use JS files and dist dir:{
"moduleFileExtensions": ["js", "json"],
"rootDir": "../dist",
"testEnvironment": "node",
"testRegex": ".e2e-spec.js$"
}
E2E tests will work now. Make sure to compile your code before running e2e tests again (or in the CI/CD pipeline).
Also, it might be useful to run the build and tests (concurrently) in the watch mode (add --watch flag to both scripts and run them in 2 terminals) if you want to get quick feedback on whether the test fails or not :)
It took me forever to find this. Perhaps this should be added to the documentation.
I think this should also be added to the default template created by the nest cli.
I bootstrapped a new project recently via nest new project-name and the way e2e is set up in it isn't working with the graphql cli plugin.
The last instructions here: https://docs.nestjs.com/graphql/cli-plugin seem to describe a process for building the GQL definitions on the fly without pre compiling using @nestjs/graphql/plugin, but I have been unable to get them to work, or find any example outside the that documentation that I linked. I wonder if anyone has tried this and gotten it working?
I checked this issue in October and the documentation was updated in November. 馃
I changed the testing setup by referring to the official documentation.
See https://github.com/CatsMiaow/node-nestjs-structure/commit/b3560530182ac31dddad0fd71bfbc95388c74995
For me the new solution from the official documentation works fine:
JSON
"globals": {
"ts-jest": {
"astTransformers": {
"before": ["<rootDir>/gql-plugin.e2e-spec.js"]
},
"tsConfig": "tsconfig.build.json"
}
}
@Yehonal How I can test the mutations?
Most helpful comment
For me the new solution from the official documentation works fine:
JSON "globals": { "ts-jest": { "astTransformers": { "before": ["<rootDir>/gql-plugin.e2e-spec.js"] }, "tsConfig": "tsconfig.build.json" } }