ng-mocks: Bug: Can't export pipe MockOfAsyncPipe from MockOfPipeModule as it was neither declared nor imported!

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:


Chrome 107.0.0.0 (Windows 10) NotStandaloneComponent should create FAILED
        Error: Can't export pipe MockOfAsyncPipe from MockOfPipeModule as it was neither declared nor imported!
        Can't export pipe MockOfCurrencyPipe from MockOfPipeModule as it was neither declared nor imported!
        Can't export pipe MockOfDatePipe from MockOfPipeModule as it was neither declared nor imported!
        Can't export pipe MockOfDecimalPipe from MockOfPipeModule as it was neither declared nor imported!
        Can't export pipe MockOfPercentPipe from MockOfPipeModule as it was neither declared nor imported!
            at verifySemanticsOfNgModuleDef (node_modules/@angular/core/fesm2020/core.mjs:24259:15)
            at Function.get (node_modules/@angular/core/fesm2020/core.mjs:24183:21)
            at getInjectorDef (node_modules/@angular/core/fesm2020/core.mjs:485:13)
            at walkProviderTree (node_modules/@angular/core/fesm2020/core.mjs:6479:18)
            at walkProviderTree (node_modules/@angular/core/fesm2020/core.mjs:6520:17)
            at walkProviderTree (node_modules/@angular/core/fesm2020/core.mjs:6520:17)
            at fn (node_modules/@angular/core/fesm2020/core.mjs:6535:25)
            at forEach (node_modules/@angular/core/fesm2020/core.mjs:4217:76)
            at Array.forEach (<anonymous>)
            at deepForEach (node_modules/@angular/core/fesm2020/core.mjs:4217:11)
Chrome 107.0.0.0 (Windows 10): Executed 6 of 6 (1 FAILED) (0.341 secs / 0.307 secs)
TOTAL: 1 FAILED, 5 SUCCESS
$ ng version

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 14.2.10
Node: 16.17.0
Package Manager: npm 8.15.0
OS: win32 x64

Angular: 14.2.11
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1402.10
@angular-devkit/build-angular   14.2.10
@angular-devkit/core            14.2.10
@angular-devkit/schematics      14.2.10
@angular/cli                    14.2.10
@schematics/angular             14.2.10
rxjs                            7.5.7
typescript                      4.7.4

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.

standalone_webstorm

Expected vs actual behavior

Test passes

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 21 (13 by maintainers)

Commits related to this issue

Most upvoted comments

I found the problem, that’s because of cached declarations.

Is there a reason why you don’t use MockBuilder?

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

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!

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.