Skip to content

Commit f5feb41

Browse files
authored
Merge pull request #795 from Lemoncode/feature/update-testing-react
Feature/update testing react
2 parents f10e7aa + 01f9ddb commit f5feb41

357 files changed

Lines changed: 69472 additions & 261055 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
node_modules
22
dist
33
coverage
4-
.awcache
54
test-report.*
65
junit.xml
76
*.log
87
*.orig
9-
.awcache
108
public

05-testing/01-react/01-base/00-boilerplate/package-lock.json

Lines changed: 595 additions & 2851 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

05-testing/01-react/01-base/00-boilerplate/package.json

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@
1010
"type-check:watch": "npm run type-check -- --watch",
1111
"clean": "rimraf dist"
1212
},
13+
"imports": {
14+
"#*": "./src/*"
15+
},
1316
"author": "Lemoncode",
1417
"license": "MIT",
1518
"dependencies": {
16-
"axios": "^1.5.0",
17-
"react": "^18.2.0",
18-
"react-dom": "^18.2.0"
19+
"axios": "^1.7.9",
20+
"react": "^19.0.0",
21+
"react-dom": "^19.0.0"
1922
},
2023
"devDependencies": {
21-
"@types/node": "^18.17.12",
22-
"@types/react": "^18.2.21",
23-
"@types/react-dom": "^18.2.7",
24-
"@vitejs/plugin-react": "^4.0.4",
24+
"@types/node": "^22.10.2",
25+
"@types/react": "^19.0.1",
26+
"@types/react-dom": "^19.0.2",
27+
"@vitejs/plugin-react": "^4.3.4",
2528
"npm-run-all": "^4.1.5",
26-
"rimraf": "^5.0.1",
27-
"typescript": "^5.2.2",
28-
"vite": "^4.4.9"
29+
"rimraf": "^6.0.1",
30+
"typescript": "^5.7.2",
31+
"vite": "^6.0.3"
2932
}
3033
}

05-testing/01-react/01-base/00-boilerplate/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"esModuleInterop": true,
1414
"baseUrl": "./src/",
1515
"paths": {
16-
"@/*": ["*"]
16+
"#*": ["*"]
1717
}
1818
},
1919
"include": ["./src/**/*"]
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { defineConfig } from 'vite';
22
import react from '@vitejs/plugin-react';
3-
import { fileURLToPath, URL } from "node:url";
43

54
export default defineConfig({
65
plugins: [react()],
7-
resolve: {
8-
alias: {
9-
"@": fileURLToPath(new URL("./src", import.meta.url)),
10-
},
11-
},
126
});
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
node_modules
22
dist
33
coverage
4-
.awcache
54
test-report.*
65
junit.xml
76
*.log
87
*.orig
9-
.awcache
108
public

05-testing/01-react/01-base/01-config/README.md

Lines changed: 95 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# 01 Config
22

3-
In this example we are going to add a basic setup needed to support unit testing with Jest.
3+
In this example we are going to add a basic setup needed to support unit testing with Vitest.
44

55
We will start from `00-boilerplate`.
66

77
Summary steps:
88

9-
- Install `jest`.
9+
- Install `vitest`.
1010
- Add configuration.
1111
- Add dummy spec.
12-
- External jest config file.
12+
- External vitest config file.
1313

1414
# Steps to build it
1515

@@ -21,40 +21,32 @@ npm install
2121

2222
# Libraries
2323

