there is a way to run tests against only one selected spec file. For doing that, we should configure test.ts
file inside src
folder. This file contains the below line of code which configures to include test files while running the ng test
or ng t
command.
const context = require.context('./', true, /\.spec\.ts$/);
The first parameter is the repository path (here, all files of an angular project) the third parameter includes all spec files.
let’s assume now, we have a “brice.component.spec.ts
” that we want to run a single component test file for debugging and testing it. There are multiple ways we can do it.
configure test.js to execute a single spec file
Replace /\.spec\.ts$/
with the file name you want to test. For example: /brice.component\.spec\.ts$/
then Runs the following command :
ng test [project]
//or
ng t [project]
This will run test only for app.component.spec.ts
.
PS: if we want to run all test spec files inside a folder, we can configure them like this
const context = require.context('./service', true, /\.spec\.ts$/);
this will run all spec files under the service
folder.
command line to run single test spec file
use --include
Angular CLI provides the --include
option to include a regular expression for including single or multiple files/folders.
ng test --include=**/brice.component.spec.ts // single file
ng test --include'**/service/*.spec.ts // multiple files inside a folder
or
// single file
npm run test -- --include src/app/brice.component.spec.ts
// all files inside the service folder
npm run test -- --include src/app/service
npm script to single test spec file
we can also configure the scripts
tag in package.json
{
"scripts": {
"singletest": "ng test --include=**/brice.component.spec.ts"
}
}
then run it with “npm run singletest” command.
jasmine and karma allow executing one component test spec
Jasmine provides fdescribe
and fit
allowing us to focus specific test cases to run it and skipped non-focused test case execution.
fdescribe('MyComponent', () => {
fit('should create', () => {
//test for undefined
}
}
fdescribe -> Allows to run a group of tests
fit -> Allows running a single test case
PS: Please note that use ddescribe
for jasmine version <2.1 version, and use fdescribe
for >2.1 version.
mocha and karma allow running a single component spec file
mocha provides describe.only and it.only
allowing us to focus specific test cases to run it and skipped non-focused test case execution.
describe.only
– Allows to run a group of testsit.only
– Allows running a single test case
describe.only('MyComponent', () => {
it.only('should create', () => {
//test for undefined
}
}