Running a single test file

后端 未结 10 531
囚心锁ツ
囚心锁ツ 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:54

    You can go to src/test.ts and can change the following line:

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

    to

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

    0 讨论(0)
  • 2020-12-07 16:02

    I discovered that Jasmine allows you to prefix describe and it methods with an f (for focus): fdescribe and fit. If you use either of these, Karma will only run the relevant tests. To focus the current file, you can just take the top level describe and change it to fdescribe. If you use Jasmine prior to version 2.1, the focusing keywords are: iit and ddescribe.

    This example code runs just the first test:

    // Jasmine versions >/=2.1 use 'fdescribe'; versions <2.1 use 'ddescribe'
    fdescribe('MySpec1', function () {
        it('should do something', function () {
            // ...
        });
    });
    
    describe('MyOtherSpec', function () {
        it('should do something else', function () {
            // ...
        });
    });
    

    Here is the Jasmine documentation on Focusing Specs, and here is a related SO article that provides additional thoughtful solutions.

    0 讨论(0)
  • 2020-12-07 16:03

    Using ng test --main file.spec.ts

    0 讨论(0)
  • 2020-12-07 16:04

    Visual Studio Code Extension

    The easiest way is to use the vscode-test-explorer extension along with its child angular-karma-test-explorer and jasmine-test-adapter, you'll get a list of current test to run one by one if you want:

    This is the same answer i gave at this question, there's some more details there.

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