change this line
const context = require.context('./', true, /my-component.component.spec.ts/);
with this
const context = require.context('./', true, /my-component\.component\.spec\.ts$/);
to avoid the special character '.'
Angular 6 Unit testing : how to use test.ts for running karma tests
unit testing - How to test a specific spec.ts file in Angular 4? - Stack Overflow
How to run Angular 7 unit tests with custom environment.test.ts settings? - Stack Overflow
angular - Running a single test file - Stack Overflow
Videos
- This was caused by Visual Studio Code "helping" me by importing:
import { environment } from '@environments/environment.prod'; - The right place for
fileReplacementsis inarchitect.test.options.fileReplacements. - In order to prevent VSCode from ever "helping" me like this again, I'm going to rename all of the
environment.*.tsfiles other thanenvironment.tsto something likeDO-NOT-IMPORT.ENVIRONMENT.*.ts. At least that'll yell at me if I get the wrong import.
When you add a custom environment configuration, you need to add it also to the test element in the angular.json file, otherwise tests just run with the default environment configuration.
For instance, lets say you added an extra environment configuration named "customenv" to the already existing build.configurations element:
Copy"build": {
...
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"outputHashing": "all"
},
"development": {
"buildOptimizer": false,
"optimization": false,
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true
},
"customenv": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.customenv.ts"
}
]
}
},
"defaultConfiguration": "production"
},
At this point you can run
Copyng build --configuration=customenv
And then you might have also modified the already existing serve.configurations element like this:
Copy"serve": {
...
"configurations": {
"production": {
"browserTarget": "myproject:build:production"
},
"development": {
"browserTarget": "myproject:build:development"
},
"testws": {
"browserTarget": "myproject:build:customenv"
}
},
"defaultConfiguration": "development"
},
So now you can run:
Copyng serve --configuration=customenv
But if you try to run
Copyng test --configuration=customenv
You get the error:
An unhandled exception occurred: Configuration 'customenv' is not set in the workspace.
To be able to do that, you need to create a test.configurations element and add only the new custom environment:
Copy"test": {
...
"configurations": {
"customenv": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.customenv.ts"
}
]
}
},
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.