Skip to content

Commit 11a1fb5

Browse files
committed
docs: Add instructions to migrate to jest
1 parent 1f3536b commit 11a1fb5

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
134 KB
Loading
26 KB
Loading
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
## Migrating your Angular project from Karma & Jasmine to Jest:
2+
3+
Your projects will use Karma and Jasmine by default if you use the [Angular schematics](https://angular.io/guide/schematics). However, the migration is pretty simple. Let's check what we need to do:
4+
5+
For this example, I'm going to use a basic Angular project created with the `ng new` schematics:
6+
7+
Then, if I run the command:
8+
```bash
9+
npm run test
10+
```
11+
I'll get:
12+
13+
![karma-preview](./assets/karma-preview.webp "Karma preview");
14+
15+
As you can see, all my test cases are running and passing without any problems. The idea at the end of the migration is to get the same result but using Jest.
16+
17+
Let's start with the migration:
18+
19+
1. Installing the jest dev dependencies
20+
```bash
21+
npm install jest jest-preset-angular --save-dev
22+
```
23+
24+
2. Creating the jest configuration file
25+
26+
In your root folder, create a file called: `jest.config.js` and add the following configuration:
27+
28+
```json
29+
module.exports = {
30+
preset: 'jest-preset-angular',
31+
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
32+
collectCoverage: true,
33+
coverageReporters: ['text'],
34+
roots: ['<rootDir>/src/'],
35+
testMatch: ['**/+(*.)+(spec).+(ts)'],
36+
setupFilesAfterEnv: ['<rootDir>/src/test.ts'],
37+
collectCoverage: true,
38+
coverageReporters: ['html', 'text']
39+
};
40+
```
41+
This configuration will show all the relevant information on the command line when you run the test command.
42+
43+
3. In your `package.json` let's update the test script:
44+
45+
```json
46+
{
47+
...
48+
"scripts": {
49+
...
50+
"test": "jest"
51+
...
52+
},
53+
...
54+
}
55+
```
56+
57+
4. Update the global test file.
58+
59+
Go to `src/test.ts` and replace all the content with:
60+
61+
```typescript
62+
import 'jest-preset-angular/setup-jest';
63+
64+
Object.defineProperty(window, 'CSS', {value: null});
65+
Object.defineProperty(window, 'getComputedStyle', {
66+
value: () => {
67+
return {
68+
display: 'none',
69+
appearance: ['-webkit-appearance']
70+
};
71+
}
72+
});
73+
74+
Object.defineProperty(document, 'doctype', {
75+
value: '<!DOCTYPE html>'
76+
});
77+
78+
Object.defineProperty(document.body.style, 'transform', {
79+
value: () => {
80+
return {
81+
enumerable: true,
82+
configurable: true
83+
};
84+
}
85+
});
86+
```
87+
This file will refer to Jest and not to Karma.
88+
89+
5. Uninstall Karma & Jasmine In order to get rid of all the Karma & Jasmine dependencies let's run:
90+
91+
```bash
92+
npm uninstall karma karma-chrome-launcher karma-coverage-istanbul-reporter karma-jasmine karma-jasmine-html-reporter
93+
```
94+
95+
6. Finally, you could delete the `karma.conf.js` file.
96+
97+
Now, let's run
98+
```bash
99+
npm run test
100+
```
101+
And you should get something like this:
102+
![jest-preview](./assets/jest-preview.gif "Jest preview");
103+
104+
That's it, we just migrate Jest into an existing Angular project. All the code can be found in this [Repository](https://github.com/Andres2D/angular-jest-migration). Go to the branch `jest-migration` to see the final result.

testing/unit-testing-jest/docs/unit-test-jest.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- [Benefits](./introduction.md#benefits)
77
- [Options to test?](./options-to-test.md)
88
- [Jest](./jest.md)
9+
- [Migrating to jest](./migrating-to-jest.md)
910
- [Basic structure of a test](./test-structure.md)
1011
- [Jest hooks](./jest-hooks.md)
1112
- [Repeating setUp](./jest-hooks.md#repeating-setup)

0 commit comments

Comments
 (0)