Intended outcome:
Running ng test --codeCoverage=true --no-watch should pass, since running without --codeCoverage passes
Actual outcome:
The test fails with TypeError: Cannot read property 'use' of undefined this looks to be in (http://localhost:9877/_karma_webpack_/node_modules/apollo-angular/fesm2015/ngApollo.js:239:1). It seems Apollo is not being injected correctly.
How to reproduce the issue:
When testing a Query from apollo-angular in a service (not a component) the tests will pass when run normally but will fail when generating code coverage. See the repository at https://github.com/aaraggornn/apollo-error-test for a reproduction.
Run npm run test to pass and npm run test:cc to fail (using --codeCoverage)
Versions
"apollo-angular": "^1.7.0",
"apollo-client": "^2.6.4",
"typescript": "~3.5.3",
"@angular/core": "~8.2.8",
"graphql": "^14.5.8",
"graphql-tag": "^2.10.1",
This also happens with apollo-angular 1.8.0
I noticed the error comes from this line, where this.apollo is referenced and its method use() is invoked (this causes the error apparently).
I don't know why this happens only when code coverage is enabled, but hopefully this info helps for debugging the problem a bit faster.
@aaraggornn I have found a temporary workaround. You'll need to inject Apollo manually in the extended Mutation class and call super() with the provided apollo instance:
import { Injectable } from '@angular/core';
import { Mutation, Apollo } from 'apollo-angular';
import gql from 'graphql-tag';
@Injectable({ providedIn: 'root' })
export class SomeMutationGQL extends Mutation {
constructor(apollo: Apollo) {
super(apollo); // <-- use this to prevent the error from throwing
}
public document = gql`
mutation ...
`;
}
It will work with --codeCoverage turned on.
Hi. I have same problem with using apollo-graphql with Angular 9.
We have the same strange problem in our project. Please fix.
Our solution was to ignore generated GraphQL types for the coverage. Perhaps this is related to the instrumentation of the generated code.
// https://github.com/thymikee/jest-preset-angular#brief-explanation-of-config
module.exports = {
preset: 'jest-preset-angular',
roots: ['src'],
setupFilesAfterEnv: ['<rootDir>/src/setup-jest.ts'],
moduleNameMapper: {
'@app/(.*)': '<rootDir>/src/app/$1',
'@assets/(.*)': '<rootDir>/src/assets/$1',
'@core/(.*)': '<rootDir>/src/app/core/$1',
'@env': '<rootDir>/src/environments/environment',
'@src/(.*)': '<rootDir>/src/src/$1',
'@state/(.*)': '<rootDir>/src/app/state/$1',
},
transformIgnorePatterns: ['node_modules/(?!(jest-test))'],
coverageReporters: ['html', 'text'],
+ coveragePathIgnorePatterns: ['/node_modules/', 'src/app/modules/graphql/types/generated.ts'],
};
Hope this helps.
Seems like it is caused by this issue https://github.com/angular/angular/issues/31337
Most helpful comment
@aaraggornn I have found a temporary workaround. You'll need to inject
Apollomanually in the extendedMutationclass and callsuper()with the provided apollo instance:It will work with
--codeCoverageturned on.