@ngrs/vite 0.0.0-stage → 0.2.0
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 +30 -2
- package/package.json +63 -4
- package/src/index.cjs +24 -0
- package/src/index.d.ts +132 -0
- package/src/index.mjs +1465 -0
- package/src/testing.cjs +17 -0
- package/src/testing.d.ts +52 -0
- package/src/testing.mjs +295 -0
package/src/testing.cjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
let esmPromise;
|
|
4
|
+
function loadTestingEsm() {
|
|
5
|
+
if (!esmPromise) {
|
|
6
|
+
esmPromise = import('./testing.mjs');
|
|
7
|
+
}
|
|
8
|
+
return esmPromise;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function angularTesting(options = {}) {
|
|
12
|
+
return [loadTestingEsm().then((m) => m.angularTesting(options))];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
module.exports = angularTesting;
|
|
16
|
+
module.exports.angularTesting = angularTesting;
|
|
17
|
+
module.exports.default = angularTesting;
|
package/src/testing.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
import type { AngularVitePluginOptions } from './index.js';
|
|
3
|
+
|
|
4
|
+
export interface AngularTestingPluginOptions extends AngularVitePluginOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Project root directory relative to `workspaceRoot` (defaults to `'.'`).
|
|
7
|
+
*/
|
|
8
|
+
projectRoot?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Polyfills to load before Angular TestBed initialization (e.g. `['zone.js']`).
|
|
11
|
+
* Automatically appends `'zone.js/testing'` when `'zone.js'` is included.
|
|
12
|
+
*/
|
|
13
|
+
polyfills?: string[];
|
|
14
|
+
/**
|
|
15
|
+
* Additional Vitest setup files to run after Angular TestBed initialization.
|
|
16
|
+
*/
|
|
17
|
+
setupFiles?: string[];
|
|
18
|
+
/**
|
|
19
|
+
* Optional file default-exporting an array of global Angular `Provider`s for `TestBed`.
|
|
20
|
+
*/
|
|
21
|
+
providersFile?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Automatically initialize Angular `TestBed` (`BrowserTestingModule` + `platformBrowserTesting()`).
|
|
24
|
+
* Defaults to `true`.
|
|
25
|
+
*/
|
|
26
|
+
initTestBed?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Destroy Angular test fixtures after each test (`teardown.destroyAfterEach`). Defaults to `true`.
|
|
29
|
+
*/
|
|
30
|
+
teardown?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Vitest test environment (`'happy-dom' | 'node'`). Defaults to `'happy-dom'`.
|
|
33
|
+
*/
|
|
34
|
+
environment?: 'happy-dom' | 'node' | (string & {});
|
|
35
|
+
/**
|
|
36
|
+
* Isolate each test file in its own environment (`false` by default to match `@angular/build:unit-test`).
|
|
37
|
+
*/
|
|
38
|
+
isolate?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Test include globs.
|
|
41
|
+
*/
|
|
42
|
+
include?: string[];
|
|
43
|
+
/**
|
|
44
|
+
* Test exclude globs.
|
|
45
|
+
*/
|
|
46
|
+
exclude?: string[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export declare const VIRTUAL_INIT_COMPILER_FILE: string;
|
|
50
|
+
export declare const VIRTUAL_INIT_TESTBED_FILE: string;
|
|
51
|
+
export declare function angularTesting(options?: AngularTestingPluginOptions): Plugin[];
|
|
52
|
+
export default angularTesting;
|
package/src/testing.mjs
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { angular } from './index.mjs';
|
|
5
|
+
|
|
6
|
+
export const VIRTUAL_INIT_COMPILER_FILE = '__ngrs_init_compiler__.mjs';
|
|
7
|
+
export const VIRTUAL_INIT_TESTBED_FILE = '__ngrs_init_testbed__.mjs';
|
|
8
|
+
const RESOLVED_INIT_COMPILER_ID = '\0ngrs:vitest-init-compiler.mjs';
|
|
9
|
+
const RESOLVED_INIT_TESTBED_ID = '\0ngrs:vitest-init-testbed.mjs';
|
|
10
|
+
|
|
11
|
+
function toPosixPath(p) {
|
|
12
|
+
return p.replace(/\\/g, '/');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function tryResolveFromDirs(specifier, dirs) {
|
|
16
|
+
for (const dir of dirs) {
|
|
17
|
+
try {
|
|
18
|
+
const req = createRequire(path.join(dir, 'package.json'));
|
|
19
|
+
return toPosixPath(req.resolve(specifier));
|
|
20
|
+
} catch {
|
|
21
|
+
// Try next directory
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function ensureSpecTsconfig(rootDir, workspaceRoot, explicitTsconfig) {
|
|
28
|
+
if (explicitTsconfig) {
|
|
29
|
+
return path.resolve(workspaceRoot, explicitTsconfig);
|
|
30
|
+
}
|
|
31
|
+
const projectSpec = path.resolve(rootDir, 'tsconfig.spec.json');
|
|
32
|
+
if (fs.existsSync(projectSpec)) {
|
|
33
|
+
return projectSpec;
|
|
34
|
+
}
|
|
35
|
+
const wsSpec = path.resolve(workspaceRoot, 'tsconfig.spec.json');
|
|
36
|
+
if (fs.existsSync(wsSpec)) {
|
|
37
|
+
return wsSpec;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Synthesize a spec tsconfig in `.angular/cache/ngrs-vitest/` if only `tsconfig.app.json` /
|
|
41
|
+
// `tsconfig.json` exists so `*.spec.ts` & `*.test.ts` are always part of the Rust program!
|
|
42
|
+
const baseCandidates = [
|
|
43
|
+
path.resolve(rootDir, 'tsconfig.json'),
|
|
44
|
+
path.resolve(rootDir, 'tsconfig.app.json'),
|
|
45
|
+
path.resolve(workspaceRoot, 'tsconfig.base.json'),
|
|
46
|
+
path.resolve(workspaceRoot, 'tsconfig.json'),
|
|
47
|
+
];
|
|
48
|
+
const baseTsconfig = baseCandidates.find((p) => fs.existsSync(p));
|
|
49
|
+
if (!baseTsconfig) {
|
|
50
|
+
return path.resolve(rootDir, 'tsconfig.json');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const cacheDir = path.join(workspaceRoot, '.angular', 'cache', 'ngrs-vitest');
|
|
54
|
+
fs.mkdirSync(cacheDir, { recursive: true });
|
|
55
|
+
const generatedPath = path.join(
|
|
56
|
+
cacheDir,
|
|
57
|
+
`tsconfig.spec.${path.basename(rootDir) || 'root'}.json`,
|
|
58
|
+
);
|
|
59
|
+
const relExtends = toPosixPath(path.relative(cacheDir, baseTsconfig));
|
|
60
|
+
const relRoot = toPosixPath(path.relative(cacheDir, rootDir)) || '.';
|
|
61
|
+
const generatedJson = {
|
|
62
|
+
extends: relExtends.startsWith('.') ? relExtends : `./${relExtends}`,
|
|
63
|
+
compilerOptions: {
|
|
64
|
+
outDir: './out-tsc/spec',
|
|
65
|
+
},
|
|
66
|
+
files: [],
|
|
67
|
+
include: [
|
|
68
|
+
`${relRoot}/**/*.d.ts`,
|
|
69
|
+
`${relRoot}/**/*.spec.ts`,
|
|
70
|
+
`${relRoot}/**/*.test.ts`,
|
|
71
|
+
],
|
|
72
|
+
};
|
|
73
|
+
fs.writeFileSync(generatedPath, JSON.stringify(generatedJson, null, 2) + '\n', 'utf8');
|
|
74
|
+
return generatedPath;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function createInitCompilerCode(polyfills, searchDirs) {
|
|
78
|
+
const lines = [];
|
|
79
|
+
for (const spec of polyfills) {
|
|
80
|
+
const localCandidate = path.resolve(searchDirs[0], spec);
|
|
81
|
+
if (fs.existsSync(localCandidate) && fs.statSync(localCandidate).isFile()) {
|
|
82
|
+
lines.push(`import ${JSON.stringify(toPosixPath(localCandidate))};`);
|
|
83
|
+
} else {
|
|
84
|
+
const resolved = tryResolveFromDirs(spec, searchDirs) || spec;
|
|
85
|
+
lines.push(`import ${JSON.stringify(resolved)};`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
lines.push(`import '@angular/compiler';`);
|
|
89
|
+
return lines.join('\n') + '\n';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function createInitTestBedCode({ providersFile, teardown = true, hasLocalize = false }) {
|
|
93
|
+
const providersStmt = providersFile
|
|
94
|
+
? `import providers from ${JSON.stringify(toPosixPath(providersFile))};`
|
|
95
|
+
: `const providers = [];`;
|
|
96
|
+
|
|
97
|
+
return `\
|
|
98
|
+
${hasLocalize ? "import '@angular/localize/init';\n" : ''}\
|
|
99
|
+
import * as i0 from '@angular/core';
|
|
100
|
+
import { getTestBed, ɵgetCleanupHook as getCleanupHook, TestComponentRenderer } from '@angular/core/testing';
|
|
101
|
+
import { BrowserTestingModule, platformBrowserTesting } from '@angular/platform-browser/testing';
|
|
102
|
+
import { ɵgetDOM } from '@angular/common';
|
|
103
|
+
import { afterEach, beforeEach } from 'vitest';
|
|
104
|
+
${providersStmt}
|
|
105
|
+
|
|
106
|
+
beforeEach(getCleanupHook(false));
|
|
107
|
+
afterEach(getCleanupHook(true));
|
|
108
|
+
|
|
109
|
+
class DynamicDOMTestComponentRenderer extends TestComponentRenderer {
|
|
110
|
+
insertRootElement(rootElId, tagName = 'div') {
|
|
111
|
+
this.removeAllRootElements();
|
|
112
|
+
const dom = ɵgetDOM();
|
|
113
|
+
const doc = dom.getDefaultDocument();
|
|
114
|
+
if (doc && doc.body) {
|
|
115
|
+
const rootElement = doc.createElement(tagName);
|
|
116
|
+
rootElement.setAttribute('id', rootElId);
|
|
117
|
+
doc.body.appendChild(rootElement);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
removeAllRootElements() {
|
|
122
|
+
const dom = ɵgetDOM();
|
|
123
|
+
const doc = dom.getDefaultDocument();
|
|
124
|
+
if (doc && typeof doc.querySelectorAll === 'function') {
|
|
125
|
+
const oldRoots = doc.querySelectorAll('[id^=root]');
|
|
126
|
+
for (let i = 0; i < oldRoots.length; i++) {
|
|
127
|
+
dom.remove(oldRoots[i]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const ANGULAR_TESTBED_SETUP = Symbol.for('@angular/cli/testbed-setup');
|
|
134
|
+
if (!globalThis[ANGULAR_TESTBED_SETUP]) {
|
|
135
|
+
globalThis[ANGULAR_TESTBED_SETUP] = true;
|
|
136
|
+
|
|
137
|
+
class NgrsTestProvidersModule {}
|
|
138
|
+
NgrsTestProvidersModule.ɵfac = function NgrsTestProvidersModule_Factory(t) {
|
|
139
|
+
return new (t || NgrsTestProvidersModule)();
|
|
140
|
+
};
|
|
141
|
+
NgrsTestProvidersModule.ɵmod = i0.ɵɵdefineNgModule({ type: NgrsTestProvidersModule });
|
|
142
|
+
NgrsTestProvidersModule.ɵinj = i0.ɵɵdefineInjector({
|
|
143
|
+
providers: [
|
|
144
|
+
...(typeof Zone !== 'undefined' && typeof i0.provideZoneChangeDetection === 'function'
|
|
145
|
+
? [i0.provideZoneChangeDetection()]
|
|
146
|
+
: []),
|
|
147
|
+
...(Array.isArray(providers) ? providers : []),
|
|
148
|
+
{ provide: TestComponentRenderer, useClass: DynamicDOMTestComponentRenderer },
|
|
149
|
+
],
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
getTestBed().initTestEnvironment(
|
|
153
|
+
[BrowserTestingModule, NgrsTestProvidersModule],
|
|
154
|
+
platformBrowserTesting(),
|
|
155
|
+
{
|
|
156
|
+
errorOnUnknownElements: true,
|
|
157
|
+
errorOnUnknownProperties: true,
|
|
158
|
+
${teardown === false ? 'teardown: { destroyAfterEach: false },' : ''}
|
|
159
|
+
},
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
`;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* First-class Vitest plugin for Angular (`@ngrs/vite/testing`).
|
|
167
|
+
* Configures Angular `TestBed` (`BrowserTestingModule` + `happy-dom` + zoneless / `zone.js/testing`),
|
|
168
|
+
* compiles `*.spec.ts` + components/templates/SCSS via native Rust `NapiProjectCompiler`, and links
|
|
169
|
+
* all Partial-Ivy `node_modules` packages via native Rust Ivy Linker.
|
|
170
|
+
*
|
|
171
|
+
* @param {import('./testing.js').AngularTestingPluginOptions} [options]
|
|
172
|
+
* @returns {import('vite').Plugin[]}
|
|
173
|
+
*/
|
|
174
|
+
export function angularTesting(options = {}) {
|
|
175
|
+
const workspaceRoot = path.resolve(options.workspaceRoot || process.cwd());
|
|
176
|
+
const projectRoot = path.resolve(workspaceRoot, options.projectRoot || '.');
|
|
177
|
+
const searchDirs = [projectRoot, workspaceRoot, process.cwd()];
|
|
178
|
+
|
|
179
|
+
let rawPolyfills = Array.isArray(options.polyfills) ? [...options.polyfills] : [];
|
|
180
|
+
if (rawPolyfills.includes('zone.js') && !rawPolyfills.includes('zone.js/testing')) {
|
|
181
|
+
rawPolyfills.push('zone.js/testing');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const hasLocalize = Boolean(tryResolveFromDirs('@angular/localize/init', searchDirs));
|
|
185
|
+
const resolvedProvidersFile = options.providersFile
|
|
186
|
+
? path.resolve(workspaceRoot, options.providersFile)
|
|
187
|
+
: undefined;
|
|
188
|
+
const resolvedTsconfig = ensureSpecTsconfig(projectRoot, workspaceRoot, options.tsconfig);
|
|
189
|
+
|
|
190
|
+
const initCompilerCode = createInitCompilerCode(rawPolyfills, searchDirs);
|
|
191
|
+
const initTestBedCode = createInitTestBedCode({
|
|
192
|
+
providersFile: resolvedProvidersFile,
|
|
193
|
+
teardown: options.teardown ?? true,
|
|
194
|
+
hasLocalize,
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
/** @type {import('vite').Plugin} */
|
|
198
|
+
const setupPlugin = {
|
|
199
|
+
name: 'ngrs:vitest-setup',
|
|
200
|
+
enforce: 'pre',
|
|
201
|
+
|
|
202
|
+
config(userConfig) {
|
|
203
|
+
const existingTest = userConfig?.test || {};
|
|
204
|
+
const userSetupRaw = existingTest.setupFiles;
|
|
205
|
+
const userSetupList = Array.isArray(userSetupRaw)
|
|
206
|
+
? userSetupRaw
|
|
207
|
+
: typeof userSetupRaw === 'string'
|
|
208
|
+
? [userSetupRaw]
|
|
209
|
+
: [];
|
|
210
|
+
const extraSetup = (options.setupFiles || []).map((f) =>
|
|
211
|
+
toPosixPath(path.resolve(workspaceRoot, f)),
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
const mergedSetup =
|
|
215
|
+
options.initTestBed === false
|
|
216
|
+
? [...extraSetup, ...userSetupList]
|
|
217
|
+
: [
|
|
218
|
+
VIRTUAL_INIT_COMPILER_FILE,
|
|
219
|
+
VIRTUAL_INIT_TESTBED_FILE,
|
|
220
|
+
...extraSetup,
|
|
221
|
+
...userSetupList,
|
|
222
|
+
];
|
|
223
|
+
|
|
224
|
+
return {
|
|
225
|
+
resolve: {
|
|
226
|
+
mainFields: ['es2020', 'module', 'main'],
|
|
227
|
+
conditions: ['es2015', 'es2020', 'module'],
|
|
228
|
+
},
|
|
229
|
+
test: {
|
|
230
|
+
globals: existingTest.globals ?? true,
|
|
231
|
+
isolate: existingTest.isolate ?? options.isolate ?? false,
|
|
232
|
+
environment: existingTest.environment ?? options.environment ?? 'happy-dom',
|
|
233
|
+
fsModuleCache: existingTest.fsModuleCache ?? true,
|
|
234
|
+
fsModuleCachePath:
|
|
235
|
+
existingTest.fsModuleCachePath ??
|
|
236
|
+
path.join(workspaceRoot, '.angular', 'cache', 'ngrs-vitest', 'module-cache'),
|
|
237
|
+
sequence: {
|
|
238
|
+
...(existingTest.sequence || {}),
|
|
239
|
+
setupFiles: 'list',
|
|
240
|
+
},
|
|
241
|
+
setupFiles: mergedSetup,
|
|
242
|
+
...(options.include ? { include: options.include } : {}),
|
|
243
|
+
...(options.exclude ? { exclude: options.exclude } : {}),
|
|
244
|
+
server: {
|
|
245
|
+
deps: {
|
|
246
|
+
inline: [
|
|
247
|
+
/^(?!vitest(?:\/|$)|@vitest\/|happy-dom(?:\/|$)|playwright(?:\/|$)|@playwright\/).*/,
|
|
248
|
+
],
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
},
|
|
254
|
+
|
|
255
|
+
resolveId(id) {
|
|
256
|
+
const clean = toPosixPath(id.split('?')[0]);
|
|
257
|
+
if (
|
|
258
|
+
clean === VIRTUAL_INIT_COMPILER_FILE ||
|
|
259
|
+
clean.endsWith(`/${VIRTUAL_INIT_COMPILER_FILE}`)
|
|
260
|
+
) {
|
|
261
|
+
return RESOLVED_INIT_COMPILER_ID;
|
|
262
|
+
}
|
|
263
|
+
if (clean === VIRTUAL_INIT_TESTBED_FILE || clean.endsWith(`/${VIRTUAL_INIT_TESTBED_FILE}`)) {
|
|
264
|
+
return RESOLVED_INIT_TESTBED_ID;
|
|
265
|
+
}
|
|
266
|
+
return null;
|
|
267
|
+
},
|
|
268
|
+
|
|
269
|
+
load(id) {
|
|
270
|
+
if (id === RESOLVED_INIT_COMPILER_ID) {
|
|
271
|
+
return initCompilerCode;
|
|
272
|
+
}
|
|
273
|
+
if (id === RESOLVED_INIT_TESTBED_ID) {
|
|
274
|
+
return initTestBedCode;
|
|
275
|
+
}
|
|
276
|
+
return null;
|
|
277
|
+
},
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
return [
|
|
281
|
+
setupPlugin,
|
|
282
|
+
...angular({
|
|
283
|
+
...options,
|
|
284
|
+
tsconfig: resolvedTsconfig,
|
|
285
|
+
workspaceRoot,
|
|
286
|
+
test: true,
|
|
287
|
+
hmr: false,
|
|
288
|
+
deferTypecheck: false,
|
|
289
|
+
supportTestBed: true,
|
|
290
|
+
supportJitMode: true,
|
|
291
|
+
}),
|
|
292
|
+
];
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export default angularTesting;
|