Since at least 2019:
npm test -- bar.spec.js
In 2015:
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.spec.js.
Note: You don't have to enter the full path to your test file. The argument is interpreted as a regular expression. Any part of the full path that uniquely identifies a file suffices.
Answer from ncuillery on Stack OverflowSince at least 2019:
npm test -- bar.spec.js
In 2015:
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.spec.js.
Note: You don't have to enter the full path to your test file. The argument is interpreted as a regular expression. Any part of the full path that uniquely identifies a file suffices.
All you have to do is chant the magic incantation:
npm test -- SomeTestFileToRun
The stand-alone -- is *nix magic for marking the end of options, meaning (for NPM) that everything after that is passed to the command being run, in this case jest. As an aside, you can display Jest usage notes by saying
npm test -- --help
Anyhow, chanting
npm test -- Foo
runs the tests in the named file (FooBar.js). You should note, though, that:
Jest treats the name as case-sensitive, so if you're using a case-insensitive, but case-preserving file system (like Windows NTFS), you might encounter what appears to be oddness going on.
Jest appears to treat the specification as a prefix.
So the above incantation will
- Run
FooBar.js,Foo.jsandFooZilla.js - But not run
foo.js
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.
ng test specific file
Cannot run a single test file
Support single-file running of Unit testing
Run A Single Unit Test
I tried many commands that I could find regarding this topic but nothing worked out. What I’m trying to do is execute a single spec.ts file.
• Angular & Nx Version 17
How do I do that?
Edit: resolved
=> nx test <library> —include=libs/path-to-spec.ts
library = project.json: value of name
Each of your .spec.ts file have all its tests grouped in describe block like this:
describe('SomeComponent', () => {...}
You can easily run just this single block, by prefixing the describe function name with f:
fdescribe('SomeComponent', () => {...}
If you have such function, no other describe blocks will run.
Btw. you can do similar thing with it => fit and there is also a "blacklist" version - x. So:
fdescribeandfitcauses only functions marked this way to runxdescribeandxitcauses all but functions marked this way to run
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.