@nuxt/test-utils 3.0.0-rc.8 → 3.0.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) 2022 - Nuxt Project
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/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;
@@ -47,7 +48,13 @@ interface TestHooks {
47
48
  declare function createTestContext(options: Partial<TestOptions>): TestContext;
48
49
  declare function useTestContext(): TestContext;
49
50
  declare function setTestContext(context: TestContext): TestContext;
51
+ declare function setTestContext(context?: TestContext): TestContext | undefined;
50
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>;
51
58
 
52
59
  declare function loadFixture(): Promise<void>;
53
60
  declare function buildFixture(): Promise<void>;
@@ -58,7 +65,7 @@ declare function fetch(path: string, options?: any): Promise<Response>;
58
65
  declare function $fetch(path: string, options?: FetchOptions): Promise<any>;
59
66
  declare function url(path: string): string;
60
67
 
61
- declare function setupJest(hooks: TestHooks): void;
68
+ declare function setupJest(hooks: TestHooks): Promise<void>;
62
69
 
63
70
  declare function setupVitest(hooks: TestHooks): Promise<void>;
64
71
 
@@ -67,14 +74,15 @@ declare const setupMaps: {
67
74
  vitest: typeof setupVitest;
68
75
  };
69
76
  declare function createTest(options: Partial<TestOptions>): TestHooks;
70
- declare function setup(options: Partial<TestOptions>): Promise<void>;
77
+ declare function setup(options?: Partial<TestOptions>): Promise<void>;
71
78
 
72
79
  interface RunTestOptions {
73
80
  rootDir: string;
74
81
  dev?: boolean;
75
82
  watch?: boolean;
76
83
  runner?: 'vitest';
84
+ globalSetup?: boolean;
77
85
  }
78
86
  declare function runTests(opts: RunTestOptions): Promise<void>;
79
87
 
80
- 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,7 +15,7 @@ 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,
@@ -23,9 +26,12 @@ function createTestContext(options) {
23
26
  type: "chromium"
24
27
  }
25
28
  });
26
- return setTestContext({ options: _options });
29
+ return setTestContext({
30
+ options: _options
31
+ });
27
32
  }
28
33
  function useTestContext() {
34
+ recoverContextFromEnv();
29
35
  if (!currentContext) {
30
36
  throw new Error("No context is available. (Forgot calling setup or createContext?)");
31
37
  }
@@ -39,6 +45,15 @@ function isDev() {
39
45
  const ctx = useTestContext();
40
46
  return ctx.options.dev;
41
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
+ }
42
57
 
43
58
  const kit$1 = _kit.default || _kit;
44
59
  async function startServer() {
@@ -54,6 +69,7 @@ async function startServer() {
54
69
  env: {
55
70
  ...process.env,
56
71
  PORT: String(port),
72
+ NITRO_PORT: String(port),
57
73
  NODE_ENV: "development"
58
74
  }
59
75
  });
@@ -77,6 +93,7 @@ async function startServer() {
77
93
  env: {
78
94
  ...process.env,
79
95
  PORT: String(port),
96
+ NITRO_PORT: String(port),
80
97
  NODE_ENV: "test"
81
98
  }
82
99
  });
@@ -136,9 +153,22 @@ async function createPage(path, options) {
136
153
  return page;
137
154
  }
138
155
 
156
+ function mockFn() {
157
+ const ctx = useTestContext();
158
+ return ctx.mockFn;
159
+ }
160
+ function mockLogger() {
161
+ const mocks = {};
162
+ consola.mockTypes((type) => {
163
+ mocks[type] = mockFn();
164
+ return mocks[type];
165
+ });
166
+ return mocks;
167
+ }
168
+
139
169
  const kit = _kit.default || _kit;
140
170
  const isNuxtApp = (dir) => {
141
- 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")));
142
172
  };
