@analogjs/vitest-angular 1.4.0-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +218 -0
- package/builders.json +9 -0
- package/package.json +48 -0
- package/src/index.d.ts +2 -0
- package/src/index.js +6 -0
- package/src/index.js.map +1 -0
- package/src/lib/schema.json +34 -0
- package/src/lib/vitest.impl.d.ts +4 -0
- package/src/lib/vitest.impl.js +51 -0
- package/src/lib/vitest.impl.js.map +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# @analogjs/vitest-angular
|
|
2
|
+
|
|
3
|
+
A standalone builder for running tests with Vitest and Angular.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Use your package manager of choice to install the necessary packages.
|
|
8
|
+
|
|
9
|
+
With ng add:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
ng add @analogjs/vitest-angular
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
With npm:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install @analogjs/vitest-angular vitest --save-dev
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
With pnpm:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
pnpm install -w @analogjs/vitest-angular vitest --dev
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
With Yarn:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
yarn install @analogjs/vitest-angular vitest --dev
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Setup for Running Tests in Node
|
|
34
|
+
|
|
35
|
+
To setup Vitest, create a `vite.config.ts` at the root of your project:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
/// <reference types="vitest" />
|
|
39
|
+
import { defineConfig } from 'vite';
|
|
40
|
+
|
|
41
|
+
import angular from '@analogjs/vite-plugin-angular';
|
|
42
|
+
|
|
43
|
+
export default defineConfig(({ mode }) => ({
|
|
44
|
+
plugins: [angular()],
|
|
45
|
+
test: {
|
|
46
|
+
globals: true,
|
|
47
|
+
setupFiles: ['src/test-setup.ts'],
|
|
48
|
+
environment: 'jsdom',
|
|
49
|
+
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
|
|
50
|
+
reporters: ['default'],
|
|
51
|
+
},
|
|
52
|
+
define: {
|
|
53
|
+
'import.meta.vitest': mode !== 'production',
|
|
54
|
+
},
|
|
55
|
+
}));
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Next, define a `src/test-setup.ts` file to setup the `TestBed`:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import '@analogjs/vitest-angular/setup';
|
|
62
|
+
|
|
63
|
+
import {
|
|
64
|
+
BrowserDynamicTestingModule,
|
|
65
|
+
platformBrowserDynamicTesting,
|
|
66
|
+
} from '@angular/platform-browser-dynamic/testing';
|
|
67
|
+
import { getTestBed } from '@angular/core/testing';
|
|
68
|
+
|
|
69
|
+
getTestBed().initTestEnvironment(
|
|
70
|
+
BrowserDynamicTestingModule,
|
|
71
|
+
platformBrowserDynamicTesting()
|
|
72
|
+
);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Next, update the `test` target in the `angular.json` to use the `@analogjs/vitest-angular:test` builder:
|
|
76
|
+
|
|
77
|
+
```json
|
|
78
|
+
{
|
|
79
|
+
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
|
|
80
|
+
"version": 1,
|
|
81
|
+
"newProjectRoot": "projects",
|
|
82
|
+
"projects": {
|
|
83
|
+
"your-project": {
|
|
84
|
+
"projectType": "application",
|
|
85
|
+
"architect": {
|
|
86
|
+
"test": {
|
|
87
|
+
"builder": "@analogjs/vitest-angular:test"
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Lastly, add the `src/test-setup.ts` to `files` array in the `tsconfig.spec.json` in the root of your project, set the `target` to `es2016`, and update the `types`.
|
|
96
|
+
|
|
97
|
+
```json
|
|
98
|
+
{
|
|
99
|
+
"extends": "./tsconfig.json",
|
|
100
|
+
"compilerOptions": {
|
|
101
|
+
"outDir": "./out-tsc/spec",
|
|
102
|
+
"target": "es2016",
|
|
103
|
+
"types": ["vitest/globals", "node"]
|
|
104
|
+
},
|
|
105
|
+
"files": ["src/test-setup.ts"],
|
|
106
|
+
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Running Tests
|
|
111
|
+
|
|
112
|
+
To run unit tests, use the `test` command:
|
|
113
|
+
|
|
114
|
+
```shell
|
|
115
|
+
npm run test
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
```shell
|
|
119
|
+
yarn test
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
```shell
|
|
123
|
+
pnpm test
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
> The `npx vitest` command can also be used directly.
|
|
127
|
+
|
|
128
|
+
## Snapshot Testing
|
|
129
|
+
|
|
130
|
+
For snapshot testing you can use `toMatchSnapshot` from `expect` API.
|
|
131
|
+
|
|
132
|
+
Below is a small example of how to write a snapshot test:
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
// card.component.spec.ts
|
|
136
|
+
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|
137
|
+
|
|
138
|
+
import { CardComponent } from './card.component';
|
|
139
|
+
|
|
140
|
+
describe('CardComponent', () => {
|
|
141
|
+
let fixture: ComponentFixture<CardComponent>;
|
|
142
|
+
let component: CardComponent;
|
|
143
|
+
|
|
144
|
+
beforeEach(() =>
|
|
145
|
+
TestBed.configureTestingModule({
|
|
146
|
+
imports: [CardComponent],
|
|
147
|
+
})
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
beforeEach(() => {
|
|
151
|
+
fixture = TestBed.createComponent(CardComponent);
|
|
152
|
+
component = fixture.componentInstance;
|
|
153
|
+
fixture.detectChanges();
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('should create the app', () => {
|
|
157
|
+
expect(fixture).toMatchSnapshot();
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
After you run the test, a `card.component.spec.ts.snap` file is created in the`__snapshots__` folder with the below content:
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
166
|
+
|
|
167
|
+
exports[`CardComponent > should create the app 1`] = `
|
|
168
|
+
<component-code>
|
|
169
|
+
`;
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
The snapshots generated should be reviewed and added to version control.
|
|
173
|
+
|
|
174
|
+
## Using TypeScript Config Path Aliases
|
|
175
|
+
|
|
176
|
+
If you are using `paths` in your `tsconfig.json`, support for those aliases can be added to the `vite.config.ts`.
|
|
177
|
+
|
|
178
|
+
First, install the `vite-tsconfig-paths` package.
|
|
179
|
+
|
|
180
|
+
With npm:
|
|
181
|
+
|
|
182
|
+
```shell
|
|
183
|
+
npm install vite-tsconfig-paths --save-dev
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
With pnpm:
|
|
187
|
+
|
|
188
|
+
```shell
|
|
189
|
+
pnpm install -w vite-tsconfig-paths --dev
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
With Yarn:
|
|
193
|
+
|
|
194
|
+
```shell
|
|
195
|
+
yarn add vite-tsconfig-paths --dev
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Next, add the plugin to the `plugins` array in the `vite.config.ts`.
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
/// <reference types="vitest" />
|
|
202
|
+
import { defineConfig } from 'vite';
|
|
203
|
+
|
|
204
|
+
import angular from '@analogjs/vite-plugin-angular';
|
|
205
|
+
import viteTsConfigPaths from 'vite-tsconfig-paths';
|
|
206
|
+
|
|
207
|
+
export default defineConfig(({ mode }) => ({
|
|
208
|
+
// ...other config
|
|
209
|
+
plugins: [angular(), viteTsConfigPaths()],
|
|
210
|
+
}));
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## Supporting Analog
|
|
214
|
+
|
|
215
|
+
- Star the [GitHub Repo](https://github.com/analogjs/analog)
|
|
216
|
+
- Join the [Discord](https://chat.analogjs.org)
|
|
217
|
+
- Follow us on [Twitter](https://twitter.com/analogjs)
|
|
218
|
+
- Become a [Sponsor](https://analogjs.org/docs/sponsoring)
|
package/builders.json
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@analogjs/vitest-angular",
|
|
3
|
+
"version": "1.4.0-beta.6",
|
|
4
|
+
"description": "Vitest Builder for Angular",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"author": "Brandon Roberts <robertsbt@gmail.com>",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js",
|
|
9
|
+
"./package.json": "./package.json",
|
|
10
|
+
"./setup": "./setup.js"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"angular",
|
|
14
|
+
"vite",
|
|
15
|
+
"vitest",
|
|
16
|
+
"meta-framework"
|
|
17
|
+
],
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/analogjs/analog/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://analogjs.org",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/analogjs/analog.git"
|
|
26
|
+
},
|
|
27
|
+
"builders": "./builders.json",
|
|
28
|
+
"ng-add": {
|
|
29
|
+
"save": "devDependencies"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@analogjs/vite-plugin-angular": "^1.3.0",
|
|
33
|
+
"@angular-devkit/architect": "^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0",
|
|
34
|
+
"vitest": "^1.3.1"
|
|
35
|
+
},
|
|
36
|
+
"ng-update": {
|
|
37
|
+
"packageGroup": [
|
|
38
|
+
"@analogjs/platform",
|
|
39
|
+
"@analogjs/content",
|
|
40
|
+
"@analogjs/router",
|
|
41
|
+
"@analogjs/trpc",
|
|
42
|
+
"@analogjs/vite-plugin-angular",
|
|
43
|
+
"@analogjs/vite-plugin-nitro",
|
|
44
|
+
"@analogjs/vitest-angular"
|
|
45
|
+
]
|
|
46
|
+
},
|
|
47
|
+
"main": "./src/index.js"
|
|
48
|
+
}
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/vitest-angular/src/index.ts"],"names":[],"mappings":";;;AAAA,mDAA8C;AAErC,wBAFF,qBAAa,CAEE"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 2,
|
|
3
|
+
"cli": "nx",
|
|
4
|
+
"title": "Vitest Builder for Angular",
|
|
5
|
+
"description": "Run unit tests using Vitest.",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"properties": {
|
|
8
|
+
"configFile": {
|
|
9
|
+
"type": "string",
|
|
10
|
+
"description": "The path to the local vitest config",
|
|
11
|
+
"x-completion-type": "file",
|
|
12
|
+
"x-completion-glob": "@(vitest|vite).config@(.js|.ts)",
|
|
13
|
+
"aliases": ["config"]
|
|
14
|
+
},
|
|
15
|
+
"reportsDirectory": {
|
|
16
|
+
"type": "string",
|
|
17
|
+
"description": "Directory to write coverage report to."
|
|
18
|
+
},
|
|
19
|
+
"mode": {
|
|
20
|
+
"type": "string",
|
|
21
|
+
"description": "Mode for Vite."
|
|
22
|
+
},
|
|
23
|
+
"testFiles": {
|
|
24
|
+
"aliases": ["testFile"],
|
|
25
|
+
"type": "array",
|
|
26
|
+
"items": { "type": "string" }
|
|
27
|
+
},
|
|
28
|
+
"watch": {
|
|
29
|
+
"description": "Watch files for changes and rerun tests related to changed files.",
|
|
30
|
+
"type": "boolean"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"required": []
|
|
34
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getExtraArgs = void 0;
|
|
4
|
+
const architect_1 = require("@angular-devkit/architect");
|
|
5
|
+
async function vitestBuilder(options, context) {
|
|
6
|
+
process.env['TEST'] = 'true';
|
|
7
|
+
process.env['VITEST'] = 'true';
|
|
8
|
+
const { startVitest } = await Function('return import("vitest/node")')();
|
|
9
|
+
const projectConfig = await context.getProjectMetadata(context.target);
|
|
10
|
+
const extraArgs = await getExtraArgs(options);
|
|
11
|
+
const config = {
|
|
12
|
+
root: `${projectConfig['root'] || '.'}`,
|
|
13
|
+
watch: options.watch === true,
|
|
14
|
+
...extraArgs,
|
|
15
|
+
};
|
|
16
|
+
const server = await startVitest('test', options.testFiles ?? [], config);
|
|
17
|
+
let hasErrors = false;
|
|
18
|
+
const processExit = () => {
|
|
19
|
+
server?.exit();
|
|
20
|
+
if (hasErrors) {
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
process.exit(0);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
if (options.watch) {
|
|
28
|
+
process.on('SIGINT', processExit);
|
|
29
|
+
process.on('SIGTERM', processExit);
|
|
30
|
+
process.on('exit', processExit);
|
|
31
|
+
}
|
|
32
|
+
// vitest sets the exitCode = 1 when code coverage isn't met
|
|
33
|
+
hasErrors = (process.exitCode && process.exitCode !== 0);
|
|
34
|
+
return {
|
|
35
|
+
success: !hasErrors,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
async function getExtraArgs(options) {
|
|
39
|
+
// support passing extra args to Vitest CLI
|
|
40
|
+
const schema = await Promise.resolve().then(() => require('./schema.json'));
|
|
41
|
+
const extraArgs = {};
|
|
42
|
+
for (const key of Object.keys(options)) {
|
|
43
|
+
if (!schema.properties[key]) {
|
|
44
|
+
extraArgs[key] = options[key];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return extraArgs;
|
|
48
|
+
}
|
|
49
|
+
exports.getExtraArgs = getExtraArgs;
|
|
50
|
+
exports.default = (0, architect_1.createBuilder)(vitestBuilder);
|
|
51
|
+
//# sourceMappingURL=vitest.impl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vitest.impl.js","sourceRoot":"","sources":["../../../../../packages/vitest-angular/src/lib/vitest.impl.ts"],"names":[],"mappings":";;;AAAA,yDAImC;AAInC,KAAK,UAAU,aAAa,CAC1B,OAAqB,EACrB,OAAuB;IAEvB,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC;IAC7B,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,MAAM,CAAC;IAE/B,MAAM,EAAE,WAAW,EAAE,GAAG,MAAO,QAAQ,CACrC,8BAA8B,CAC/B,EAA4C,CAAC;IAE9C,MAAM,aAAa,GAAG,MAAM,OAAO,CAAC,kBAAkB,CACpD,OAAO,CAAC,MAA2B,CACpC,CAAC;IACF,MAAM,SAAS,GAAG,MAAM,YAAY,CAAC,OAAO,CAAC,CAAC;IAC9C,MAAM,MAAM,GAAG;QACb,IAAI,EAAE,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,GAAG,EAAE;QACvC,KAAK,EAAE,OAAO,CAAC,KAAK,KAAK,IAAI;QAC7B,GAAG,SAAS;KACb,CAAC;IAEF,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,SAAS,IAAI,EAAE,EAAE,MAAM,CAAC,CAAC;IAE1E,IAAI,SAAS,GAAG,KAAK,CAAC;IAEtB,MAAM,WAAW,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