vitest-browser-angular 0.0.2 → 0.0.4
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 +88 -19
- package/dist/index.d.mts +10 -0
- package/dist/index.mjs +15 -0
- package/dist/{index.d.ts → pure-DuuxgQMT.d.mts} +13 -11
- package/dist/pure-J6VXh4xd.mjs +39 -0
- package/dist/pure.d.mts +2 -0
- package/dist/pure.mjs +3 -0
- package/dist/{setup-RnZmQ_2I.js → setup-CcdUOv69.mjs} +1 -1
- package/dist/setup-zones.d.mts +2 -0
- package/dist/{setup-zones.js → setup-zones.mjs} +1 -1
- package/dist/setup.mjs +3 -0
- package/package.json +13 -13
- package/dist/index.js +0 -48
- package/dist/setup-zones.d.ts +0 -3
- package/dist/setup.js +0 -3
- /package/dist/{setup.d.ts → setup.d.mts} +0 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# vitest-browser-angular
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Render Angular components in VItest Browser Mode.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -13,6 +13,8 @@ pnpm add -D vitest-browser-angular
|
|
|
13
13
|
```ts
|
|
14
14
|
// vitest.config.ts
|
|
15
15
|
|
|
16
|
+
import { playwright } from '@vitest/browser-playwright';
|
|
17
|
+
|
|
16
18
|
export default defineConfig({
|
|
17
19
|
test: {
|
|
18
20
|
globals: true,
|
|
@@ -22,7 +24,7 @@ export default defineConfig({
|
|
|
22
24
|
|
|
23
25
|
browser: {
|
|
24
26
|
enabled: true,
|
|
25
|
-
provider:
|
|
27
|
+
provider: playwright(),
|
|
26
28
|
instances: [{ browser: 'chromium' }],
|
|
27
29
|
},
|
|
28
30
|
},
|
|
@@ -36,8 +38,8 @@ TBD
|
|
|
36
38
|
## Usage
|
|
37
39
|
|
|
38
40
|
```ts
|
|
39
|
-
|
|
40
41
|
import { test, expect } from 'vitest-browser-angular';
|
|
42
|
+
import { render } from 'vitest-browser-angular';
|
|
41
43
|
|
|
42
44
|
@Component({
|
|
43
45
|
template: '<h1>{{ title }}</h1>',
|
|
@@ -46,34 +48,102 @@ export class HelloWorldComponent {
|
|
|
46
48
|
title = 'Hello World';
|
|
47
49
|
}
|
|
48
50
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const { component } = await mount(HelloWorldComponent);
|
|
51
|
+
test('render', async () => {
|
|
52
|
+
const { component } = await render(HelloWorldComponent);
|
|
52
53
|
await expect.element(component).toHaveTextContent('Hello World');
|
|
53
54
|
});
|
|
54
55
|
```
|
|
55
56
|
|
|
56
|
-
##
|
|
57
|
+
## Routing
|
|
58
|
+
|
|
59
|
+
### Simple Routing
|
|
57
60
|
|
|
58
|
-
|
|
61
|
+
Enable routing with `withRouting: true` for components that use routing features but don't require specific route configuration:
|
|
59
62
|
|
|
60
63
|
```ts
|
|
61
|
-
import { test
|
|
64
|
+
import { test, expect } from 'vitest';
|
|
65
|
+
import { render } from 'vitest-browser-angular';
|
|
66
|
+
import { Component } from '@angular/core';
|
|
67
|
+
import { RouterLink, RouterOutlet } from '@angular/router';
|
|
68
|
+
|
|
69
|
+
@Component({
|
|
70
|
+
template: `
|
|
71
|
+
<nav>
|
|
72
|
+
<a routerLink="/home">Home</a>
|
|
73
|
+
<a routerLink="/about">About</a>
|
|
74
|
+
</nav>
|
|
75
|
+
<router-outlet></router-outlet>
|
|
76
|
+
`,
|
|
77
|
+
imports: [RouterLink, RouterOutlet],
|
|
78
|
+
})
|
|
79
|
+
export class RoutedComponent {}
|
|
62
80
|
|
|
63
|
-
|
|
81
|
+
test('render with simple routing', async () => {
|
|
82
|
+
const { component } = await render(RoutedComponent, {
|
|
83
|
+
withRouting: true,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
await expect.element(component).toHaveTextContent('Home');
|
|
87
|
+
await expect.element(component).toHaveTextContent('About');
|
|
88
|
+
});
|
|
64
89
|
```
|
|
65
90
|
|
|
66
|
-
|
|
91
|
+
### Routing with Configuration
|
|
92
|
+
|
|
93
|
+
Configure specific routes and optionally set an initial route:
|
|
67
94
|
|
|
68
|
-
|
|
95
|
+
```ts
|
|
96
|
+
import { test, expect } from 'vitest';
|
|
97
|
+
import { render } from 'vitest-browser-angular';
|
|
98
|
+
import { Component, inject } from '@angular/core';
|
|
99
|
+
import { Router, RouterLink, RouterOutlet, Routes } from '@angular/router';
|
|
69
100
|
|
|
70
|
-
|
|
71
|
-
|
|
101
|
+
@Component({
|
|
102
|
+
template: '<h1>Home Page</h1>',
|
|
103
|
+
})
|
|
104
|
+
export class HomeComponent {}
|
|
72
105
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
106
|
+
@Component({
|
|
107
|
+
template: '<h1>About Page</h1>',
|
|
108
|
+
standalone: true,
|
|
109
|
+
})
|
|
110
|
+
export class AboutComponent {}
|
|
111
|
+
|
|
112
|
+
@Component({
|
|
113
|
+
template: `
|
|
114
|
+
<nav>
|
|
115
|
+
<a routerLink="/home">Home</a>
|
|
116
|
+
<a routerLink="/about">About</a>
|
|
117
|
+
</nav>
|
|
118
|
+
<router-outlet></router-outlet>
|
|
119
|
+
`,
|
|
120
|
+
imports: [RouterLink, RouterOutlet],
|
|
121
|
+
standalone: true,
|
|
122
|
+
})
|
|
123
|
+
export class AppComponent {
|
|
124
|
+
router = inject(Router);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const routes: Routes = [
|
|
128
|
+
{ path: 'home', component: HomeComponent },
|
|
129
|
+
{ path: 'about', component: AboutComponent },
|
|
130
|
+
{ path: '', redirectTo: '/home', pathMatch: 'full' },
|
|
131
|
+
];
|
|
132
|
+
|
|
133
|
+
test('render with route configuration', async () => {
|
|
134
|
+
const { component, router } = await render(AppComponent, {
|
|
135
|
+
withRouting: {
|
|
136
|
+
routes,
|
|
137
|
+
initialRoute: '/home',
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
await expect.element(component).toHaveTextContent('Home Page');
|
|
142
|
+
|
|
143
|
+
// Navigate programmatically
|
|
144
|
+
await router.navigate(['/about']);
|
|
145
|
+
await expect.element(component).toHaveTextContent('About Page');
|
|
146
|
+
});
|
|
77
147
|
```
|
|
78
148
|
|
|
79
149
|
## Contributing
|
|
@@ -92,7 +162,6 @@ Be kind to each other and please read our [code of conduct](CODE_OF_CONDUCT.md).
|
|
|
92
162
|
|
|
93
163
|
<br/>
|
|
94
164
|
|
|
95
|
-
|
|
96
165
|
## Credits
|
|
97
166
|
|
|
98
167
|
This project is inspired by the following projects:
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { i as render, n as RenderResult, r as cleanup, t as RenderFn } from "./pure-DuuxgQMT.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
declare module "vitest/browser" {
|
|
5
|
+
interface BrowserPage {
|
|
6
|
+
render: typeof render;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
export { type RenderFn, type RenderResult, cleanup, render };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { n as render, t as cleanup } from "./pure-J6VXh4xd.mjs";
|
|
2
|
+
import { beforeEach } from "vitest";
|
|
3
|
+
import { page } from "vitest/browser";
|
|
4
|
+
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
page.extend({
|
|
7
|
+
render,
|
|
8
|
+
[Symbol.for("vitest:component-cleanup")]: cleanup
|
|
9
|
+
});
|
|
10
|
+
beforeEach(async () => {
|
|
11
|
+
await cleanup(true);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { cleanup, render };
|
|
@@ -1,32 +1,34 @@
|
|
|
1
|
+
import { Locator } from "vitest/browser";
|
|
1
2
|
import { ComponentFixture } from "@angular/core/testing";
|
|
2
3
|
import { Router, Routes } from "@angular/router";
|
|
3
4
|
import { RouterTestingHarness } from "@angular/router/testing";
|
|
4
|
-
import { BrowserPage, Locator } from "@vitest/browser/context";
|
|
5
|
-
import * as vitest0 from "vitest";
|
|
6
5
|
import { EnvironmentProviders, Provider, Type } from "@angular/core";
|
|
7
|
-
export * from "vitest";
|
|
8
6
|
|
|
9
|
-
//#region src/
|
|
7
|
+
//#region src/pure.d.ts
|
|
10
8
|
interface RoutingConfig {
|
|
11
9
|
routes: Routes;
|
|
12
10
|
initialRoute?: string;
|
|
13
11
|
}
|
|
14
|
-
interface
|
|
12
|
+
interface RenderConfig {
|
|
15
13
|
withRouting?: RoutingConfig | boolean;
|
|
16
14
|
providers?: Array<Provider | EnvironmentProviders>;
|
|
17
15
|
imports?: unknown[];
|
|
18
16
|
}
|
|
19
|
-
interface
|
|
17
|
+
interface RenderResult<T> {
|
|
20
18
|
fixture: ComponentFixture<T>;
|
|
21
19
|
component: Locator;
|
|
22
20
|
componentClassInstance: T;
|
|
23
21
|
routerHarness?: RouterTestingHarness;
|
|
24
22
|
router?: Router;
|
|
25
23
|
}
|
|
26
|
-
type
|
|
27
|
-
declare
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
type RenderFn = <T>(component: Type<T>, config?: RenderConfig) => Promise<RenderResult<T>>;
|
|
25
|
+
declare function render<T>(componentClass: Type<T>, config?: RenderConfig): Promise<{
|
|
26
|
+
fixture: ComponentFixture<T>;
|
|
27
|
+
componentClassInstance: T;
|
|
28
|
+
component: Locator;
|
|
29
|
+
routerHarness?: RouterTestingHarness | undefined;
|
|
30
|
+
router?: Router | undefined;
|
|
30
31
|
}>;
|
|
32
|
+
declare function cleanup(shouldTearndown?: boolean): void;
|
|
31
33
|
//#endregion
|
|
32
|
-
export {
|
|
34
|
+
export { render as i, RenderResult as n, cleanup as r, RenderFn as t };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { page } from "vitest/browser";
|
|
2
|
+
import { TestBed, ɵgetCleanupHook } from "@angular/core/testing";
|
|
3
|
+
import { Router, provideRouter } from "@angular/router";
|
|
4
|
+
import { RouterTestingHarness } from "@angular/router/testing";
|
|
5
|
+
|
|
6
|
+
//#region src/pure.ts
|
|
7
|
+
async function render(componentClass, config) {
|
|
8
|
+
const imports = [componentClass, ...config?.imports || []];
|
|
9
|
+
const providers = [...config?.providers || []];
|
|
10
|
+
const renderResult = {};
|
|
11
|
+
if (config?.withRouting) {
|
|
12
|
+
const routes = typeof config.withRouting === "boolean" ? [] : config.withRouting.routes;
|
|
13
|
+
providers.push(provideRouter(routes));
|
|
14
|
+
}
|
|
15
|
+
TestBed.configureTestingModule({
|
|
16
|
+
imports,
|
|
17
|
+
providers
|
|
18
|
+
});
|
|
19
|
+
if (config?.withRouting) {
|
|
20
|
+
renderResult.routerHarness = await RouterTestingHarness.create(typeof config.withRouting === "boolean" ? void 0 : config.withRouting.initialRoute);
|
|
21
|
+
renderResult.router = TestBed.inject(Router);
|
|
22
|
+
}
|
|
23
|
+
const fixture = TestBed.createComponent(componentClass);
|
|
24
|
+
fixture.autoDetectChanges();
|
|
25
|
+
await fixture.whenStable();
|
|
26
|
+
const component = page.elementLocator(fixture.nativeElement);
|
|
27
|
+
return {
|
|
28
|
+
...renderResult,
|
|
29
|
+
fixture,
|
|
30
|
+
componentClassInstance: fixture.componentInstance,
|
|
31
|
+
component
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function cleanup(shouldTearndown = false) {
|
|
35
|
+
return ɵgetCleanupHook(shouldTearndown)();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
export { render as n, cleanup as t };
|
package/dist/pure.d.mts
ADDED
package/dist/pure.mjs
ADDED
package/dist/setup.mjs
ADDED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vitest-browser-angular",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"description": "Render Angular components in VItest Browser Mode",
|
|
5
5
|
"author": "Shai Reznik (HiRez.io)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/hirezio/vitest-browser-angular#readme",
|
|
@@ -21,17 +21,17 @@
|
|
|
21
21
|
"type": "module",
|
|
22
22
|
"exports": {
|
|
23
23
|
".": {
|
|
24
|
-
"types": "./dist/index.d.
|
|
25
|
-
"default": "./dist/index.
|
|
24
|
+
"types": "./dist/index.d.mts",
|
|
25
|
+
"default": "./dist/index.mjs"
|
|
26
26
|
},
|
|
27
27
|
"./setup-zones": {
|
|
28
|
-
"types": "./dist/setup-zones.d.
|
|
29
|
-
"default": "./dist/setup-zones.
|
|
28
|
+
"types": "./dist/setup-zones.d.mts",
|
|
29
|
+
"default": "./dist/setup-zones.mjs"
|
|
30
30
|
}
|
|
31
31
|
},
|
|
32
|
-
"main": "./dist/index.
|
|
33
|
-
"module": "./dist/index.
|
|
34
|
-
"types": "./dist/index.d.
|
|
32
|
+
"main": "./dist/index.mjs",
|
|
33
|
+
"module": "./dist/index.mjs",
|
|
34
|
+
"types": "./dist/index.d.mts",
|
|
35
35
|
"files": [
|
|
36
36
|
"*.d.ts",
|
|
37
37
|
"*.mjs",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"@angular/platform-browser": "^20.0.0",
|
|
48
48
|
"@angular/router": "^20.0.0",
|
|
49
49
|
"@vitest/browser": "^2.1.0 || ^3.0.0 || ^4.0.0-0",
|
|
50
|
-
"vitest": "^2.1.0 || ^3.0.0 || ^4.0.0
|
|
50
|
+
"vitest": "^2.1.0 || ^3.0.0 || ^4.0.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@analogjs/vitest-angular": "^1.20.2",
|
|
@@ -60,6 +60,7 @@
|
|
|
60
60
|
"@changesets/types": "^6.1.0",
|
|
61
61
|
"@eslint/js": "^9.35.0",
|
|
62
62
|
"@vitest/browser": "^3.2.0",
|
|
63
|
+
"@vitest/browser-playwright": "^4.0.0",
|
|
63
64
|
"bumpp": "^9.4.2",
|
|
64
65
|
"changelogithub": "^0.13.9",
|
|
65
66
|
"dotenv": "^17.2.2",
|
|
@@ -70,12 +71,11 @@
|
|
|
70
71
|
"prettier": "^3.6.2",
|
|
71
72
|
"pretty-quick": "^4.2.2",
|
|
72
73
|
"simple-git-hooks": "^2.13.1",
|
|
73
|
-
"tsdown": "^0.
|
|
74
|
-
"tsup": "^8.2.4",
|
|
74
|
+
"tsdown": "^0.16.2",
|
|
75
75
|
"tsx": "^4.17.0",
|
|
76
76
|
"typescript": "^5.5.4",
|
|
77
77
|
"typescript-eslint": "^8.42.0",
|
|
78
|
-
"vitest": "^
|
|
78
|
+
"vitest": "^4.0.0",
|
|
79
79
|
"zone.js": "^0.15.1",
|
|
80
80
|
"zx": "^8.1.4"
|
|
81
81
|
},
|
package/dist/index.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { TestBed, ɵgetCleanupHook } from "@angular/core/testing";
|
|
2
|
-
import { Router, provideRouter } from "@angular/router";
|
|
3
|
-
import { RouterTestingHarness } from "@angular/router/testing";
|
|
4
|
-
import { page } from "@vitest/browser/context";
|
|
5
|
-
import { test as test$1 } from "vitest";
|
|
6
|
-
|
|
7
|
-
export * from "vitest"
|
|
8
|
-
|
|
9
|
-
//#region src/index.ts
|
|
10
|
-
page.extend({ [Symbol.for("vitest:component-cleanup")]: ɵgetCleanupHook(false) });
|
|
11
|
-
const test = test$1.extend({
|
|
12
|
-
page: async ({}, use) => {
|
|
13
|
-
await use(page);
|
|
14
|
-
},
|
|
15
|
-
mount: async ({ page: page$1 }, use) => {
|
|
16
|
-
await use(async (componentClass, config) => {
|
|
17
|
-
const imports = [componentClass, ...config?.imports || []];
|
|
18
|
-
const providers = [...config?.providers || []];
|
|
19
|
-
const mountResult = {};
|
|
20
|
-
if (config?.withRouting) {
|
|
21
|
-
const routes = typeof config.withRouting === "boolean" ? [] : config.withRouting.routes;
|
|
22
|
-
providers.push(provideRouter(routes));
|
|
23
|
-
}
|
|
24
|
-
TestBed.configureTestingModule({
|
|
25
|
-
imports,
|
|
26
|
-
providers
|
|
27
|
-
});
|
|
28
|
-
if (config?.withRouting) {
|
|
29
|
-
const routerHarness = await RouterTestingHarness.create(typeof config.withRouting === "boolean" ? void 0 : config.withRouting.initialRoute);
|
|
30
|
-
mountResult.routerHarness = routerHarness;
|
|
31
|
-
mountResult.router = TestBed.inject(Router);
|
|
32
|
-
}
|
|
33
|
-
const fixture = TestBed.createComponent(componentClass);
|
|
34
|
-
fixture.autoDetectChanges();
|
|
35
|
-
await fixture.whenStable();
|
|
36
|
-
const component = page$1.elementLocator(fixture.nativeElement);
|
|
37
|
-
return {
|
|
38
|
-
...mountResult,
|
|
39
|
-
fixture,
|
|
40
|
-
componentClassInstance: fixture.componentInstance,
|
|
41
|
-
component
|
|
42
|
-
};
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
//#endregion
|
|
48
|
-
export { test };
|
package/dist/setup-zones.d.ts
DELETED
package/dist/setup.js
DELETED
|
File without changes
|