Running a single test file

后端 未结 10 530
囚心锁ツ
囚心锁ツ 2020-12-07 15:29

Is there a way to run ng test for a single file instead of for the entire test suite? Ideally, I\'d like to get the quickest possible feedback loop when I\'m e

相关标签:
10条回答
  • 2020-12-07 15:42

    I found that ng test has an additional option --include which you can use in order to be able to run test for a single file, or for a particular directory, or for a bunch of files:

    // one file
    npm run test -- --include src/app/components/component/component-name.component.spec.ts
    
    // directory or bunch of files
    npm run test -- --include src/app/components
    

    ng cli docs

    0 讨论(0)
  • 2020-12-07 15:42

    In Angular 9 I have had luck with the following:

    If you want to test a specific file:

    ng test --test-file=path/to/your/file.component.spec.ts
    

    If you want to test only what has changed since your last commit (using git or other version control)

    ng test --only-changed
    

    If you have multiple projects in your Angular project and/or are using NX, you can specify an Angular project to test:

    ng test project-name
    


    You can also use

    ng test --testNamePattern componentname
    

    for something like path/to/your/component/componentname.spec.ts. This will scan every file in every project, and is slower.

    0 讨论(0)
  • 2020-12-07 15:43

    This can be achieved these days via the include option. https://angular.io/cli/test#options

    It's a glob match, so as an example:

    ng test --include='**/someFolder/*.spec.ts'
    

    I can't find it in the 8.1.0 release notes, but @Swoox mentions below this is a feature after cli version 8.1.0. Thanks for figuring that out.

    0 讨论(0)
  • 2020-12-07 15:48

    It's worth mentioning that you can disable particular test without commenting by xdescribe and xit

    xdescribe('Hello world', () => { 
      xit('says hello', () => { 
        expect(helloWorld())
            .toEqual('Hello world!');
      });
    });
    

    And as somebody already said if you want to focus on some test then fdescribe and fit

    fdescribe('Hello world', () => { 
      fit('says hello', () => { 
        expect(helloWorld())
            .toEqual('Hello world!');
      });
    });
    
    0 讨论(0)
  • 2020-12-07 15:51

    Works if you specify your spec file as parameter.

    For example:

    ng test foo.spec.ts

    Hope this helps.

    0 讨论(0)
  • 2020-12-07 15:52

    You must have to go src/test.ts and can change the following line number code 18:

    //Then we find all the tests.
    const context = require.context('./', true, /\.spec\.ts$/);
    

    to

    //Then we find all the tests.
    const context = require.context('./', true, /testing.component\.spec\.ts$/);
    

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