@nuxt/test-utils 3.0.0-rc.13 → 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 +37 -6
- package/dist/runtime/global-setup.d.ts +2 -0
- package/dist/runtime/global-setup.mjs +9 -0
- package/package.json +8 -7
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() {
|
|
@@ -156,7 +168,7 @@ function mockLogger() {
|
|
|
156
168
|
|
|
157
169
|
const kit = _kit.default || _kit;
|
|
158
170
|
const isNuxtApp = (dir) => {
|
|
159
|
-
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")));
|
|
160
172
|
};
|
|
161
173
|
const resolveRootDir = () => {
|
|
162
174
|
const { options } = useTestContext();
|
|
@@ -269,14 +281,18 @@ function createTest(options) {
|
|
|
269
281
|
ctx
|
|
270
282
|
};
|
|
271
283
|
}
|
|
272
|
-
async function setup(options) {
|
|
284
|
+
async function setup(options = {}) {
|
|
273
285
|
const hooks = createTest(options);
|
|
274
286
|
const setupFn = setupMaps[hooks.ctx.options.runner];
|
|
275
287
|
await setupFn(hooks);
|
|
276
288
|
}
|
|
277
289
|
|
|
290
|
+
const distDir = dirname(fileURLToPath(import.meta.url));
|
|
291
|
+
resolve$1(distDir, "..");
|
|
292
|
+
|
|
278
293
|
const RunTestDefaults = {
|
|
279
|
-
runner: "vitest"
|
|
294
|
+
runner: "vitest",
|
|
295
|
+
globalSetup: true
|
|
280
296
|
};
|
|
281
297
|
async function runTests(opts) {
|
|
282
298
|
opts = { ...RunTestDefaults, ...opts };
|
|
@@ -286,7 +302,8 @@ async function runTests(opts) {
|
|
|
286
302
|
if (opts.dev) {
|
|
287
303
|
process.env.NUXT_TEST_DEV = "true";
|
|
288
304
|
}
|
|
289
|
-
|
|
305
|
+
process.env.NUXT_TEST_OPTIONS = JSON.stringify(opts);
|
|
306
|
+
const { startVitest } = await import('vitest/node');
|
|
290
307
|
const succeeded = await startVitest(
|
|
291
308
|
"test",
|
|
292
309
|
[],
|
|
@@ -300,6 +317,20 @@ async function runTests(opts) {
|
|
|
300
317
|
{
|
|
301
318
|
esbuild: {
|
|
302
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
|
+
]
|
|
303
334
|
}
|
|
304
335
|
}
|
|
305
336
|
);
|
|
@@ -308,4 +339,4 @@ async function runTests(opts) {
|
|
|
308
339
|
}
|
|
309
340
|
}
|
|
310
341
|
|
|
311
|
-
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,22 +10,23 @@
|
|
|
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": "^0.
|
|
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
32
|
"node": "^14.16.0 || ^16.10.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
|