@nuxt/test-utils 3.3.2 → 3.4.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/dist/experimental.d.ts +7 -0
- package/dist/experimental.mjs +24 -0
- package/dist/index.mjs +8 -119
- package/dist/shared/test-utils.1e170326.mjs +125 -0
- package/package.json +12 -7
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is a function to render a component directly with the Nuxt server.
|
|
3
|
+
*/
|
|
4
|
+
declare function $fetchComponent(filepath: string, props?: Record<string, any>): Promise<any>;
|
|
5
|
+
declare function componentTestUrl(filepath: string, props?: Record<string, any>): string;
|
|
6
|
+
|
|
7
|
+
export { $fetchComponent, componentTestUrl };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { resolve } from 'pathe';
|
|
2
|
+
import { stringifyQuery } from 'ufo';
|
|
3
|
+
import { $ as $fetch, u as useTestContext } from './shared/test-utils.1e170326.mjs';
|
|
4
|
+
import 'execa';
|
|
5
|
+
import 'get-port-please';
|
|
6
|
+
import 'ofetch';
|
|
7
|
+
import '@nuxt/kit';
|
|
8
|
+
import 'node:path';
|
|
9
|
+
import 'defu';
|
|
10
|
+
|
|
11
|
+
function $fetchComponent(filepath, props) {
|
|
12
|
+
return $fetch(componentTestUrl(filepath, props));
|
|
13
|
+
}
|
|
14
|
+
function componentTestUrl(filepath, props) {
|
|
15
|
+
const ctx = useTestContext();
|
|
16
|
+
filepath = resolve(ctx.options.rootDir, filepath);
|
|
17
|
+
const path = stringifyQuery({
|
|
18
|
+
path: filepath,
|
|
19
|
+
props: JSON.stringify(props)
|
|
20
|
+
});
|
|
21
|
+
return `/__nuxt_component_test__/?${path}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { $fetchComponent, componentTestUrl };
|
package/dist/index.mjs
CHANGED
|
@@ -1,126 +1,15 @@
|
|
|
1
|
+
import { u as useTestContext, a as url, c as createTestContext, s as setTestContext, b as stopServer, d as startServer } from './shared/test-utils.1e170326.mjs';
|
|
2
|
+
export { $ as $fetch, e as exposeContextToEnv, f as fetch, i as isDev, r as recoverContextFromEnv } from './shared/test-utils.1e170326.mjs';
|
|
3
|
+
import { consola } from 'consola';
|
|
4
|
+
import { promises, existsSync } from 'node:fs';
|
|
1
5
|
import { resolve } from 'node:path';
|
|
2
6
|
import { defu } from 'defu';
|
|
3
|
-
import { execa } from 'execa';
|
|
4
|
-
import { getRandomPort, waitForPort } from 'get-port-please';
|
|
5
|
-
import { fetch as fetch$1, $fetch as $fetch$1 } from 'ofetch';
|
|
6
7
|
import * as _kit from '@nuxt/kit';
|
|
7
|
-
import consola from 'consola';
|
|
8
|
-
import { promises, existsSync } from 'node:fs';
|
|
9
8
|
import { dirname, resolve as resolve$1 } from 'pathe';
|
|
10
9
|
import { fileURLToPath } from 'node:url';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const _options = defu(options, {
|
|
15
|
-
testDir: resolve(process.cwd(), "test"),
|
|
16
|
-
fixture: "fixture",
|
|
17
|
-
configFile: "nuxt.config",
|
|
18
|
-
setupTimeout: 120 * 1e3,
|
|
19
|
-
dev: !!JSON.parse(process.env.NUXT_TEST_DEV || "false"),
|
|
20
|
-
logLevel: 1,
|
|
21
|
-
server: true,
|
|
22
|
-
build: options.browser !== false || options.server !== false,
|
|
23
|
-
nuxtConfig: {},
|
|
24
|
-
// TODO: auto detect based on process.env
|
|
25
|
-
runner: "vitest",
|
|
26
|
-
browserOptions: {
|
|
27
|
-
type: "chromium"
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
return setTestContext({
|
|
31
|
-
options: _options
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
function useTestContext() {
|
|
35
|
-
recoverContextFromEnv();
|
|
36
|
-
if (!currentContext) {
|
|
37
|
-
throw new Error("No context is available. (Forgot calling setup or createContext?)");
|
|
38
|
-
}
|
|
39
|
-
return currentContext;
|
|
40
|
-
}
|
|
41
|
-
function setTestContext(context) {
|
|
42
|
-
currentContext = context;
|
|
43
|
-
return currentContext;
|
|
44
|
-
}
|
|
45
|
-
function isDev() {
|
|
46
|
-
const ctx = useTestContext();
|
|
47
|
-
return ctx.options.dev;
|
|
48
|
-
}
|
|
49
|
-
function recoverContextFromEnv() {
|
|
50
|
-
if (!currentContext && process.env.NUXT_TEST_CONTEXT) {
|
|
51
|
-
setTestContext(JSON.parse(process.env.NUXT_TEST_CONTEXT || "{}"));
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
function exposeContextToEnv() {
|
|
55
|
-
const { options, browser, url } = currentContext;
|
|
56
|
-
process.env.NUXT_TEST_CONTEXT = JSON.stringify({ options, browser, url });
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const kit$1 = _kit.default || _kit;
|
|
60
|
-
async function startServer() {
|
|
61
|
-
const ctx = useTestContext();
|
|
62
|
-
await stopServer();
|
|
63
|
-
const port = await getRandomPort();
|
|
64
|
-
ctx.url = "http://127.0.0.1:" + port;
|
|
65
|
-
if (ctx.options.dev) {
|
|
66
|
-
const nuxiCLI = await kit$1.resolvePath("nuxi/cli");
|
|
67
|
-
ctx.serverProcess = execa(nuxiCLI, ["dev"], {
|
|
68
|
-
cwd: ctx.nuxt.options.rootDir,
|
|
69
|
-
stdio: "inherit",
|
|
70
|
-
env: {
|
|
71
|
-
...process.env,
|
|
72
|
-
PORT: String(port),
|
|
73
|
-
NITRO_PORT: String(port),
|
|
74
|
-
NODE_ENV: "development"
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
await waitForPort(port, { retries: 32 });
|
|
78
|
-
for (let i = 0; i < 50; i++) {
|
|
79
|
-
await new Promise((resolve2) => setTimeout(resolve2, 100));
|
|
80
|
-
try {
|
|
81
|
-
const res = await $fetch(ctx.nuxt.options.app.baseURL);
|
|
82
|
-
if (!res.includes("__NUXT_LOADING__")) {
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
} catch {
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
ctx.serverProcess.kill();
|
|
89
|
-
throw new Error("Timeout waiting for dev server!");
|
|
90
|
-
} else {
|
|
91
|
-
ctx.serverProcess = execa("node", [
|
|
92
|
-
resolve(ctx.nuxt.options.nitro.output.dir, "server/index.mjs")
|
|
93
|
-
], {
|
|
94
|
-
stdio: "inherit",
|
|
95
|
-
env: {
|
|
96
|
-
...process.env,
|
|
97
|
-
PORT: String(port),
|
|
98
|
-
NITRO_PORT: String(port),
|
|
99
|
-
NODE_ENV: "test"
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
await waitForPort(port, { retries: 8 });
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
async function stopServer() {
|
|
106
|
-
const ctx = useTestContext();
|
|
107
|
-
if (ctx.serverProcess) {
|
|
108
|
-
await ctx.serverProcess.kill();
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
function fetch(path, options) {
|
|
112
|
-
return fetch$1(url(path), options);
|
|
113
|
-
}
|
|
114
|
-
function $fetch(path, options) {
|
|
115
|
-
return $fetch$1(url(path), options);
|
|
116
|
-
}
|
|
117
|
-
function url(path) {
|
|
118
|
-
const ctx = useTestContext();
|
|
119
|
-
if (!ctx.url) {
|
|
120
|
-
throw new Error("url is not available (is server option enabled?)");
|
|
121
|
-
}
|
|
122
|
-
return ctx.url + path;
|
|
123
|
-
}
|
|
10
|
+
import 'execa';
|
|
11
|
+
import 'get-port-please';
|
|
12
|
+
import 'ofetch';
|
|
124
13
|
|
|
125
14
|
async function createBrowser() {
|
|
126
15
|
const ctx = useTestContext();
|
|
@@ -345,4 +234,4 @@ async function runTests(opts) {
|
|
|
345
234
|
}
|
|
346
235
|
}
|
|
347
236
|
|
|
348
|
-
export {
|
|
237
|
+
export { buildFixture, createBrowser, createPage, createTest, createTestContext, getBrowser, loadFixture, mockFn, mockLogger, runTests, setTestContext, setup, setupMaps, startServer, stopServer, url, useTestContext };
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { execa } from 'execa';
|
|
2
|
+
import { getRandomPort, waitForPort } from 'get-port-please';
|
|
3
|
+
import { fetch as fetch$1, $fetch as $fetch$1 } from 'ofetch';
|
|
4
|
+
import * as _kit from '@nuxt/kit';
|
|
5
|
+
import { resolve as resolve$1 } from 'pathe';
|
|
6
|
+
import { resolve } from 'node:path';
|
|
7
|
+
import { defu } from 'defu';
|
|
8
|
+
|
|
9
|
+
let currentContext;
|
|
10
|
+
function createTestContext(options) {
|
|
11
|
+
const _options = defu(options, {
|
|
12
|
+
testDir: resolve(process.cwd(), "test"),
|
|
13
|
+
fixture: "fixture",
|
|
14
|
+
configFile: "nuxt.config",
|
|
15
|
+
setupTimeout: 120 * 1e3,
|
|
16
|
+
dev: !!JSON.parse(process.env.NUXT_TEST_DEV || "false"),
|
|
17
|
+
logLevel: 1,
|
|
18
|
+
server: true,
|
|
19
|
+
build: options.browser !== false || options.server !== false,
|
|
20
|
+
nuxtConfig: {},
|
|
21
|
+
// TODO: auto detect based on process.env
|
|
22
|
+
runner: "vitest",
|
|
23
|
+
browserOptions: {
|
|
24
|
+
type: "chromium"
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
return setTestContext({
|
|
28
|
+
options: _options
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function useTestContext() {
|
|
32
|
+
recoverContextFromEnv();
|
|
33
|
+
if (!currentContext) {
|
|
34
|
+
throw new Error("No context is available. (Forgot calling setup or createContext?)");
|
|
35
|
+
}
|
|
36
|
+
return currentContext;
|
|
37
|
+
}
|
|
38
|
+
function setTestContext(context) {
|
|
39
|
+
currentContext = context;
|
|
40
|
+
return currentContext;
|
|
41
|
+
}
|
|
42
|
+
function isDev() {
|
|
43
|
+
const ctx = useTestContext();
|
|
44
|
+
return ctx.options.dev;
|
|
45
|
+
}
|
|
46
|
+
function recoverContextFromEnv() {
|
|
47
|
+
if (!currentContext && process.env.NUXT_TEST_CONTEXT) {
|
|
48
|
+
setTestContext(JSON.parse(process.env.NUXT_TEST_CONTEXT || "{}"));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function exposeContextToEnv() {
|
|
52
|
+
const { options, browser, url } = currentContext;
|
|
53
|
+
process.env.NUXT_TEST_CONTEXT = JSON.stringify({ options, browser, url });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const kit = _kit.default || _kit;
|
|
57
|
+
async function startServer() {
|
|
58
|
+
const ctx = useTestContext();
|
|
59
|
+
await stopServer();
|
|
60
|
+
const port = await getRandomPort();
|
|
61
|
+
ctx.url = "http://127.0.0.1:" + port;
|
|
62
|
+
if (ctx.options.dev) {
|
|
63
|
+
const nuxiCLI = await kit.resolvePath("nuxi/cli");
|
|
64
|
+
ctx.serverProcess = execa(nuxiCLI, ["dev"], {
|
|
65
|
+
cwd: ctx.nuxt.options.rootDir,
|
|
66
|
+
stdio: "inherit",
|
|
67
|
+
env: {
|
|
68
|
+
...process.env,
|
|
69
|
+
PORT: String(port),
|
|
70
|
+
NITRO_PORT: String(port),
|
|
71
|
+
NODE_ENV: "development"
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
await waitForPort(port, { retries: 32 });
|
|
75
|
+
for (let i = 0; i < 50; i++) {
|
|
76
|
+
await new Promise((resolve2) => setTimeout(resolve2, 100));
|
|
77
|
+
try {
|
|
78
|
+
const res = await $fetch(ctx.nuxt.options.app.baseURL);
|
|
79
|
+
if (!res.includes("__NUXT_LOADING__")) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
} catch {
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
ctx.serverProcess.kill();
|
|
86
|
+
throw new Error("Timeout waiting for dev server!");
|
|
87
|
+
} else {
|
|
88
|
+
ctx.serverProcess = execa("node", [
|
|
89
|
+
resolve$1(ctx.nuxt.options.nitro.output.dir, "server/index.mjs")
|
|
90
|
+
], {
|
|
91
|
+
stdio: "inherit",
|
|
92
|
+
env: {
|
|
93
|
+
...process.env,
|
|
94
|
+
PORT: String(port),
|
|
95
|
+
NITRO_PORT: String(port),
|
|
96
|
+
NODE_ENV: "test"
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
await waitForPort(port, { retries: 8 });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async function stopServer() {
|
|
103
|
+
const ctx = useTestContext();
|
|
104
|
+
if (ctx.serverProcess) {
|
|
105
|
+
await ctx.serverProcess.kill();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function fetch(path, options) {
|
|
109
|
+
return fetch$1(url(path), options);
|
|
110
|
+
}
|
|
111
|
+
function $fetch(path, options) {
|
|
112
|
+
return $fetch$1(url(path), options);
|
|
113
|
+
}
|
|
114
|
+
function url(path) {
|
|
115
|
+
const ctx = useTestContext();
|
|
116
|
+
if (!ctx.url) {
|
|
117
|
+
throw new Error("url is not available (is server option enabled?)");
|
|
118
|
+
}
|
|
119
|
+
if (path.startsWith(ctx.url)) {
|
|
120
|
+
return path;
|
|
121
|
+
}
|
|
122
|
+
return ctx.url + path;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export { $fetch as $, url as a, stopServer as b, createTestContext as c, startServer as d, exposeContextToEnv as e, fetch as f, isDev as i, recoverContextFromEnv as r, setTestContext as s, useTestContext as u };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nuxt/test-utils",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0",
|
|
4
4
|
"repository": "nuxt/nuxt",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -9,26 +9,31 @@
|
|
|
9
9
|
".": {
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"import": "./dist/index.mjs"
|
|
12
|
+
},
|
|
13
|
+
"./experimental": {
|
|
14
|
+
"types": "./dist/experimental.d.ts",
|
|
15
|
+
"import": "./dist/experimental.mjs"
|
|
12
16
|
}
|
|
13
17
|
},
|
|
14
18
|
"files": [
|
|
15
19
|
"dist"
|
|
16
20
|
],
|
|
17
21
|
"dependencies": {
|
|
18
|
-
"
|
|
19
|
-
"@nuxt/schema": "3.3.2",
|
|
20
|
-
"consola": "^2.15.3",
|
|
22
|
+
"consola": "^3.0.1",
|
|
21
23
|
"defu": "^6.1.2",
|
|
22
24
|
"execa": "^7.1.1",
|
|
23
25
|
"get-port-please": "^3.0.1",
|
|
24
26
|
"jiti": "^1.18.2",
|
|
25
27
|
"ofetch": "^1.0.1",
|
|
26
|
-
"pathe": "^1.1.0"
|
|
28
|
+
"pathe": "^1.1.0",
|
|
29
|
+
"ufo": "^1.1.1",
|
|
30
|
+
"@nuxt/schema": "3.4.0",
|
|
31
|
+
"@nuxt/kit": "3.4.0"
|
|
27
32
|
},
|
|
28
33
|
"devDependencies": {
|
|
29
|
-
"playwright": "^1.32.
|
|
34
|
+
"playwright": "^1.32.3",
|
|
30
35
|
"unbuild": "latest",
|
|
31
|
-
"vitest": "^0.
|
|
36
|
+
"vitest": "^0.30.1"
|
|
32
37
|
},
|
|
33
38
|
"peerDependencies": {
|
|
34
39
|
"vue": "^3.2.47"
|