24-
- We are going to install the main library which we base all our unit tests, [Jest](https://facebook.github.io/jest/en/).
24+
- We are going to install the main library which we base all our unit tests, [Vitest](https://vitest.dev/).
2525

26-
- [jest](https://github.com/facebook/jest): JavaScript Testing library with runner, assertion, mocks, etc.
27-
- [@types/jest](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/df38f202a0185eadfb6012e47dd91f8975eb6151/types/jest): Typings for jest.
26+
- [vitest](https://github.com/vitest-dev/vitest): JavaScript Testing library with runner, assertion, mocks, etc.
27+
28+
> Jest it's a good option for React projects, but it doesn't support [ECMAScript Modules](https://jestjs.io/docs/ecmascript-modules) yet.
29+
>
30+
> [Migration from Jest](https://vitest.dev/guide/migration.html#migrating-from-jest)
2831
2932
```bash
30-
npm install jest @types/jest --save-dev
33+
npm install vitest --save-dev
3134
```
3235

33-
> If we are using `@babel/preset-typescript` it is not necessary install [ts-jest](https://github.com/kulshekhar/ts-jest): A preprocessor with sourcemap support to help use TypeScript with Jest.
34-
>
35-
> [Official docs](https://jestjs.io/docs/getting-started#using-typescript)
36-
>
37-
> NOTE: [Since jest v29.x it drops support for Node 12](https://github.com/facebook/jest/releases/tag/v29.0.0)
38-
3936
# Config
4037

41-
Jest test commands:
42-
- `npm test`: to single run
43-
- `npm run test:watch`: to run all specs after changes.
44-
45-
> NOTE:
46-
47-
> [Jest CLI options](https://facebook.github.io/jest/docs/en/cli.html#options)
38+
Test commands:
39+
- `npm test`: it will run all specs in watch mode (by default). But it depends on the [process.env.CI](https://vitest.dev/config/#watch)
4840

49-
> --watchAll To rerun all tests.
50-
51-
> --watch To rerun tests related to changed files.
52-
53-
> --verbose Display individual test results with the test suite hierarchy.
54-
55-
> -i or --runInBand Run all tests serially in the current process, rather than creating a worker pool of child processes that run tests. This can be useful for debugging
41+
> NOTES:
42+
>
43+
> [CLI options](https://vitest.dev/guide/cli.html#options)
44+
>
45+
> [--watch](https://vitest.dev/guide/cli.html#watch): Enable watch mode
46+
>
47+
> [--globals](https://vitest.dev/guide/cli.html#globals): Inject Vitest methods into global scope
5648
57-
### ./package.json
49+
_./package.json_
5850

5951
```diff
6052
{
@@ -63,8 +55,7 @@ Jest test commands:
6355
...
6456
- "clean": "rimraf dist"
6557
+ "clean": "rimraf dist",
66-
+ "test": "jest --verbose",
67-
+ "test:watch": "npm test -- --watchAll -i"
58+
+ "test": "vitest --globals"
6859
},
6960
...
7061
}
@@ -75,12 +66,12 @@ Jest test commands:
7566
Let's launch tests in watch mode:
7667

7768
```bash
78-
npm run test:watch
69+
npm test
7970
```
8071

8172
Adding success spec:
8273

83-
### ./src/dummy.spec.ts
74+
_./src/dummy.spec.ts_
8475

8576
```javascript
8677
describe('dummy specs', () => {
@@ -95,9 +86,36 @@ describe('dummy specs', () => {
9586
});
9687
```
9788

89+
We could see that TypeScript doesn't recognize `vitest` methods, we need to add a `vitest` type definition:
90+
91+
_./vitest.d.ts_
92+
93+
```ts
94+
/// <reference types="vitest/globals" />
95+
96+
```
97+
98+
And add it to `tsconfig.json`:
99+
100+
_./tsconfig.json_
101+
102+
```diff
103+
{
104+
...
105+
},
106+
- "include": ["./src/**/*"]
107+
+ "include": ["./src/**/*", "./vitest.d.ts"]
108+
}
109+
110+
```
111+
112+
> Sometimes we need to restart the editor to recognize the new type definition
113+
>
114+
> Control/Command + Shift + P -> TypeScript: Restart TS server
115+
98116
Adding failed spec:
99117

100-
### ./src/dummy.spec.ts
118+
_./src/dummy.spec.ts_
101119

102120
```diff
103121
describe('dummy specs', () => {
@@ -125,35 +143,61 @@ describe('dummy specs', () => {
125143

126144
We could create a jest config outside `package.json` to improve maintainability.
127145

128-
> [Jest configuration options](https://facebook.github.io/jest/docs/en/configuration.html#options)
146+
> [Config file](https://vitest.dev/config/)
129147
130-
Create config in `config/test/jest.js` file:
148+
Create config in `config/test/config.ts` file:
131149

132-
### ./config/test/jest.js
150+
_./config/test/config.ts_
133151

134152
```js
135-
export default {
136-
rootDir: '../../',
137-
verbose: true,
138-
};
153+
import { defineConfig } from 'vitest/config';
154+
155+
export default defineConfig({
156+
test: {
157+
globals: true,
158+
},
159+
});
139160

140161
```
141-
> Check `verbose: false` to see differences
142-
>
143-
> We will add some configuration in next examples when needed
144162

145-
And use that file:
163+
> We will add more configuration in next examples when needed
164+
165+
Move also the `.d.ts` file:
166+
167+
_./config/test/config.d.ts_
168+
169+
```ts
170+
/// <reference types="vitest/globals" />
171+
172+
```
173+
174+
> Remove the ./vitest.d.ts file
175+
176+
And update the `tsconfig.json`:
146177

147-
### ./package.json
178+
_./tsconfig.json_
179+
180+
```diff
181+
{
182+
...
183+
},
184+
- "include": ["./src/**/*", "./vitest.d.ts"]
185+
+ "include": ["./src/**/*", "./config/test/config.d.ts"]
186+
}
187+
188+
```
189+
190+
Update the `package.json`:
191+
192+
_./package.json_
148193

149194
```diff
150195
{
151196
...
152197
"scripts": {
153198
...
154-
- "test": "jest --verbose",
155-
+ "test": "jest -c ./config/test/jest.js",
156-
"test:watch": "npm run test -- --watchAll -i"
199+
- "test": "vitest --globals"
200+
+ "test": "vitest -c ./config/test/config.ts"
157201
},
158202
...
159203
}
@@ -162,9 +206,11 @@ And use that file:
162206
Running specs again:
163207

164208
```bash
165-
npm run test:watch
209+
npm test
166210
```
167211

212+
> We could update the globals property in the config file to `false` to see the difference
213+
168214
# About Basefactor + Lemoncode
169215

170216
We are an innovating team of Javascript experts, passionate about turning your ideas into robust products.

0 commit comments

Comments
 (0)