@nuxt/test-utils 3.0.0-rc.12 → 3.0.0-rc.14
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/dist/index.d.ts +7 -4
- package/dist/index.mjs +42 -12
- package/dist/runtime/global-setup.d.ts +2 -0
- package/dist/runtime/global-setup.mjs +9 -0
- package/package.json +9 -8
package/dist/index.d.ts
CHANGED
|
@@ -2,13 +2,13 @@ import * as playwright from 'playwright';
|
|
|
2
2
|
import { Browser, BrowserContextOptions, LaunchOptions } from 'playwright';
|
|
3
3
|
import { NuxtConfig, Nuxt } from '@nuxt/schema';
|
|
4
4
|
import { ExecaChildProcess } from 'execa';
|
|
5
|
-
import { FetchOptions } from '
|
|
5
|
+
import { FetchOptions } from 'ofetch';
|
|
6
6
|
|
|
7
7
|
declare function createBrowser(): Promise<void>;
|
|
8
8
|
declare function getBrowser(): Promise<Browser>;
|
|
9
9
|
declare function createPage(path?: string, options?: BrowserContextOptions): Promise<playwright.Page>;
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
type TestRunner = 'vitest' | 'jest';
|
|
12
12
|
interface TestOptions {
|
|
13
13
|
testDir: string;
|
|
14
14
|
fixture: string;
|
|
@@ -50,6 +50,8 @@ declare function useTestContext(): TestContext;
|
|
|
50
50
|
declare function setTestContext(context: TestContext): TestContext;
|
|
51
51
|
declare function setTestContext(context?: TestContext): TestContext | undefined;
|
|
52
52
|
declare function isDev(): boolean;
|
|
53
|
+
declare function recoverContextFromEnv(): void;
|
|
54
|
+
declare function exposeContextToEnv(): void;
|
|
53
55
|
|
|
54
56
|
declare function mockFn(): Function | undefined;
|
|
55
57
|
declare function mockLogger(): Record<string, Function>;
|
|
@@ -72,14 +74,15 @@ declare const setupMaps: {
|
|
|
72
74
|
vitest: typeof setupVitest;
|
|
73
75
|
};
|
|
74
76
|
declare function createTest(options: Partial<TestOptions>): TestHooks;
|
|
75
|
-
declare function setup(options
|
|
77
|
+
declare function setup(options?: Partial<TestOptions>): Promise<void>;
|
|
76
78
|
|
|
77
79
|
interface RunTestOptions {
|
|
78
80
|
rootDir: string;
|
|
79
81
|
dev?: boolean;
|
|
80
82
|
watch?: boolean;
|
|
81
83
|
runner?: 'vitest';
|
|
84
|
+
globalSetup?: boolean;
|
|
82
85
|
}
|
|
83
86
|
declare function runTests(opts: RunTestOptions): Promise<void>;
|
|
84
87
|
|
|
85
|
-
export { $fetch, RunTestOptions, buildFixture, createBrowser, createPage, createTest, createTestContext, fetch, getBrowser, isDev, loadFixture, mockFn, mockLogger, runTests, setTestContext, setup, setupMaps, startServer, stopServer, url, useTestContext };
|
|
88
|
+
export { $fetch, RunTestOptions, TestContext, TestHooks, TestOptions, TestRunner, buildFixture, createBrowser, createPage, createTest, createTestContext, exposeContextToEnv, fetch, getBrowser, isDev, loadFixture, mockFn, mockLogger, recoverContextFromEnv, runTests, setTestContext, setup, setupMaps, startServer, stopServer, url, useTestContext };
|
package/dist/index.mjs
CHANGED
|
@@ -2,10 +2,12 @@ import { resolve } from 'node:path';
|
|
|
2
2
|
import defu from 'defu';
|
|
3
3
|
import { execa } from 'execa';
|
|
4
4
|
import { getRandomPort, waitForPort } from 'get-port-please';
|
|
5
|
-
import { fetch as fetch$1, $fetch as $fetch$1 } from '
|
|
5
|
+
import { fetch as fetch$1, $fetch as $fetch$1 } from 'ofetch';
|
|
6
6
|
import * as _kit from '@nuxt/kit';
|
|
7
7
|
import consola from 'consola';
|
|
8
8
|
import { promises, existsSync } from 'node:fs';
|
|
9
|
+
import { dirname, resolve as resolve$1 } from 'pathe';
|
|
10
|
+
import { fileURLToPath } from 'node:url';
|
|
9
11
|
|
|
10
12
|
let currentContext;
|
|
11
13
|
function createTestContext(options) {
|
|
@@ -29,6 +31,7 @@ function createTestContext(options) {
|
|
|
29
31
|
});
|
|
30
32
|
}
|
|
31
33
|
function useTestContext() {
|
|
34
|
+
recoverContextFromEnv();
|
|
32
35
|
if (!currentContext) {
|
|
33
36
|
throw new Error("No context is available. (Forgot calling setup or createContext?)");
|
|
34
37
|
}
|
|
@@ -42,6 +45,15 @@ function isDev() {
|
|
|
42
45
|
const ctx = useTestContext();
|
|
43
46
|
return ctx.options.dev;
|
|
44
47
|
}
|
|
48
|
+
function recoverContextFromEnv() {
|
|
49
|
+
if (!currentContext && process.env.NUXT_TEST_CONTEXT) {
|
|
50
|
+
setTestContext(JSON.parse(process.env.NUXT_TEST_CONTEXT || "{}"));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function exposeContextToEnv() {
|
|
54
|
+
const { options, browser, url } = currentContext;
|
|
55
|
+
process.env.NUXT_TEST_CONTEXT = JSON.stringify({ options, browser, url });
|
|
56
|
+
}
|
|
45
57
|
|
|
46
58
|
const kit$1 = _kit.default || _kit;
|
|
47
59
|
async function startServer() {
|
|
@@ -57,6 +69,7 @@ async function startServer() {
|
|
|
57
69
|
env: {
|
|
58
70
|
...process.env,
|
|
59
71
|
PORT: String(port),
|
|
72
|
+
NITRO_PORT: String(port),
|
|
60
73
|
NODE_ENV: "development"
|
|
61
74
|
}
|
|
62
75
|
});
|
|
@@ -80,6 +93,7 @@ async function startServer() {
|
|
|
80
93
|
env: {
|
|
81
94
|
...process.env,
|
|
82
95
|
PORT: String(port),
|
|
96
|
+
NITRO_PORT: String(port),
|
|
83
97
|
NODE_ENV: "test"
|
|
84
98
|
}
|
|
85
99
|
});
|
|
@@ -154,7 +168,7 @@ function mockLogger() {
|
|
|
154
168
|
|
|
155
169
|
const kit = _kit.default || _kit;
|
|
156
170
|
const isNuxtApp = (dir) => {
|
|
157
|
-
return existsSync(dir) && (existsSync(resolve(dir, "pages")) || existsSync(resolve(dir, "nuxt.config.js")) || existsSync(resolve(dir, "nuxt.config.ts")));
|
|
171
|
+
return existsSync(dir) && (existsSync(resolve(dir, "pages")) || existsSync(resolve(dir, "nuxt.config.js")) || existsSync(resolve(dir, "nuxt.config.mjs")) || existsSync(resolve(dir, "nuxt.config.cjs")) || existsSync(resolve(dir, "nuxt.config.ts")));
|
|
158
172
|
};
|
|
159
173
|
const resolveRootDir = () => {
|
|
160
174
|
const { options } = useTestContext();
|
|
@@ -267,14 +281,18 @@ function createTest(options) {
|
|
|
267
281
|
ctx
|
|
268
282
|
};
|
|
269
283
|
}
|
|
270
|
-
async function setup(options) {
|
|
284
|
+
async function setup(options = {}) {
|
|
271
285
|
const hooks = createTest(options);
|
|
272
286
|
const setupFn = setupMaps[hooks.ctx.options.runner];
|
|
273
287
|
await setupFn(hooks);
|
|
274
288
|
}
|
|
275
289
|
|
|
290
|
+
const distDir = dirname(fileURLToPath(import.meta.url));
|
|
291
|
+
resolve$1(distDir, "..");
|
|
292
|
+
|
|
276
293
|
const RunTestDefaults = {
|
|
277
|
-
runner: "vitest"
|
|
294
|
+
runner: "vitest",
|
|
295
|
+
globalSetup: true
|
|
278
296
|
};
|
|
279
297
|
async function runTests(opts) {
|
|
280
298
|
opts = { ...RunTestDefaults, ...opts };
|
|
@@ -284,8 +302,10 @@ async function runTests(opts) {
|
|
|
284
302
|
if (opts.dev) {
|
|
285
303
|
process.env.NUXT_TEST_DEV = "true";
|
|
286
304
|
}
|
|
287
|
-
|
|
288
|
-
const
|
|
305
|
+
process.env.NUXT_TEST_OPTIONS = JSON.stringify(opts);
|
|
306
|
+
const { startVitest } = await import('vitest/node');
|
|
307
|
+
const succeeded = await startVitest(
|
|
308
|
+
"test",
|
|
289
309
|
[],
|
|
290
310
|
{
|
|
291
311
|
root: opts.rootDir,
|
|
@@ -297,16 +317,26 @@ async function runTests(opts) {
|
|
|
297
317
|
{
|
|
298
318
|
esbuild: {
|
|
299
319
|
tsconfigRaw: "{}"
|
|
320
|
+
},
|
|
321
|
+
test: {
|
|
322
|
+
dir: opts.rootDir,
|
|
323
|
+
deps: {
|
|
324
|
+
inline: [
|
|
325
|
+
distDir,
|
|
326
|
+
"@nuxt/test-utils",
|
|
327
|
+
"@nuxt/test-utils-edge"
|
|
328
|
+
]
|
|
329
|
+
},
|
|
330
|
+
globals: true,
|
|
331
|
+
globalSetup: [
|
|
332
|
+
...opts.globalSetup ? [resolve$1(distDir, "./runtime/global-setup")] : []
|
|
333
|
+
]
|
|
300
334
|
}
|
|
301
335
|
}
|
|
302
|
-
|
|
303
|
-
if (startVitest.length >= 4) {
|
|
304
|
-
args.unshift("test");
|
|
305
|
-
}
|
|
306
|
-
const succeeded = await startVitest(...args);
|
|
336
|
+
);
|
|
307
337
|
if (!succeeded) {
|
|
308
338
|
process.exit(1);
|
|
309
339
|
}
|
|
310
340
|
}
|
|
311
341
|
|
|
312
|
-
export { $fetch, buildFixture, createBrowser, createPage, createTest, createTestContext, fetch, getBrowser, isDev, loadFixture, mockFn, mockLogger, runTests, setTestContext, setup, setupMaps, startServer, stopServer, url, useTestContext };
|
|
342
|
+
export { $fetch, buildFixture, createBrowser, createPage, createTest, createTestContext, exposeContextToEnv, fetch, getBrowser, isDev, loadFixture, mockFn, mockLogger, recoverContextFromEnv, runTests, setTestContext, setup, setupMaps, startServer, stopServer, url, useTestContext };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { createTest, exposeContextToEnv } from "@nuxt/test-utils";
|
|
2
|
+
const hooks = createTest(JSON.parse(process.env.NUXT_TEST_OPTIONS || "{}"));
|
|
3
|
+
export const setup = async () => {
|
|
4
|
+
await hooks.setup();
|
|
5
|
+
exposeContextToEnv();
|
|
6
|
+
};
|
|
7
|
+
export const teardown = async () => {
|
|
8
|
+
await hooks.afterAll();
|
|
9
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nuxt/test-utils",
|
|
3
|
-
"version": "3.0.0-rc.
|
|
3
|
+
"version": "3.0.0-rc.14",
|
|
4
4
|
"repository": "nuxt/framework",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -10,25 +10,26 @@
|
|
|
10
10
|
"dist"
|
|
11
11
|
],
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@nuxt/kit": "3.0.0-rc.
|
|
14
|
-
"@nuxt/schema": "3.0.0-rc.
|
|
13
|
+
"@nuxt/kit": "3.0.0-rc.14",
|
|
14
|
+
"@nuxt/schema": "3.0.0-rc.14",
|
|
15
15
|
"consola": "^2.15.3",
|
|
16
|
-
"defu": "^6.1.
|
|
16
|
+
"defu": "^6.1.1",
|
|
17
17
|
"execa": "^6.1.0",
|
|
18
18
|
"get-port-please": "^2.6.1",
|
|
19
19
|
"jiti": "^1.16.0",
|
|
20
|
-
"
|
|
20
|
+
"ofetch": "^1.0.0",
|
|
21
|
+
"pathe": "^1.0.0"
|
|
21
22
|
},
|
|
22
23
|
"devDependencies": {
|
|
23
24
|
"playwright": "^1.27.1",
|
|
24
25
|
"unbuild": "latest",
|
|
25
|
-
"vitest": "
|
|
26
|
+
"vitest": "^0.25.2"
|
|
26
27
|
},
|
|
27
28
|
"peerDependencies": {
|
|
28
|
-
"vue": "^3.2.
|
|
29
|
+
"vue": "^3.2.45"
|
|
29
30
|
},
|
|
30
31
|
"engines": {
|
|
31
|
-
"node": "^14.16.0 || ^16.10.0 || ^17.0.0 || ^18.0.0"
|
|
32
|
+
"node": "^14.16.0 || ^16.10.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
|
|
32
33
|
},
|
|
33
34
|
"scripts": {}
|
|
34
35
|
}
|