@nuxt/test-utils 3.0.0-rc.9 → 3.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016-present - Nuxt Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,128 @@
1
- # Nuxt Test Utils
2
1
 
3
- Test utilities for Nuxt.
2
+ [![Nuxt banner](./.github/assets/banner.png)](https://nuxt.com)
3
+
4
+ # 👋 Welcome to Nuxt
5
+
6
+
7
+ Nuxt's goal is to make web development intuitive and performant, with a great developer experience.<br>Learn more in the ['What is Nuxt?'](https://nuxt.com/docs/getting-started/introduction) section of our documentation.
8
+
9
+
10
+ <p>
11
+ <a href="https://www.npmjs.com/package/nuxt"><img src="https://img.shields.io/npm/v/nuxt.svg?style=flat&colorA=18181B&colorB=28CF8D" alt="Version"></a>
12
+ <a href="https://www.npmjs.com/package/nuxt"><img src="https://img.shields.io/npm/dm/nuxt.svg?style=flat&colorA=18181B&colorB=28CF8D" alt="Downloads"></a>
13
+ <a href="./LICENSE"><img src="https://img.shields.io/github/license/nuxt/nuxt.svg?style=flat&colorA=18181B&colorB=28CF8D" alt="License"></a>
14
+ <a href="https://volta.net/nuxt/nuxt?utm_source=nuxt_readme"><img src="https://user-images.githubusercontent.com/904724/209143798-32345f6c-3cf8-4e06-9659-f4ace4a6acde.svg" alt="Volta board"></a>
15
+ </p>
16
+
17
+ <table>
18
+ <thead>
19
+ <tr>
20
+ <th width="2000" colspan="2">
21
+ </th>
22
+ </tr>
23
+ </thead>
24
+ <tbody>
25
+ <tr>
26
+ <td width="80" align="center" valign="top">
27
+ <br>
28
+ <a href="https://nuxt.com/docs"><img src="./.github/assets/documentation.png"></a>
29
+ </td>
30
+ <td valign="top">
31
+ <h3>Documentation</h3>
32
+ <p>
33
+ We highly recommend you take a look at <a href="https://nuxt.com">the Nuxt documentation</a> to level up.
34
+ </p>
35
+ </td>
36
+ </tr>
37
+ <tr>
38
+ <td width="80" align="center" valign="top">
39
+ <br>
40
+ <a href="https://nuxt.com/modules"><img src="./.github/assets/modules.png"></a>
41
+ </td>
42
+ <td valign="top">
43
+ <h3>Modules</h3>
44
+ <p>
45
+ Discover our <a href="https://nuxt.com/modules">list of modules</a> to supercharge your Nuxt project. Created by the Nuxt team and community.
46
+ </p>
47
+ </td>
48
+ </tr>
49
+ <tr>
50
+ <td width="80" align="center" valign="top">
51
+ <br>
52
+ <a href="https://nuxt.com/docs/examples/hello-world"><img src="./.github/assets/examples.png"></a>
53
+ </td>
54
+ <td>
55
+ <h3>Examples</h3>
56
+ <p>
57
+ Explore different ways of using Nuxt features and get inspired with <a href="https://nuxt.com/docs/examples/essentials/hello-world">our list of examples</a>.
58
+ </p>
59
+ </td>
60
+ </tr>
61
+ </tbody>
62
+ </table>
63
+
64
+ <table>
65
+ <thead>
66
+ <tr>
67
+ <th width="2000" colspan="2">
68
+ </th>
69
+ </tr>
70
+ </thead>
71
+ <tbody>
72
+ <tr>
73
+ <td width="80" align="center" valign="top">
74
+ <br>
75
+ <a href="https://nuxt.com/docs/community/reporting-bugs"><img src="./.github/assets/reporting-bugs.png"></a>
76
+ </td>
77
+ <td valign="top">
78
+ <h3>Reporting bugs</h3>
79
+ <p>
80
+ Check out the <a href="https://nuxt.com/docs/community/reporting-bugs">Reporting Bugs</a> page.</p>
81
+ </p>
82
+ </td>
83
+ </tr>
84
+ <tr>
85
+ <td width="80" align="center" valign="top">
86
+ <br>
87
+ <a href="https://nuxt.com/docs/community/contribution"><img src="./.github/assets/suggestions.png"></a>
88
+ </td>
89
+ <td valign="top">
90
+ <h3>Suggestions</h3>
91
+ <p>
92
+ Check out the <a href="https://nuxt.com/docs/community/contribution">Contribution</a> page.
93
+ </p>
94
+ </td>
95
+ </tr>
96
+ <tr>
97
+ <td width="80" align="center" valign="top">
98
+ <br>
99
+ <a href="https://nuxt.com/docs/community/getting-help"><img src="./.github/assets/questions.png"></a>
100
+ </td>
101
+ <td valign="top">
102
+ <h3>Questions</h3>
103
+ <p>
104
+ Check out the <a href="https://nuxt.com/docs/community/getting-help">Getting Help</a> page.
105
+ </p>
106
+ </td>
107
+ </tr>
108
+ </tbody>
109
+ </table>
110
+
111
+ ## Local Development
112
+
113
+ Follow the docs to [Set Up Your Local Development Environment](https://nuxt.com/docs/community/framework-contribution#set-up-your-local-development-environment) to contribute to the framework and documentation.
114
+
115
+ ## Nuxt 2
116
+
117
+ You can find the code for Nuxt 2 on the [`2.x-dev` branch](https://github.com/nuxt/nuxt/tree/2.x-dev) and the documentation at [nuxtjs.org](https://nuxtjs.org).
118
+
119
+ ## Follow us
120
+
121
+ <p valign="center">
122
+ <a href="https://chat.nuxt.dev"><img width="20px" src="./.github/assets/discord.svg" alt="Discord"></a>&nbsp;&nbsp;<a href="https://twitter.nuxt.dev"><img width="20px" src="./.github/assets/twitter.svg" alt="Twitter"></a>&nbsp;&nbsp;<a href="https://github.nuxt.dev"><img width="20px" src="./.github/assets/github.svg" alt="GitHub"></a>
123
+ </p>
124
+
125
+ ## License
126
+
127
+ [MIT](./LICENSE)
128
+
package/dist/index.d.ts CHANGED
@@ -1,14 +1,14 @@
1
- import * as playwright_core from 'playwright-core';
1
+ 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 'ohmyfetch';
5
+ import { FetchOptions } from 'ofetch';
6
6
 
7
7
  declare function createBrowser(): Promise<void>;
8
8
  declare function getBrowser(): Promise<Browser>;
9
- declare function createPage(path?: string, options?: BrowserContextOptions): Promise<playwright_core.Page>;
9
+ declare function createPage(path?: string, options?: BrowserContextOptions): Promise<playwright.Page>;
10
10
 
11
- declare type TestRunner = 'vitest' | 'jest';
11
+ type TestRunner = 'vitest' | 'jest';
12
12
  interface TestOptions {
13
13
  testDir: string;
14
14
  fixture: string;
@@ -35,6 +35,7 @@ interface TestContext {
35
35
  browser?: Browser;
36
36
  url?: string;
37
37
  serverProcess?: ExecaChildProcess;
38
+ mockFn?: Function;
38
39
  }
39
40
  interface TestHooks {
40
41
  beforeEach: () => void;
@@ -49,6 +50,11 @@ declare function useTestContext(): TestContext;
49
50
  declare function setTestContext(context: TestContext): TestContext;
50
51
  declare function setTestContext(context?: TestContext): TestContext | undefined;
51
52
  declare function isDev(): boolean;
53
+ declare function recoverContextFromEnv(): void;
54
+ declare function exposeContextToEnv(): void;
55
+
56
+ declare function mockFn(): Function | undefined;
57
+ declare function mockLogger(): Record<string, Function>;
52
58
 
53
59
  declare function loadFixture(): Promise<void>;
54
60
  declare function buildFixture(): Promise<void>;
@@ -59,7 +65,7 @@ declare function fetch(path: string, options?: any): Promise<Response>;
59
65
  declare function $fetch(path: string, options?: FetchOptions): Promise<any>;
60
66
  declare function url(path: string): string;
61
67
 
62
- declare function setupJest(hooks: TestHooks): void;
68
+ declare function setupJest(hooks: TestHooks): Promise<void>;
63
69
 
64
70
  declare function setupVitest(hooks: TestHooks): Promise<void>;
65
71
 
@@ -68,14 +74,15 @@ declare const setupMaps: {
68
74
  vitest: typeof setupVitest;
69
75
  };
70
76
  declare function createTest(options: Partial<TestOptions>): TestHooks;
71
- declare function setup(options: Partial<TestOptions>): Promise<void>;
77
+ declare function setup(options?: Partial<TestOptions>): Promise<void>;
72
78
 
73
79
  interface RunTestOptions {
74
80
  rootDir: string;
75
81
  dev?: boolean;
76
82
  watch?: boolean;
77
83
  runner?: 'vitest';
84
+ globalSetup?: boolean;
78
85
  }
79
86
  declare function runTests(opts: RunTestOptions): Promise<void>;
80
87
 
81
- export { $fetch, RunTestOptions, buildFixture, createBrowser, createPage, createTest, createTestContext, fetch, getBrowser, isDev, loadFixture, 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,9 +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 'ohmyfetch';
5
+ import { fetch as fetch$1, $fetch as $fetch$1 } from 'ofetch';
6
6
  import * as _kit from '@nuxt/kit';
7
+ import consola from 'consola';
7
8
  import { promises, existsSync } from 'node:fs';
9
+ import { dirname, resolve as resolve$1 } from 'pathe';
10
+ import { fileURLToPath } from 'node:url';
8
11
 
9
12
  let currentContext;
10
13
  function createTestContext(options) {
@@ -12,20 +15,24 @@ function createTestContext(options) {
12
15
  testDir: resolve(process.cwd(), "test"),
13
16
  fixture: "fixture",
14
17
  configFile: "nuxt.config",
15
- setupTimeout: 6e4,
18
+ setupTimeout: 120 * 1e3,
16
19
  dev: !!JSON.parse(process.env.NUXT_TEST_DEV || "false"),
17
20
  logLevel: 1,
18
21
  server: true,
19
22
  build: options.browser !== false || options.server !== false,
20
23
  nuxtConfig: {},
24
+ // TODO: auto detect based on process.env
21
25
  runner: "vitest",
22
26
  browserOptions: {
23
27
  type: "chromium"
24
28
  }
25
29
  });
26
- return setTestContext({ options: _options });
30
+ return setTestContext({
31
+ options: _options
32
+ });
27
33
  }
28
34
  function useTestContext() {
35
+ recoverContextFromEnv();
29
36
  if (!currentContext) {
30
37
  throw new Error("No context is available. (Forgot calling setup or createContext?)");
31
38
  }
@@ -39,6 +46,15 @@ function isDev() {
39
46
  const ctx = useTestContext();
40
47
  return ctx.options.dev;
41
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
+ }
42
58
 
43
59
  const kit$1 = _kit.default || _kit;
44
60
  async function startServer() {
@@ -54,6 +70,7 @@ async function startServer() {
54
70
  env: {
55
71
  ...process.env,
56
72
  PORT: String(port),
73
+ NITRO_PORT: String(port),
57
74
  NODE_ENV: "development"
58
75
  }
59
76
  });
@@ -77,6 +94,7 @@ async function startServer() {
77
94
  env: {
78
95
  ...process.env,
79
96
  PORT: String(port),
97
+ NITRO_PORT: String(port),
80
98
  NODE_ENV: "test"
81
99
  }
82
100
  });
@@ -136,9 +154,22 @@ async function createPage(path, options) {
136
154
  return page;
137
155
  }
138
156
 
157
+ function mockFn() {
158
+ const ctx = useTestContext();
159
+ return ctx.mockFn;
160
+ }
161
+ function mockLogger() {
162
+ const mocks = {};
163
+ consola.mockTypes((type) => {
164
+ mocks[type] = mockFn();
165
+ return mocks[type];
166
+ });
167
+ return mocks;
168
+ }
169
+
139
170
  const kit = _kit.default || _kit;
140
171
  const isNuxtApp = (dir) => {
141
- return existsSync(dir) && (existsSync(resolve(dir, "pages")) || existsSync(resolve(dir, "nuxt.config.js")) || existsSync(resolve(dir, "nuxt.config.ts")));
172
+ 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")));
142
173
  };
143
174
  const resolveRootDir = () => {
144
175
  const { options } = useTestContext();
@@ -175,16 +206,20 @@ async function loadFixture() {
175
206
  overrides: ctx.options.nuxtConfig,
176
207
  configFile: ctx.options.configFile
177
208
  });
178
- kit.logger.level = ctx.options.logLevel;
179
209
  await promises.mkdir(ctx.nuxt.options.buildDir, { recursive: true });
180
210
  }
181
211
  async function buildFixture() {
182
212
  const ctx = useTestContext();
213
+ const prevLevel = kit.logger.level;
214
+ kit.logger.level = ctx.options.logLevel;
183
215
  await kit.buildNuxt(ctx.nuxt);
216
+ kit.logger.level = prevLevel;
184
217
  }
185
218
 
186
- function setupJest(hooks) {
187
- test("setup", hooks.setup, 120 * 1e3);
219
+ async function setupJest(hooks) {
220
+ const jest = await import('jest');
221
+ hooks.ctx.mockFn = jest.fn;
222
+ test("setup", hooks.setup, hooks.ctx.options.setupTimeout);
188
223
  beforeEach(hooks.beforeEach);
189
224
  afterEach(hooks.afterEach);
190
225
  afterAll(hooks.afterAll);
@@ -192,7 +227,8 @@ function setupJest(hooks) {
192
227
 
193
228
  async function setupVitest(hooks) {
194
229
  const vitest = await import('vitest');
195
- vitest.beforeAll(hooks.setup, 120 * 1e3);
230
+ hooks.ctx.mockFn = vitest.vi.fn;
231
+ vitest.beforeAll(hooks.setup, hooks.ctx.options.setupTimeout);
196
232
  vitest.beforeEach(hooks.beforeEach);
197
233
  vitest.afterEach(hooks.afterEach);
198
234
  vitest.afterAll(hooks.afterAll);
@@ -248,14 +284,18 @@ function createTest(options) {
248
284
  ctx
249
285
  };
250
286
  }
251
- async function setup(options) {
287
+ async function setup(options = {}) {
252
288
  const hooks = createTest(options);
253
289
  const setupFn = setupMaps[hooks.ctx.options.runner];
254
290
  await setupFn(hooks);
255
291
  }
256
292
 
293
+ const distDir = dirname(fileURLToPath(import.meta.url));
294
+ resolve$1(distDir, "..");
295
+
257
296
  const RunTestDefaults = {
258
- runner: "vitest"
297
+ runner: "vitest",
298
+ globalSetup: true
259
299
  };
260
300
  async function runTests(opts) {
261
301
  opts = { ...RunTestDefaults, ...opts };
@@ -265,16 +305,37 @@ async function runTests(opts) {
265
305
  if (opts.dev) {
266
306
  process.env.NUXT_TEST_DEV = "true";
267
307
  }
268
- const { startVitest } = await import('vitest/dist/node.mjs');
308
+ process.env.NUXT_TEST_OPTIONS = JSON.stringify(opts);
309
+ const { startVitest } = await import('vitest/node');
269
310
  const succeeded = await startVitest(
311
+ "test",
270
312
  [],
313
+ // Vitest options
271
314
  {
272
315
  root: opts.rootDir,
273
- run: !opts.watch
316
+ run: !opts.watch,
317
+ deps: {
318
+ inline: [/@nuxt\/test-utils/]
319
+ }
274
320
  },
321
+ // Vite options
275
322
  {
276
323
  esbuild: {
277
324
  tsconfigRaw: "{}"
325
+ },
326
+ test: {
327
+ dir: opts.rootDir,
328
+ deps: {
329
+ inline: [
330
+ distDir,
331
+ "@nuxt/test-utils",
332
+ "@nuxt/test-utils-edge"
333
+ ]
334
+ },
335
+ globals: true,
336
+ globalSetup: [
337
+ ...opts.globalSetup ? [resolve$1(distDir, "./runtime/global-setup")] : []
338
+ ]
278
339
  }
279
340
  }
280
341
  );
@@ -283,4 +344,4 @@ async function runTests(opts) {
283
344
  }
284
345
  }
285
346
 
286
- export { $fetch, buildFixture, createBrowser, createPage, createTest, createTestContext, fetch, getBrowser, isDev, loadFixture, runTests, setTestContext, setup, setupMaps, startServer, stopServer, url, useTestContext };
347
+ 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,2 @@
1
+ export declare const setup: () => Promise<void>;
2
+ export declare const teardown: () => Promise<void>;
@@ -0,0 +1,14 @@
1
+ import * as _kit from "@nuxt/kit";
2
+ import { createTest, exposeContextToEnv } from "@nuxt/test-utils";
3
+ const kit = _kit.default || _kit;
4
+ const options = JSON.parse(process.env.NUXT_TEST_OPTIONS || "{}");
5
+ const hooks = createTest(options);
6
+ export const setup = async () => {
7
+ kit.logger.info("Building Nuxt app...");
8
+ await hooks.setup();
9
+ exposeContextToEnv();
10
+ kit.logger.info("Running tests...");
11
+ };
12
+ export const teardown = async () => {
13
+ await hooks.afterAll();
14
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nuxt/test-utils",
3
- "version": "3.0.0-rc.9",
4
- "repository": "nuxt/framework",
3
+ "version": "3.1.0",
4
+ "repository": "nuxt/nuxt",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "exports": "./dist/index.mjs",
@@ -9,27 +9,27 @@
9
9
  "files": [
10
10
  "dist"
11
11
  ],
12
- "scripts": {
13
- "prepack": "unbuild"
14
- },
15
12
  "dependencies": {
16
- "@nuxt/kit": "3.0.0-rc.9",
17
- "@nuxt/schema": "3.0.0-rc.9",
18
- "defu": "^6.1.0",
13
+ "@nuxt/kit": "3.1.0",
14
+ "@nuxt/schema": "3.1.0",
15
+ "consola": "^2.15.3",
16
+ "defu": "^6.1.1",
19
17
  "execa": "^6.1.0",
20
- "get-port-please": "^2.6.1",
21
- "jiti": "^1.14.0",
22
- "ohmyfetch": "^0.4.18"
18
+ "get-port-please": "^3.0.1",
19
+ "jiti": "^1.16.2",
20
+ "ofetch": "^1.0.0",
21
+ "pathe": "^1.0.0"
23
22
  },
24
23
  "devDependencies": {
25
- "playwright": "^1.25.1",
24
+ "playwright": "^1.29.2",
26
25
  "unbuild": "latest",
27
- "vitest": "~0.19.1"
26
+ "vitest": "^0.27.2"
28
27
  },
29
28
  "peerDependencies": {
30
- "vue": "^3.2.38"
29
+ "vue": "^3.2.45"
31
30
  },
32
31
  "engines": {
33
- "node": "^14.16.0 || ^16.11.0 || ^17.0.0 || ^18.0.0"
34
- }
35
- }
32
+ "node": "^14.16.0 || ^16.10.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
33
+ },
34
+ "scripts": {}
35
+ }