@nuxt/test-utils 3.0.0-rc.0 → 3.0.0-rc.10

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 CHANGED
@@ -2,7 +2,6 @@ import * as playwright_core from 'playwright-core';
2
2
  import { Browser, BrowserContextOptions, LaunchOptions } from 'playwright';
3
3
  import { NuxtConfig, Nuxt } from '@nuxt/schema';
4
4
  import { ExecaChildProcess } from 'execa';
5
- import { Listener } from 'listhen';
6
5
  import { FetchOptions } from 'ohmyfetch';
7
6
 
8
7
  declare function createBrowser(): Promise<void>;
@@ -36,7 +35,7 @@ interface TestContext {
36
35
  browser?: Browser;
37
36
  url?: string;
38
37
  serverProcess?: ExecaChildProcess;
39
- listener?: Listener;
38
+ mockFn?: Function;
40
39
  }
41
40
  interface TestHooks {
42
41
  beforeEach: () => void;
@@ -49,8 +48,12 @@ interface TestHooks {
49
48
  declare function createTestContext(options: Partial<TestOptions>): TestContext;
50
49
  declare function useTestContext(): TestContext;
51
50
  declare function setTestContext(context: TestContext): TestContext;
51
+ declare function setTestContext(context?: TestContext): TestContext | undefined;
52
52
  declare function isDev(): boolean;
53
53
 
54
+ declare function mockFn(): Function | undefined;
55
+ declare function mockLogger(): Record<string, Function>;
56
+
54
57
  declare function loadFixture(): Promise<void>;
55
58
  declare function buildFixture(): Promise<void>;
56
59
 
@@ -60,7 +63,7 @@ declare function fetch(path: string, options?: any): Promise<Response>;
60
63
  declare function $fetch(path: string, options?: FetchOptions): Promise<any>;
61
64
  declare function url(path: string): string;
62
65
 
63
- declare function setupJest(hooks: TestHooks): void;
66
+ declare function setupJest(hooks: TestHooks): Promise<void>;
64
67
 
65
68
  declare function setupVitest(hooks: TestHooks): Promise<void>;
66
69
 
@@ -79,4 +82,4 @@ interface RunTestOptions {
79
82
  }
80
83
  declare function runTests(opts: RunTestOptions): Promise<void>;
81
84
 
82
- export { $fetch, RunTestOptions, buildFixture, createBrowser, createPage, createTest, createTestContext, fetch, getBrowser, isDev, loadFixture, runTests, setTestContext, setup, setupMaps, startServer, stopServer, url, useTestContext };
85
+ export { $fetch, RunTestOptions, buildFixture, createBrowser, createPage, createTest, createTestContext, fetch, getBrowser, isDev, loadFixture, mockFn, mockLogger, runTests, setTestContext, setup, setupMaps, startServer, stopServer, url, useTestContext };
package/dist/index.mjs CHANGED
@@ -2,8 +2,9 @@ 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 'ohmyfetch';
6
6
  import * as _kit from '@nuxt/kit';
7
+ import consola from 'consola';
7
8
  import { promises, existsSync } from 'node:fs';
8
9
 
9
10
  let currentContext;
@@ -23,7 +24,9 @@ function createTestContext(options) {
23
24
  type: "chromium"
24
25
  }
25
26
  });
26
- return setTestContext({ options: _options });
27
+ return setTestContext({
28
+ options: _options
29
+ });
27
30
  }
