Graphql: Graphql cli plugin is not working in graphql e2e tests

Created on 23 Apr 2020  路  7Comments  路  Source: nestjs/graphql

I'm submitting a...


[ ] 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.

Current behavior


With graphql cli plugin being enabled, when graphql e2e tests are ran, it couldn't recognize graphql fields and raise errors.

Expected behavior


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()

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?


Graphql cli plugin is useful in reducing boilerplate in defining graphql schema, but in tests it isn't working :(

Environment


Nest version: 7.0.8


For Tooling issues:
- Node version: 13.12.0  
- Platform:  Mac 

Others:

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" } }

All 7 comments

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.

  1. Create a new tsconfig.e2e-test.json in the root directory with the following content:
{
  "extends": "./tsconfig.json",
  "exclude": ["node_modules", "dist"]
}
  1. Define a new script in the package.json
"build:e2e": "nest build -p tsconfig.e2e-test.json",
  1. Update the 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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

galkin picture galkin  路  4Comments

liudonghua123 picture liudonghua123  路  3Comments

ghost picture ghost  路  5Comments

danil-z picture danil-z  路  4Comments

cschroeter picture cschroeter  路  3Comments