-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
Bug: Can't export pipe MockOfAsyncPipe from MockOfPipeModule as it was neither declared nor imported! #4344
Comments
Hi @GipHub123, thanks for the report. a single test: import {TestBed} from '@angular/core/testing';
import {MockComponent, MockRender} from 'ng-mocks';
import {Component,NgModule} from '@angular/core';
import {AsyncPipe, CommonModule} from '@angular/common';
@NgModule({
imports: [CommonModule],
exports: [AsyncPipe],
})
export class SharedModule {}
@Component({
selector: 'standalone',
standalone: true,
template: '',
imports: [SharedModule],
})
export class StandaloneComponent {}
describe('issue-4344', () => {
describe('step1', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [StandaloneComponent],
}).compileComponents();
});
it('creates NotStandaloneComponent', () => {
expect(() => MockRender(StandaloneComponent)).not.toThrow();
});
});
describe('step2', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [MockComponent(StandaloneComponent)],
}).compileComponents();
});
it('creates NotStandaloneComponent', () => {
expect(() => MockRender(StandaloneComponent)).not.toThrow();
});
});
}); |
I found the problem, that's because of cached declarations. Is there a reason why you don't use The min test to reproduce the issue: import {TestBed} from '@angular/core/testing';
import {MockModule, MockRender} from 'ng-mocks';
import {Component,NgModule} from '@angular/core';
import {AsyncPipe, CommonModule} from '@angular/common';
@Component({
selector: 'target',
template: '',
})
export class TargetComponent {}
@NgModule({
declarations: [TargetComponent],
imports: [CommonModule],
exports: [AsyncPipe, TargetComponent],
})
export class TargetModule {}
// @see https://github.com/help-me-mom/ng-mocks/issues/4344
// exporting AsyncPipe from CommonModule which is kept,
// causes an issue, because ng-mocks mocks AsyncPipe, whereas it shouldn't.
// That happens because a previously checked CommonModule doesn't expose its guts anymore.
describe('issue-4344', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [MockModule(CommonModule), MockModule(TargetModule)],
}).compileComponents();
});
it('creates NotStandaloneComponent', () => {
expect(() => MockRender(TargetComponent)).not.toThrow();
});
}); |
Hi. Thanks for fast reply. I've been using MockBuilder whenever it's needed / I was not able to get test working. Project contains ~2200 test cases so I'm trying to make changes only when it's absolutely needed. zip-file contained how I've been able to get rid the error (file not-standalone.component-workaround.spec.ts). MockBuilder is causing some troubles because I didn't find a way to pass extra module / functionality. Have been trying to achieve following.
Most component's in a project are using ngx-translate so I've been using that extra module to set translation test context so that I can verify that translations are actually working. |
Understood, thanks for the info. A fix should released soon. |
fix(core): correct caching of touched declarations #4344
v14.4.0 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems. |
I've no longer the same problems / I can verify bug fix works. |
Hey there, thanks for the feedback. Feel free to report anything what you stumble over with the lib or angular testing. Stay in touch. |
Hi. I continued the migration and stumble across same kind of problem. WebStorm successfully runs the test case but using
I tried to reopen this issue but it looked that I don't have necessary rights to do it :| |
Hi @GipHub123, could you share a min example of the failing project? |
@satanTime. |
I tried to make simplified test case for you but unfortunately I haven't been able to reproduce the bug 😢 |
Hey there. I think it's a combination of two tests, there the first one breaks the second one. I would suggest to delete a test by test in your project, and if the project stops to fail - restore the deleted one and continue deletion of others until you have 2 or 3 tests left. That should be enough to create a min project with the issue. |
I updated project to the V15 since all 3rd parties had added support for v15.
Same problem exists here. Project is quite complicated. |
But if you remove the failing test - does it work or fails on a different test? |
I managed to get pass this. When I deleted most of the my test cases.
I rolled back most the changes and restarted migration. With these comment, I'll close this issue as completed. |
Hi @GipHub123, I would say based on:
one of your tests imports Could you share the |
Could you check if you test looks like that one? import {CdkFixedSizeVirtualScroll, ScrollingModule } from "@angular/cdk/scrolling";
import {Component, NgModule} from "@angular/core";
import {TestBed} from "@angular/core/testing";
import {MockModule, MockRender, ngMocks} from "ng-mocks";
@Component({
selector: 'dependency',
template: '<cdk-virtual-scroll-viewport [itemSize]="15"></cdk-virtual-scroll-viewport>',
})
class DependencyComponent {}
@NgModule({
imports: [ScrollingModule],
declarations: [DependencyComponent],
exports: [DependencyComponent, ScrollingModule],
})
class DependencyModule {}
@Component({
selector: 'target',
template: '<dependency></dependency><cdk-virtual-scroll-viewport [itemSize]="15"></cdk-virtual-scroll-viewport>',
})
class TargetComponent {}
@NgModule({
imports: [DependencyModule],
declarations: [TargetComponent],
exports: [TargetComponent],
})
class TargetModule {}
ngMocks.globalKeep(CdkFixedSizeVirtualScroll);
// @see https://github.com/help-me-mom/ng-mocks/issues/4344
// Type CdkFixedSizeVirtualScroll is part of the declarations of 2 modules:
// MockOfScrollingModule and ScrollingModule!
// Please consider moving CdkFixedSizeVirtualScroll to a higher module
// that imports MockOfScrollingModule and ScrollingModule.
fdescribe('issue-4344', () => {
describe('fully mocked', () => {
beforeEach(() => TestBed.configureTestingModule({
imports: [
MockModule(DependencyModule),
TargetModule,
],
}).compileComponents());
it('creates TargetComponent', () => {
expect(() => MockRender(TargetComponent)).not.toThrow();
});
});
}); where you keep a declaration globally with |
Hi @GipHub123, could you check whether this build fixes the issue: ng-mocks.zip? |
Hi, Not really 😐 I haven't use ngMocks.globalKeep -statement in any test case. Those MockOfScrollingModule and MatRippleModule errors came somewhere really deep from the component/module structure. I mean really really deep 😳 As I said earlier I haven't encounter these errors anymore 👍 I used quite a mount of time to investigate this one with no luck. |
I see, that's good :) if you have a chance to setup the failing env and test the updated build, it'll be really great. Otherwise, stay in touch and have a nice weekend! |
…odules are used together help-me-mom#4344
…odules are used together help-me-mom#4344
…odules are used together help-me-mom#4344
feat(core): hidden usage of MockBuilder in TestBed if kept and mock modules are used together #4344
v14.5.0 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems. |
Description of the bug
Hi,
I've started to migrating a project to use standalone components.
Not sure if this is bug or just me doing something wrong / is there something wrong with my environment?
I was able to create a simple example project.
When I run "ng test" -command, I got consistently following error:
An example of the bug
Link:
example_v2.zip
Weird thing is that when I run this failing test cases with WebStorm -> It passes consistently.
I tested this also with latest v15 and got similar results.
Expected vs actual behavior
Test passes
The text was updated successfully, but these errors were encountered: