/
vitest.spec.ts
180 lines (159 loc) · 5.47 KB
/
vitest.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { Tree, readProjectConfiguration } from '@nrwl/devkit';
import generator from './vitest-generator';
import { VitestGeneratorSchema } from './schema';
import { mockReactAppGenerator } from '../../utils/test-utils';
describe('vitest generator', () => {
let appTree: Tree;
const options: VitestGeneratorSchema = {
project: 'my-test-react-app',
uiFramework: 'react',
};
beforeEach(async () => {
appTree = createTreeWithEmptyWorkspace();
await mockReactAppGenerator(appTree);
});
it('Should add the test target', async () => {
await generator(appTree, options);
const config = readProjectConfiguration(appTree, 'my-test-react-app');
expect(config.targets['test']).toMatchInlineSnapshot(`
Object {
"executor": "@nrwl/vite:test",
"options": Object {
"passWithNoTests": true,
},
"outputs": Array [
"{workspaceRoot}/coverage/{projectRoot}",
],
}
`);
});
describe('tsconfig', () => {
it('should add a tsconfig.spec.json file', async () => {
await generator(appTree, options);
const tsconfig = JSON.parse(
appTree.read('apps/my-test-react-app/tsconfig.json')?.toString() ?? '{}'
);
expect(tsconfig.references).toMatchInlineSnapshot(`
Array [
Object {
"path": "./tsconfig.app.json",
},
Object {
"path": "./tsconfig.spec.json",
},
]
`);
const tsconfigSpec = JSON.parse(
appTree.read('apps/my-test-react-app/tsconfig.spec.json')?.toString() ??
'{}'
);
expect(tsconfigSpec).toMatchInlineSnapshot(`
Object {
"compilerOptions": Object {
"outDir": "../../dist/out-tsc",
"types": Array [
"vitest/globals",
"node",
],
},
"extends": "./tsconfig.json",
"include": Array [
"vite.config.ts",
"**/*.test.ts",
"**/*.spec.ts",
"**/*.test.tsx",
"**/*.spec.tsx",
"**/*.test.js",
"**/*.spec.js",
"**/*.test.jsx",
"**/*.spec.jsx",
"**/*.d.ts",
],
}
`);
});
it('should add vitest/importMeta when inSourceTests is true', async () => {
await generator(appTree, { ...options, inSourceTests: true });
const tsconfig = JSON.parse(
appTree.read('apps/my-test-react-app/tsconfig.app.json')?.toString() ??
'{}'
);
expect(tsconfig.compilerOptions.types).toMatchInlineSnapshot(`
Array [
"vitest/importMeta",
]
`);
});
});
describe('vite.config', () => {
it('should modify the vite.config.js file to include the test options', async () => {
await generator(appTree, options);
const viteConfig = appTree
.read('apps/my-test-react-app/vite.config.ts')
.toString();
expect(viteConfig).toMatchInlineSnapshot(`
"
/// <reference types=\\"vitest\\" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import tsconfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
server:{
port: 4200,
host: 'localhost',
},
plugins: [
react(),
tsconfigPaths({
root: '../../',
projects: ['tsconfig.base.json'],
}),
],
test: {
globals: true,
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
},
});"
`);
});
});
describe('insourceTests', () => {
it('should add the insourceSource option in the vite config', async () => {
await generator(appTree, { ...options, inSourceTests: true });
const viteConfig = appTree
.read('apps/my-test-react-app/vite.config.ts')
.toString();
expect(viteConfig).toMatchInlineSnapshot(`
"
/// <reference types=\\"vitest\\" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import tsconfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
server:{
port: 4200,
host: 'localhost',
},
plugins: [
react(),
tsconfigPaths({
root: '../../',
projects: ['tsconfig.base.json'],
}),
],
define: {
'import.meta.vitest': undefined
},
test: {
globals: true,
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
includeSource: ['src/**/*.{js,mjs,cjs,ts,mts,cts,jsx,tsx}']
},
});"
`);
});
});
});