@analogjs/vitest-angular 3.0.0-alpha.8 → 3.0.0-alpha.9
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/_virtual/_rolldown/runtime.js +7 -0
- package/package.json +5 -8
- package/setup-snapshots.js +71 -95
- package/setup-snapshots.js.map +1 -0
- package/setup-testbed.js +23 -29
- package/setup-testbed.js.map +1 -0
- package/setup-zone.js +90 -103
- package/setup-zone.js.map +1 -0
- package/src/index.js +3 -4
- package/src/lib/builders/build/devkit.js +22 -17
- package/src/lib/builders/build/devkit.js.map +1 -0
- package/src/lib/builders/build/plugins/angular-memory-plugin.js +51 -77
- package/src/lib/builders/build/plugins/angular-memory-plugin.js.map +1 -0
- package/src/lib/builders/build/plugins/downlevel-plugin.js +23 -22
- package/src/lib/builders/build/plugins/downlevel-plugin.js.map +1 -0
- package/src/lib/builders/build/schema.js +53 -0
- package/src/lib/builders/build/schema.js.map +1 -0
- package/src/lib/builders/build/vitest.impl.js +153 -190
- package/src/lib/builders/build/vitest.impl.js.map +1 -0
- package/src/lib/builders/test/schema.js +51 -0
- package/src/lib/builders/test/schema.js.map +1 -0
- package/src/lib/builders/test/vitest.impl.js +48 -58
- package/src/lib/builders/test/vitest.impl.js.map +1 -0
- package/src/lib/tools/package.json +2 -4
- package/src/lib/tools/src/index.js +3 -6
- package/src/lib/tools/src/schematics/setup/index.js +46 -71
- package/src/lib/tools/src/schematics/setup/index.js.map +1 -1
- package/src/lib/tools/src/schematics/utils/angular.js +16 -22
- package/src/lib/tools/src/schematics/utils/angular.js.map +1 -1
- package/src/lib/tools/src/schematics/utils/dependencies.js +34 -44
- package/src/lib/tools/src/schematics/utils/dependencies.js.map +1 -1
- package/src/lib/tools/src/schematics/utils/index.js +4 -8
- package/src/lib/tools/src/schematics/utils/versions.d.ts +1 -1
- package/src/lib/tools/src/schematics/utils/versions.js +17 -12
- package/src/lib/tools/src/schematics/utils/versions.js.map +1 -1
- package/src/lib/tools/src/schematics/utils/workspace.js +14 -17
- package/src/lib/tools/src/schematics/utils/workspace.js.map +1 -1
- package/src/lib/tools/src/test-global-setup.d.ts +1 -0
- package/README.md +0 -297
- package/src/lib/builders/build/schema.json +0 -47
- package/src/lib/builders/build/utils.js +0 -1
- package/src/lib/builders/test/schema.json +0 -47
- package/src/lib/tools/src/index.js.map +0 -1
- package/src/lib/tools/src/schematics/utils/index.js.map +0 -1
package/README.md
DELETED
|
@@ -1,297 +0,0 @@
|
|
|
1
|
-
# @analogjs/vitest-angular
|
|
2
|
-
|
|
3
|
-
A standalone builder for running tests with Vitest and Angular.
|
|
4
|
-
|
|
5
|
-
## Supporting Analog
|
|
6
|
-
|
|
7
|
-
- Star the [GitHub Repo](https://github.com/analogjs/analog)
|
|
8
|
-
- Join the [Discord](https://chat.analogjs.org)
|
|
9
|
-
- Follow us on [Twitter](https://twitter.com/analogjs)
|
|
10
|
-
- Become a [Sponsor](https://analogjs.org/docs/sponsoring)
|
|
11
|
-
|
|
12
|
-
## Installation
|
|
13
|
-
|
|
14
|
-
Use your package manager of choice to install the necessary packages.
|
|
15
|
-
|
|
16
|
-
With ng add:
|
|
17
|
-
|
|
18
|
-
```sh
|
|
19
|
-
ng add @analogjs/vitest-angular
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
With npm:
|
|
23
|
-
|
|
24
|
-
```sh
|
|
25
|
-
npm install @analogjs/vitest-angular vitest --save-dev
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
With pnpm:
|
|
29
|
-
|
|
30
|
-
```sh
|
|
31
|
-
pnpm install -w @analogjs/vitest-angular vitest --dev
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
With Yarn:
|
|
35
|
-
|
|
36
|
-
```sh
|
|
37
|
-
yarn install @analogjs/vitest-angular vitest --dev
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
## Automated Setup Using a Schematic
|
|
41
|
-
|
|
42
|
-
A schematic can be used to setup Vitest in an existing Angular project:
|
|
43
|
-
|
|
44
|
-
Run the schematic to set up the Vite config, test configuration files, and update the test configuration.
|
|
45
|
-
|
|
46
|
-
```sh
|
|
47
|
-
npx ng generate @analogjs/vitest-angular:setup --project [your-project-name]
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### Schematic Options
|
|
51
|
-
|
|
52
|
-
| Option | Type | Default | Description |
|
|
53
|
-
| ------------- | ------- | ------- | ----------------------------------------------------------- |
|
|
54
|
-
| `project` | string | - | The name of the project to configure (required) |
|
|
55
|
-
| `browserMode` | boolean | `false` | Configure Vitest to run tests in a browser using Playwright |
|
|
56
|
-
|
|
57
|
-
To enable browser mode during setup:
|
|
58
|
-
|
|
59
|
-
```shell
|
|
60
|
-
ng g @analogjs/vitest-angular:setup --project [your-project-name] --browserMode
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
This automatically installs Playwright dependencies and configures Vitest for browser testing. See [Setup for Running Tests in the Browser](#setup-for-running-tests-in-the-browser) for more details.
|
|
64
|
-
|
|
65
|
-
If using browser mode, run `npx playwright install` after the schematic to ensure playwright is installed and configured.
|
|
66
|
-
|
|
67
|
-
## Manual Setup
|
|
68
|
-
|
|
69
|
-
Vitest can be setup manually also using the steps below.
|
|
70
|
-
|
|
71
|
-
### Setup for Running Tests in Node
|
|
72
|
-
|
|
73
|
-
To setup Vitest, create a `vite.config.mts` at the root of your project:
|
|
74
|
-
|
|
75
|
-
```ts
|
|
76
|
-
/// <reference types="vitest" />
|
|
77
|
-
import { defineConfig } from 'vite';
|
|
78
|
-
|
|
79
|
-
import angular from '@analogjs/vite-plugin-angular';
|
|
80
|
-
|
|
81
|
-
export default defineConfig(({ mode }) => ({
|
|
82
|
-
plugins: [angular()],
|
|
83
|
-
test: {
|
|
84
|
-
globals: true,
|
|
85
|
-
setupFiles: ['src/test-setup.ts'],
|
|
86
|
-
environment: 'jsdom',
|
|
87
|
-
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
|
|
88
|
-
reporters: ['default'],
|
|
89
|
-
},
|
|
90
|
-
}));
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
Next, define a `src/test-setup.ts` file to setup the `TestBed`:
|
|
94
|
-
|
|
95
|
-
### Zoneless setup
|
|
96
|
-
|
|
97
|
-
As of Angular v21, `Zoneless` change detection is the default for new projects.
|
|
98
|
-
|
|
99
|
-
Use the following setup:
|
|
100
|
-
|
|
101
|
-
```ts
|
|
102
|
-
import '@angular/compiler';
|
|
103
|
-
import '@analogjs/vitest-angular/setup-snapshots';
|
|
104
|
-
import { setupTestBed } from '@analogjs/vitest-angular/setup-testbed';
|
|
105
|
-
|
|
106
|
-
setupTestBed();
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
### Zone.js setup
|
|
110
|
-
|
|
111
|
-
If you are using `Zone.js` for change detection, import the `setup-zone` script. This script automatically includes support for setting up snapshot tests.
|
|
112
|
-
|
|
113
|
-
```ts
|
|
114
|
-
import '@angular/compiler';
|
|
115
|
-
import '@analogjs/vitest-angular/setup-zone';
|
|
116
|
-
import { setupTestBed } from '@analogjs/vitest-angular/setup-testbed';
|
|
117
|
-
|
|
118
|
-
setupTestBed({
|
|
119
|
-
zoneless: false,
|
|
120
|
-
});
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
### Configuration Options
|
|
124
|
-
|
|
125
|
-
The `setupTestBed()` function accepts an optional configuration object with the following properties:
|
|
126
|
-
|
|
127
|
-
- `zoneless` (boolean): Whether to use zoneless change detection (default: `true`)
|
|
128
|
-
- `providers` (`Type<any>[]`): Additional providers to include in the test environment (default: `[]`)
|
|
129
|
-
- `teardown.destroyAfterEach` (boolean): Whether to destroy the test environment after each test. Set to `false` to keep the component rendered, allowing you to inspect its final state. (default: `true`)
|
|
130
|
-
|
|
131
|
-
**Example with options:**
|
|
132
|
-
|
|
133
|
-
```ts
|
|
134
|
-
setupTestBed({
|
|
135
|
-
zoneless: true,
|
|
136
|
-
providers: [],
|
|
137
|
-
teardown: { destroyAfterEach: false },
|
|
138
|
-
});
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
Next, update the `test` target in the `angular.json` to use the `@analogjs/vitest-angular:test` builder:
|
|
142
|
-
|
|
143
|
-
```json
|
|
144
|
-
{
|
|
145
|
-
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
|
|
146
|
-
"version": 1,
|
|
147
|
-
"newProjectRoot": "projects",
|
|
148
|
-
"projects": {
|
|
149
|
-
"your-project": {
|
|
150
|
-
"projectType": "application",
|
|
151
|
-
"architect": {
|
|
152
|
-
"test": {
|
|
153
|
-
"builder": "@analogjs/vitest-angular:test"
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
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 `es2022`, and update the `types`.
|
|
162
|
-
|
|
163
|
-
```json
|
|
164
|
-
{
|
|
165
|
-
"extends": "./tsconfig.json",
|
|
166
|
-
"compilerOptions": {
|
|
167
|
-
"outDir": "./out-tsc/spec",
|
|
168
|
-
"target": "es2022",
|
|
169
|
-
"types": ["vitest/globals", "node"]
|
|
170
|
-
},
|
|
171
|
-
"files": ["src/test-setup.ts"],
|
|
172
|
-
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
|
|
173
|
-
}
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
## Running Tests
|
|
177
|
-
|
|
178
|
-
To run unit tests, use the `test` command:
|
|
179
|
-
|
|
180
|
-
```shell
|
|
181
|
-
npm run test
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
```shell
|
|
185
|
-
yarn test
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
```shell
|
|
189
|
-
pnpm test
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
> The `npx vitest` command can also be used directly.
|
|
193
|
-
|
|
194
|
-
## Snapshot Testing
|
|
195
|
-
|
|
196
|
-
For snapshot testing you can use `toMatchSnapshot` from `expect` API.
|
|
197
|
-
|
|
198
|
-
Below is a small example of how to write a snapshot test:
|
|
199
|
-
|
|
200
|
-
```ts
|
|
201
|
-
// card.component.spec.ts
|
|
202
|
-
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|
203
|
-
|
|
204
|
-
import { CardComponent } from './card.component';
|
|
205
|
-
|
|
206
|
-
describe('CardComponent', () => {
|
|
207
|
-
let fixture: ComponentFixture<CardComponent>;
|
|
208
|
-
let component: CardComponent;
|
|
209
|
-
|
|
210
|
-
beforeEach(() =>
|
|
211
|
-
TestBed.configureTestingModule({
|
|
212
|
-
imports: [CardComponent],
|
|
213
|
-
}),
|
|
214
|
-
);
|
|
215
|
-
|
|
216
|
-
beforeEach(() => {
|
|
217
|
-
fixture = TestBed.createComponent(CardComponent);
|
|
218
|
-
component = fixture.componentInstance;
|
|
219
|
-
fixture.detectChanges();
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
it('should create the app', () => {
|
|
223
|
-
expect(fixture).toMatchSnapshot();
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
```
|
|
227
|
-
|
|
228
|
-
After you run the test, a `card.component.spec.ts.snap` file is created in the`__snapshots__` folder with the below content:
|
|
229
|
-
|
|
230
|
-
```ts
|
|
231
|
-
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
232
|
-
|
|
233
|
-
exports[`CardComponent > should create the app 1`] = `
|
|
234
|
-
<component-code>
|
|
235
|
-
`;
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
The snapshots generated should be reviewed and added to version control.
|
|
239
|
-
|
|
240
|
-
## Using TypeScript Config Path Aliases
|
|
241
|
-
|
|
242
|
-
If you are using `paths` in your `tsconfig.json`, support for those aliases can be added to the `vite.config.ts`.
|
|
243
|
-
|
|
244
|
-
First, install the `vite-tsconfig-paths` package.
|
|
245
|
-
|
|
246
|
-
With npm:
|
|
247
|
-
|
|
248
|
-
```shell
|
|
249
|
-
npm install vite-tsconfig-paths --save-dev
|
|
250
|
-
```
|
|
251
|
-
|
|
252
|
-
With pnpm:
|
|
253
|
-
|
|
254
|
-
```shell
|
|
255
|
-
pnpm install -w vite-tsconfig-paths --dev
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
With Yarn:
|
|
259
|
-
|
|
260
|
-
```shell
|
|
261
|
-
yarn add vite-tsconfig-paths --dev
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
Next, add the plugin to the `plugins` array in the `vite.config.ts`.
|
|
265
|
-
|
|
266
|
-
```ts
|
|
267
|
-
/// <reference types="vitest" />
|
|
268
|
-
import { defineConfig } from 'vite';
|
|
269
|
-
|
|
270
|
-
import angular from '@analogjs/vite-plugin-angular';
|
|
271
|
-
import viteTsConfigPaths from 'vite-tsconfig-paths';
|
|
272
|
-
|
|
273
|
-
export default defineConfig(({ mode }) => ({
|
|
274
|
-
// ...other config
|
|
275
|
-
plugins: [angular(), viteTsConfigPaths()],
|
|
276
|
-
}));
|
|
277
|
-
```
|
|
278
|
-
|
|
279
|
-
### With Nx
|
|
280
|
-
|
|
281
|
-
For Nx workspaces, import and use the `nxViteTsPaths` plugin from the `@nx/vite` package.
|
|
282
|
-
|
|
283
|
-
```ts
|
|
284
|
-
/// <reference types="vitest" />
|
|
285
|
-
import { defineConfig } from 'vite';
|
|
286
|
-
|
|
287
|
-
import angular from '@analogjs/vite-plugin-angular';
|
|
288
|
-
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
|
|
289
|
-
|
|
290
|
-
export default defineConfig(({ mode }) => ({
|
|
291
|
-
plugins: [angular(), nxViteTsPaths()],
|
|
292
|
-
}));
|
|
293
|
-
```
|
|
294
|
-
|
|
295
|
-
## IDE Support
|
|
296
|
-
|
|
297
|
-
Tests can also be run directly from your IDE using the Vitest [IDE integrations](https://vitest.dev/guide/ide) for VS Code or JetBrains IDEs.
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "http://json-schema.org/draft-07/schema",
|
|
3
|
-
"title": "Vitest schema for Test Facade.",
|
|
4
|
-
"description": "Vitest target options",
|
|
5
|
-
"type": "object",
|
|
6
|
-
"properties": {
|
|
7
|
-
"configFile": {
|
|
8
|
-
"type": "string",
|
|
9
|
-
"description": "The path to the local vitest config",
|
|
10
|
-
"x-completion-type": "file",
|
|
11
|
-
"x-completion-glob": "@vitest.config@(.js|.ts|.mts)",
|
|
12
|
-
"aliases": ["config"]
|
|
13
|
-
},
|
|
14
|
-
"include": {
|
|
15
|
-
"type": "array",
|
|
16
|
-
"items": {
|
|
17
|
-
"type": "string"
|
|
18
|
-
},
|
|
19
|
-
"default": ["src/**/*.spec.ts"],
|
|
20
|
-
"description": "Globs of files to include, relative to project root."
|
|
21
|
-
},
|
|
22
|
-
"exclude": {
|
|
23
|
-
"type": "array",
|
|
24
|
-
"items": {
|
|
25
|
-
"type": "string"
|
|
26
|
-
},
|
|
27
|
-
"default": ["node_modules", "dist", ".idea", ".git", ".cache"],
|
|
28
|
-
"description": "Globs of files to exclude, relative to the project root."
|
|
29
|
-
},
|
|
30
|
-
"setupFile": {
|
|
31
|
-
"type": "string",
|
|
32
|
-
"description": "The path to the setup file.",
|
|
33
|
-
"default": "src/test-setup.ts"
|
|
34
|
-
},
|
|
35
|
-
"tsConfig": {
|
|
36
|
-
"type": "string",
|
|
37
|
-
"description": "The relative path to the TypeScript configuration file for running tests.",
|
|
38
|
-
"default": "tsconfig.spec.json"
|
|
39
|
-
},
|
|
40
|
-
"watch": {
|
|
41
|
-
"type": "boolean",
|
|
42
|
-
"default": false,
|
|
43
|
-
"description": "Run the tests in watch mode"
|
|
44
|
-
}
|
|
45
|
-
},
|
|
46
|
-
"additionalProperties": true
|
|
47
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi9wYWNrYWdlcy92aXRlc3QtYW5ndWxhci9zcmMvbGliL2J1aWxkZXJzL2J1aWxkL3V0aWxzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgdHlwZSBBbmd1bGFyTWVtb3J5T3V0cHV0RmlsZXMgPSBNYXA8XG4gIHN0cmluZyxcbiAgeyBjb250ZW50czogVWludDhBcnJheTsgaGFzaDogc3RyaW5nOyBzZXJ2YWJsZTogYm9vbGVhbiB9XG4+O1xuIl19
|
|
@@ -1,47 +0,0 @@
|
|
|
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
|
-
"ui": {
|
|
33
|
-
"description": "Run tests using Vitest UI Mode.",
|
|
34
|
-
"type": "boolean"
|
|
35
|
-
},
|
|
36
|
-
"coverage": {
|
|
37
|
-
"description": "Enable code coverage analysis.",
|
|
38
|
-
"type": "boolean"
|
|
39
|
-
},
|
|
40
|
-
"update": {
|
|
41
|
-
"description": "Update snapshot.",
|
|
42
|
-
"type": "boolean",
|
|
43
|
-
"aliases": ["u"]
|
|
44
|
-
}
|
|
45
|
-
},
|
|
46
|
-
"required": []
|
|
47
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../packages/vitest-angular-tools/src/index.ts"],"names":[],"mappings":";;;AAAA,kDAA0D;AAAjD,uGAAA,cAAc,OAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../../../packages/vitest-angular-tools/src/schematics/utils/index.ts"],"names":[],"mappings":";;;AAAA,qDAA2B;AAC3B,oDAA0B;AAC1B,sDAA4B;AAC5B,yDAA+B"}
|