Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a single test file

People also ask

How do I run just one test file?

In order to run a specific test, you'll need to use the jest command. npm test will not work. To access jest directly on the command line, install it via npm i -g jest-cli or yarn global add jest-cli . Then simply run your specific test with jest bar.

How do you run only one describe jest?

Press p , and then type a filename. Then you can use describe. only and it. only which will skip all other tests from the filtered, tested file.


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.


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.


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

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