-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot test @Injectable() services with Jest #54
Comments
Can you reproduce it, please? |
Sure @NetanelBasal. Jest is setup with 'jest-preset-angular' ver. 8.0.0. Failed spec: `import { CtdPopupLoginComponent } from 'layouts/ctdPopup/CtdPopupLoginComponent'; describe('CtdPopupLoginComponent', () => { beforeEach(() => {
}); it('should be a defined component', () => { it('should open new window with params', () => { Error: TypeError: Cannot read property 'onDestroy' of undefined 3 | import { tap } from 'rxjs/operators';
at decorateDirective (node_modules/@ngneat/until-destroy/bundles/ngneat-until-destroy.umd.js:95:49) jest.mock('@ngneat/until-destroy', () => ({ ...jest.requireActual('@ngneat/until-destroy'), UntilDestroy: jest.fn().mockImplementation((options: any = {}) => { return (target: any) => { if (!target.ɵprov) { target.ɵprov = true; } return jest.requireActual('@ngneat/until-destroy').UntilDestroy(options)(target); }; }), })); Maybe there is a better way of checking Injectable than depending on target.ɵprov (injected by ivy compiler)? |
Why it's a |
It was formatted when I've pasted that. Its Sorry for that. In source code: `import { Component, OnDestroy } from '@angular/core'; @UntilDestroy() Those services for tests are created with: It's obviously problem with Jest environment, but it's because of usage of ivy flags in 'take-until'. |
We test the project with Jest, both the playground and the library, and it works fine. |
I'm also seeing this with Jest and a service provided at the component level after upgrading from Angular 8 and |
Gentlemen, that would be awesome if you could create a minimal reproducible example for us. |
@arturovt @NetanelBasal - Here is a repro repository: https://github.com/brianmcd/until-destroy-bug What I'm seeing is that if you provide the |
Thank you, Brian! I will try to have a look during the week when I get some free time! |
No problem. Thanks for looking into it, Artur. I should mention that I generated the repro project with Nx, since that's an easy way to get an app running with Jest. My real project that's hitting this bug is also using Nx. I don't think that should make a difference, but wanted to mention it just in case. |
Thanks so much for looking into this, @arturovt. I changed the postinstall to just run The postinstall command From digging through issues, it looks like Nx adds the postinstall so that ngcc runs before anything else, which was required for parallel builds. From what I could gather, the Angular CLI team is working on introducing a lockfile that should remove the need for the postinstall script for Nx users. The relevant issues are:
It's also worth noting that the standard Angular CLI doesn't add any postinstall step, and it seems to only build esm2015 versions, so I think users of the stock CLI who use Jest will see this behavior, too. |
Unfortunately in some cases |
Gentlemen, is it resolved? Can the issue be closed? |
For me, everything is working with running |
I am not sure that this should be considered closed just yet. I've been experiencing this issue myself and running However if I remove the usage of this library from the code while using NX's default ngcc parameters, the tests all pass. I haven't been able to find the exact reasoning as to why snapshots of the fixture fail when the extra parameters have been added to the I have forked @brianmcd's example repo here and have added a few branches demonstrating the different variations which can be tested by removing and reinstalling the node_modules folder to recompile with ngcc followed by running the tests If anyone can help resolve this, I would be extremely grateful! |
I had this issue as well. Changing the
In total my tests kept on failing. Fortunately, the solution for this problem was more straight forward: You have to add the line
to the projects test configuration file In case that's not clear enough, here's a small bash script to get a minimal example for this scenario up and running: # Set up a simple nx workspace
npx create-nx-workspace@latest demo
? What to create in the new workspace angular
? Application name demo
? Default stylesheet format CSS
cd demo
# Add some i18n
sed "s/<h1>/<h1 i18n>/" -i apps/demo/src/app/app.component.html
ng add @angular/localize
# Install and use `until-destroy`
npm install @ngneat/until-destroy
sed -e "2i import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';" \
-e "2i import { of } from 'rxjs';" \
-e "3i @UntilDestroy()" \
-e "10i obs$ = of().pipe(untilDestroyed(this));" \
-i apps/demo/src/app/app.component.ts
# Update the `postinstall` script to `ngcc`
sed "s/ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points/ngcc/" -i package.json
rm -rf node_modules
npm install
# Update Jest config
sed "5i setupFiles: ['./jest.setup.ts']," -i apps/demo/jest.config.js
echo "import '@angular/localize/init';" > apps/demo/jest.setup.ts
# Tests now pass again
ng test |
As an update on my report: It looks like there are compatibility issues within |
@Plysepter I have fixed this issue in this PR. |
Thank you @arturovt, it is greatly appreciated! |
I'm closing this issue. Please re-open with a reproduction if the problem still exists. Thanks. |
I've done migration from ngx-take-until-destroy to ngneat/until-destroy (after migration to angular packages in ver. 9.0.0). As application itself works fine, I've got issues with jest unit tests.
Error:
TypeError: Cannot read property 'onDestroy' of undefined
until-destroy/src/lib/internals.ts
Line 76 in 99c2c19
It's because you depend on target.ɵprov, but it doesn't have to be added by ivy compiler.
The text was updated successfully, but these errors were encountered: