How to execute only one test spec with angular-cli

前端 未结 10 671
执笔经年
执笔经年 2020-12-02 06:00

I have Angular2 project build with Angular-CLI (beta 20).

Is there a way to run tests against only one selected spec file?

I used to have a project based on

相关标签:
10条回答
  • 2020-12-02 06:13

    Just small change need in test.ts file inside src folder:

    const context = require.context('./', true, /test-example\.spec\.ts$/);
    

    Here, test-example is the exact file name which we need to run

    In the same way, if you need to test the service file only you can replace the filename like "/test-example.service"

    0 讨论(0)
  • 2020-12-02 06:14

    This worked for me in every case:

    ng test --include='**/dealer.service.spec.ts'
    

    However, I usually got "TypeError: Cannot read property 'ngModule' of null" for this:

    ng test --main src/app/services/dealer.service.spec.ts
    

    Version of @angular/cli 10.0.4

    0 讨论(0)
  • 2020-12-02 06:19

    Configure test.ts file inside src folder:

    const context = require.context('./', true, /\.spec\.ts$/);
    

    Replace /\.spec\.ts$/ with the file name you want to test. For example: /app.component\.spec\.ts$/

    This will run test only for app.component.spec.ts.

    0 讨论(0)
  • 2020-12-02 06:27

    You can test only specific file with the Angular CLI (the ng command) like this:

    ng test --main ./path/to/test.ts
    

    Further docs are at https://angular.io/cli/test

    Note that while this works for standalone library files, it will not work for angular components/services/etc. This is because angular files have dependencies on other files (namely src/test.ts in Angular 7). Sadly the --main flag doesn't take multiple arguments.

    0 讨论(0)
提交回复
热议问题