kamilkisiela / apollo-angular

A fully-featured, production ready caching GraphQL client for Angular and every GraphQL server 🎁
https://apollo-angular.com
MIT License
1.5k stars 311 forks source link

Testing with apollo-angular@3.0.0 #1759

Closed it11ah1 closed 2 years ago

it11ah1 commented 2 years ago

Hello.

First I tried to get the test from https://apollo-angular.com/docs/development-and-testing/testing running. With some changes and additions it worked with apollo-angular@2.6.0 and graphql@15.8.0.

Going to apollo-angular@3.0.0 it ended with: NullInjectorError: R3InjectorError(DynamicTestModule)[Apollo -> Apollo]: NullInjectorError: No provider for Apollo!

Then I imported the new ApolloModule with the TestBed and got: Error: Client has not been defined yet at Apollo.checkInstance (node_modules/apollo-angular/build/fesm2020/ngApollo.mjs:213:19)

Importing much more ended with: Error: Expected one matching operation for criteria "Match DocumentNode", found none. at ApolloTestingBackend.expectOne (node_modules/apollo-angular/build/fesm2020/ngApolloTesting.mjs:135:19)

What is to do to get the test running with v3? And could ApolloTestingModule handle newly needed imports?

DanielKlier commented 2 years ago

I also have this issue after upgrading to v3 on Angular 13. I could work around this issue by specifying the provider explicitly:

beforeEach(() => {
    TestBed.configureTestingModule({ imports: [ApolloTestingModule], providers: [Apollo] });
  });
thecp commented 2 years ago

A second workaround for me was to import ApolloModule additionally or instead of ApolloTestingModule.

But when looking at the source code this imho doesn't make sense because ApolloTestingModule imports ApolloTestingModuleCore which itself imports ApolloModule, so ApolloTestingModule should be aware of Apollo provider

NgaiKaKit commented 2 years ago

I have facing same problem, and I have client not defined issue when I adding providers: [Apollo] or direct import ApolloModule. My work around are copying all files under the testing folder https://github.com/kamilkisiela/apollo-angular/tree/master/packages/apollo-angular/testing into my angular project, then everything work correctly without any code modification. So I believe the problem are cause by angular 13 build package build config, there are no problem for the source code.

MikaStark commented 2 years ago

Same as @NgaiKaKit. This is a terrible issue I think :/

griest024 commented 2 years ago

I agree with @thecp , the source code seems right, ApolloModule should be transitively imported by ApolloTestingModule. Maybe there is a token mismatch, i.e., the Apollo token imported from the source doesn't match the one imported from the built distributable?

griest024 commented 2 years ago

Note that importing ApolloModule does fix the provider issue but the Client has not been defined yet error persists.

griest024 commented 2 years ago

I discovered the issue! This is caused by the build package.json reffing the fesm2015 dist. The main package.json refs the fesm2020 dist. This causes a mismatch of the Apollo tokens and the host of other issues during testing.

See the module field of the published package.json for the outdated ref.

@kamilkisiela I think you published an outdated build. Please fix 🙏

damienwebdev commented 2 years ago

Just letting everyone know that I published https://www.npmjs.com/package/@damienwebdev/apollo-angular @ v3.0.1-alpha.1 as a temporary workaround.

It looks like @kamilkisiela had a bad publish script (he published packages/angular-apollo not packages/angular-apollo/build.

CalaxDev commented 2 years ago

When is the fix expected to be published into the main branch/released?

I am not sure if I feel comfortable pulling someone elses publish/fork of this repo

griest024 commented 2 years ago

When is the fix expected to be published into the main branch/released?

I am not sure if I feel comfortable pulling someone elses publish/fork of this repo

No one knows. @kamilkisiela is MIA. Publish your own version if you don't trust @damienwebdev .

You can also build the project yourself and diff his published version against the build directory. You will see that it is identical except for an updated import in the testing module.

CalaxDev commented 2 years ago

When is the fix expected to be published into the main branch/released? I am not sure if I feel comfortable pulling someone elses publish/fork of this repo

No one knows. @kamilkisiela is MIA. Publish your own version if you don't trust @damienwebdev .

You can also build the project yourself and diff his published version against the build directory. You will see that it is identical except for an updated import in the testing module.

Alright, thanks for the update! I hope they're doing well. It's scary times right now...

I'm just a bit more careful than I probably have to be, considering the malware/miner/other stuff injected into published modules. But in that case I'll just lock in that specific version.

Thanks a bunch!

damienwebdev commented 2 years ago

@CalaxDev that's a good idea. I have no plans to publish another version beyond that alpha. We needed the fix ASAP in our project to unblock progress so I published the stop-gap package. I'll leave it up indefinitely, but you should be mindful to track this issue in a few weeks to ensure you revert back once @kamilkisiela publishes a fix.

CalaxDev commented 2 years ago

Thanks a bunch @damienwebdev !

MikaStark commented 2 years ago

Thanks @damienwebdev you save the day 🙌

kamilkisiela commented 2 years ago

v3.0.1 is out

griest024 commented 2 years ago

v3.0.1 is out

3.0.1 fixes the issue. I vote this can be closed.