28
31
  function useTestContext() {
29
32
  if (!currentContext) {
@@ -45,7 +48,7 @@ async function startServer() {
45
48
  const ctx = useTestContext();
46
49
  await stopServer();
47
50
  const port = await getRandomPort();
48
- ctx.url = "http://localhost:" + port;
51
+ ctx.url = "http://127.0.0.1:" + port;
49
52
  if (ctx.options.dev) {
50
53
  const nuxiCLI = await kit$1.resolvePath("nuxi/cli");
51
54
  ctx.serverProcess = execa(nuxiCLI, ["dev"], {
@@ -88,9 +91,6 @@ async function stopServer() {
88
91
  if (ctx.serverProcess) {
89
92
  await ctx.serverProcess.kill();
90
93
  }
91
- if (ctx.listener) {
92
- await ctx.listener.close();
93
- }
94
94
  }
95
95
  function fetch(path, options) {
96
96
  return fetch$1(url(path), options);
@@ -101,7 +101,7 @@ function $fetch(path, options) {
101
101
  function url(path) {
102
102
  const ctx = useTestContext();
103
103
  if (!ctx.url) {
104
- throw new Error("url is not availabe (is server option enabled?)");
104
+ throw new Error("url is not available (is server option enabled?)");
105
105
  }
106
106
  return ctx.url + path;
107
107
  }
@@ -139,6 +139,19 @@ async function createPage(path, options) {
139
139
  return page;
140
140
  }
141
141
 
142
+ function mockFn() {
143
+ const ctx = useTestContext();
144
+ return ctx.mockFn;
145
+ }
146
+ function mockLogger() {
147
+ const mocks = {};
148
+ consola.mockTypes((type) => {
149
+ mocks[type] = mockFn();
150
+ return mocks[type];
151
+ });
152
+ return mocks;
153
+ }
154
+
142
155
  const kit = _kit.default || _kit;
143
156
  const isNuxtApp = (dir) => {
144
157
  return existsSync(dir) && (existsSync(resolve(dir, "pages")) || existsSync(resolve(dir, "nuxt.config.js")) || existsSync(resolve(dir, "nuxt.config.ts")));
@@ -186,7 +199,9 @@ async function buildFixture() {
186
199
  await kit.buildNuxt(ctx.nuxt);
187
200
  }
188
201
 
189
- function setupJest(hooks) {
202
+ async function setupJest(hooks) {
203
+ const jest = await import('jest');
204
+ hooks.ctx.mockFn = jest.fn;
190
205
  test("setup", hooks.setup, 120 * 1e3);
191
206
  beforeEach(hooks.beforeEach);
192
207
  afterEach(hooks.afterEach);
@@ -195,6 +210,7 @@ function setupJest(hooks) {
195
210
 
196
211
  async function setupVitest(hooks) {
197
212
  const vitest = await import('vitest');
213
+ hooks.ctx.mockFn = vitest.vi.fn;
198
214
  vitest.beforeAll(hooks.setup, 120 * 1e3);
199
215
  vitest.beforeEach(hooks.beforeEach);
200
216
  vitest.afterEach(hooks.afterEach);
@@ -268,18 +284,22 @@ async function runTests(opts) {
268
284
  if (opts.dev) {
269
285
  process.env.NUXT_TEST_DEV = "true";
270
286
  }
271
- const { startVitest } = await import('vitest/dist/node.js');
272
- const succeeded = await startVitest([], {
273
- root: opts.rootDir,
274
- run: !opts.watch
275
- }, {
276
- esbuild: {
277
- tsconfigRaw: "{}"
287
+ const { startVitest } = await import('vitest/dist/node.mjs');
288
+ const succeeded = await startVitest(
289
+ [],
290
+ {
291
+ root: opts.rootDir,
292
+ run: !opts.watch
293
+ },
294
+ {
295
+ esbuild: {
296
+ tsconfigRaw: "{}"
297
+ }
278
298
  }
279
- });
299
+ );
280
300
  if (!succeeded) {
281
301
  process.exit(1);
282
302
  }
283
303
  }
284
304
 
285
- export { $fetch, buildFixture, createBrowser, createPage, createTest, createTestContext, fetch, getBrowser, isDev, loadFixture, runTests, setTestContext, setup, setupMaps, startServer, stopServer, url, useTestContext };
305
+ export { $fetch, buildFixture, createBrowser, createPage, createTest, createTestContext, fetch, getBrowser, isDev, loadFixture, mockFn, mockLogger, runTests, setTestContext, setup, setupMaps, startServer, stopServer, url, useTestContext };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuxt/test-utils",
3
- "version": "3.0.0-rc.0",
3
+ "version": "3.0.0-rc.10",
4
4
  "repository": "nuxt/framework",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -13,21 +13,22 @@
13
13
  "prepack": "unbuild"
14
14
  },
15
15
  "dependencies": {
16
- "@nuxt/kit": "3.0.0-rc.0",
17
- "@nuxt/schema": "3.0.0-rc.0",
18
- "defu": "^6.0.0",
16
+ "@nuxt/kit": "3.0.0-rc.10",
17
+ "@nuxt/schema": "3.0.0-rc.10",
18
+ "consola": "^2.15.3",
19
+ "defu": "^6.1.0",
19
20
  "execa": "^6.1.0",
20
- "get-port-please": "^2.5.0",
21
- "jiti": "^1.13.0",
22
- "ohmyfetch": "^0.4.15"
21
+ "get-port-please": "^2.6.1",
22
+ "jiti": "^1.15.0",
23
+ "ohmyfetch": "^0.4.18"
23
24
  },
24
25
  "devDependencies": {
25
- "playwright": "^1.21.1",
26
+ "playwright": "^1.25.2",
26
27
  "unbuild": "latest",
27
- "vitest": "^0.9.3"
28
+ "vitest": "~0.19.1"
28
29
  },
29
30
  "peerDependencies": {
30
- "vue": "3.2.33"
31
+ "vue": "^3.2.39"
31
32
  },
32
33
  "engines": {
33
34
  "node": "^14.16.0 || ^16.11.0 || ^17.0.0 || ^18.0.0"