143
173
  const resolveRootDir = () => {
144
174
  const { options } = useTestContext();
@@ -183,8 +213,10 @@ async function buildFixture() {
183
213
  await kit.buildNuxt(ctx.nuxt);
184
214
  }
185
215
 
186
- function setupJest(hooks) {
187
- test("setup", hooks.setup, 120 * 1e3);
216
+ async function setupJest(hooks) {
217
+ const jest = await import('jest');
218
+ hooks.ctx.mockFn = jest.fn;
219
+ test("setup", hooks.setup, hooks.ctx.options.setupTimeout);
188
220
  beforeEach(hooks.beforeEach);
189
221
  afterEach(hooks.afterEach);
190
222
  afterAll(hooks.afterAll);
@@ -192,7 +224,8 @@ function setupJest(hooks) {
192
224
 
193
225
  async function setupVitest(hooks) {
194
226
  const vitest = await import('vitest');
195
- vitest.beforeAll(hooks.setup, 120 * 1e3);
227
+ hooks.ctx.mockFn = vitest.vi.fn;
228
+ vitest.beforeAll(hooks.setup, hooks.ctx.options.setupTimeout);
196
229
  vitest.beforeEach(hooks.beforeEach);
197
230
  vitest.afterEach(hooks.afterEach);
198
231
  vitest.afterAll(hooks.afterAll);
@@ -248,14 +281,18 @@ function createTest(options) {
248
281
  ctx
249
282
  };
250
283
  }
251
- async function setup(options) {
284
+ async function setup(options = {}) {
252
285
  const hooks = createTest(options);
253
286
  const setupFn = setupMaps[hooks.ctx.options.runner];
254
287
  await setupFn(hooks);
255
288
  }
256
289
 
290
+ const distDir = dirname(fileURLToPath(import.meta.url));
291
+ resolve$1(distDir, "..");
292
+
257
293
  const RunTestDefaults = {
258
- runner: "vitest"
294
+ runner: "vitest",
295
+ globalSetup: true
259
296
  };
260
297
  async function runTests(opts) {
261
298
  opts = { ...RunTestDefaults, ...opts };
@@ -265,16 +302,35 @@ async function runTests(opts) {
265
302
  if (opts.dev) {
266
303
  process.env.NUXT_TEST_DEV = "true";
267
304
  }
268
- const { startVitest } = await import('vitest/dist/node.mjs');
305
+ process.env.NUXT_TEST_OPTIONS = JSON.stringify(opts);
306
+ const { startVitest } = await import('vitest/node');
269
307
  const succeeded = await startVitest(
308
+ "test",
270
309
  [],
271
310
  {
272
311
  root: opts.rootDir,
273
- run: !opts.watch
312
+ run: !opts.watch,
313
+ deps: {
314
+ inline: [/@nuxt\/test-utils/]
315
+ }
274
316
  },
275
317
  {
276
318
  esbuild: {
277
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
+ ]
278
334
  }
279
335
  }
280
336
  );
@@ -283,4 +339,4 @@ async function runTests(opts) {
283
339
  }
284
340
  }
285
341
 
286
- export { $fetch, buildFixture, createBrowser, createPage, createTest, createTestContext, fetch, getBrowser, isDev, loadFixture, 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,2 @@
1
+ export declare const setup: () => Promise<void>;
2
+ export declare const teardown: () => Promise<void>;
@@ -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.8",
3
+ "version": "3.0.0",
4
4
  "repository": "nuxt/framework",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -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.8",
17
- "@nuxt/schema": "3.0.0-rc.8",
18
- "defu": "^6.0.0",
13
+ "@nuxt/kit": "3.0.0",
14
+ "@nuxt/schema": "3.0.0",
15
+ "consola": "^2.15.3",
16
+ "defu": "^6.1.1",
19
17
  "execa": "^6.1.0",
20
18
  "get-port-please": "^2.6.1",
21
- "jiti": "^1.14.0",
22
- "ohmyfetch": "^0.4.18"
19
+ "jiti": "^1.16.0",
20
+ "ofetch": "^1.0.0",
21
+ "pathe": "^1.0.0"
23
22
  },
24
23
  "devDependencies": {
25
- "playwright": "^1.25.0",
24
+ "playwright": "^1.27.1",
26
25
  "unbuild": "latest",
27
- "vitest": "~0.19.1"
26
+ "vitest": "^0.25.2"
28
27
  },
29
28
  "peerDependencies": {
30
- "vue": "^3.2.37"
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
+ }