【问题标题】:Angular 13 + jest + Angular library not workingAngular 13 + jest + Angular 库不工作
【发布时间】:2022-01-15 13:24:00
【问题描述】:

我正在尝试在 Angular 13 项目中使用 jest 进行测试。 我将 jest-preset-angular 与 @angular-builders/jest 一起使用。

我使用 ngx-pipes,这是一个不是为 Angular 13 编译的库。它在正常的 Angular 构建期间工作正常。但是在开玩笑的测试运行中会导致错误:

Error: Unexpected value 'NgStringPipesModule2' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation.

最小复制:https://github.com/dhcode/jest-preset-angular-13-issue

在我的实际项目中,其他 Angular 库也有同样的错误。 我之前跑过ng serve,所以在运行测试之前已经完成了ngcc编译。

如何让我的测试运行并识别使用旧 Angular 版本构建的外部库?

【问题讨论】:

    标签: angular jestjs jest-preset-angular


    【解决方案1】:

    从外观上看,ngx-pipes 还没有发布 Ivy 发行版,您的项目在运行测试之前仍然需要 ngcc

    // package.json
    {
      "scripts": {
        "test": "ngcc && ng test"
      }
    }
    
    > jest-tests@0.0.0 test
    > ngcc && ng test
    
    Processing legacy "View Engine" libraries:
    - ngx-pipes [fesm2015/esm2015] (https://github.com/danrevah/ngx-pipes.git)
    - ngx-pipes [esm2015/esm2015] (https://github.com/danrevah/ngx-pipes.git)
    - ngx-pipes [main/umd] (https://github.com/danrevah/ngx-pipes.git)
    Encourage the library authors to publish an Ivy distribution.
     PASS  src/app/app.component.spec.ts
      AppComponent
        ✓ should create the app (163 ms)
        ✓ should have as title 'jest-tests' (46 ms)
        ✓ should render title (44 ms)
        ✓ should render shortened text (33 ms)
    
    -------------------|---------|----------|---------|---------|-------------------
    File               | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    -------------------|---------|----------|---------|---------|-------------------
    All files          |     100 |      100 |     100 |     100 |                   
     ...component.html |     100 |      100 |     100 |     100 |                   
     app.component.ts  |     100 |      100 |     100 |     100 |                   
    -------------------|---------|----------|---------|---------|-------------------
    Test Suites: 1 passed, 1 total
    Tests:       4 passed, 4 total
    Snapshots:   0 total
    Time:        3.526 s, estimated 5 s
    Ran all test suites.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      • 2020-02-18
      • 1970-01-01
      • 2020-06-14
      • 2023-02-17
      • 2022-10-07
      • 1970-01-01
      相关资源
      最近更新 更多