Running a single test file

2019-03-10 12:35发布

问题:

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 editing a file, but karma executes the whole suite on each save, which is a bit slow when you build up a big enough test suite.

回答1:

I discovered that Jasmine allows you to prefix describe and it methods with an f (for focus?). So, fdescribe and fit. If you use either of these, karma will only run the relevant tests. So, to focus the current file, you can just take the top level describe and change it to fdescribe. Works for my purposes.



回答2:

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!');
  });
});


回答3:

Works if you specify your spec file as parameter.

For example:

ng test foo.spec.ts

Hope this helps.