@nuxt/test-utils-nightly 4.0.0-1699284628.89ffa77 → 4.0.0-1702810232.fba662c

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
+ The MIT License (MIT)
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 ADDED
@@ -0,0 +1,18 @@
1
+ # Nuxt Test Utils
2
+
3
+ 🧪 [Nuxt](https://nuxt.com/) Test Utils
4
+
5
+ ## Overview
6
+
7
+ Nuxt offers first-class support for e2e and unit testing of your Nuxt applications.
8
+
9
+ Nuxt Test Utils is currently powering [the tests we use on Nuxt itself](https://github.com/nuxt/nuxt/tree/main/test), as well as tests used throughout the module ecosystem.
10
+
11
+ You can find out more about how to use Nuxt Test Utils:
12
+
13
+ - in [a general overview](https://nuxt.com/docs/getting-started/testing)
14
+ - in [an in-depth guide for module authors](https://nuxt.com/docs/guide/going-further/modules/#testing)
15
+
16
+ ## License
17
+
18
+ [MIT](./LICENSE)
package/config.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/config'
@@ -0,0 +1,46 @@
1
+ import * as vite from 'vite';
2
+ import { InlineConfig } from 'vite';
3
+ import { NuxtConfig, Nuxt } from '@nuxt/schema';
4
+ import { InlineConfig as InlineConfig$1 } from 'vitest';
5
+
6
+ interface GetVitestConfigOptions {
7
+ nuxt: Nuxt;
8
+ viteConfig: InlineConfig;
9
+ }
10
+ declare function getVitestConfigFromNuxt(options?: GetVitestConfigOptions, overrides?: NuxtConfig): Promise<InlineConfig & {
11
+ test: InlineConfig$1;
12
+ }>;
13
+ declare function defineVitestConfig(config?: InlineConfig & {
14
+ test?: InlineConfig$1;
15
+ }): vite.UserConfig & Promise<vite.UserConfig> & vite.UserConfigFnObject & vite.UserConfigExport;
16
+ declare module 'vitest' {
17
+ interface EnvironmentOptions {
18
+ nuxt?: {
19
+ rootDir?: string;
20
+ /**
21
+ * The starting URL for your Nuxt window environment
22
+ * @default {http://localhost:3000}
23
+ */
24
+ url?: string;
25
+ overrides?: NuxtConfig;
26
+ /**
27
+ * The id of the root div to which the app should be mounted. You should also set `app.rootId` to the same value.
28
+ * @default {nuxt-test}
29
+ */
30
+ rootId?: string;
31
+ /**
32
+ * The name of the DOM environment to use.
33
+ *
34
+ * It also needs to be installed as a dev dependency in your project.
35
+ * @default {happy-dom}
36
+ */
37
+ domEnvironment?: 'happy-dom' | 'jsdom';
38
+ mock?: {
39
+ intersectionObserver?: boolean;
40
+ indexedDb?: boolean;
41
+ };
42
+ };
43
+ }
44
+ }
45
+
46
+ export { defineVitestConfig, getVitestConfigFromNuxt };
@@ -0,0 +1,46 @@
1
+ import * as vite from 'vite';
2
+ import { InlineConfig } from 'vite';
3
+ import { NuxtConfig, Nuxt } from '@nuxt/schema';
4
+ import { InlineConfig as InlineConfig$1 } from 'vitest';
5
+
6
+ interface GetVitestConfigOptions {
7
+ nuxt: Nuxt;
8
+ viteConfig: InlineConfig;
9
+ }
10
+ declare function getVitestConfigFromNuxt(options?: GetVitestConfigOptions, overrides?: NuxtConfig): Promise<InlineConfig & {
11
+ test: InlineConfig$1;
12
+ }>;
13
+ declare function defineVitestConfig(config?: InlineConfig & {
14
+ test?: InlineConfig$1;
15
+ }): vite.UserConfig & Promise<vite.UserConfig> & vite.UserConfigFnObject & vite.UserConfigExport;
16
+ declare module 'vitest' {
17
+ interface EnvironmentOptions {
18
+ nuxt?: {
19
+ rootDir?: string;
20
+ /**
21
+ * The starting URL for your Nuxt window environment
22
+ * @default {http://localhost:3000}
23
+ */
24
+ url?: string;
25
+ overrides?: NuxtConfig;
26
+ /**
27
+ * The id of the root div to which the app should be mounted. You should also set `app.rootId` to the same value.
28
+ * @default {nuxt-test}
29
+ */
30
+ rootId?: string;
31
+ /**
32
+ * The name of the DOM environment to use.
33
+ *
34
+ * It also needs to be installed as a dev dependency in your project.
35
+ * @default {happy-dom}
36
+ */
37
+ domEnvironment?: 'happy-dom' | 'jsdom';
38
+ mock?: {
39
+ intersectionObserver?: boolean;
40
+ indexedDb?: boolean;
41
+ };
42
+ };
43
+ }
44
+ }
45
+
46
+ export { defineVitestConfig, getVitestConfigFromNuxt };
@@ -0,0 +1,156 @@
1
+ import { defineConfig } from 'vite';
2
+ import { defu } from 'defu';
3
+ import { createResolver } from '@nuxt/kit';
4
+
5
+ async function startNuxtAndGetViteConfig(rootDir = process.cwd(), overrides) {
6
+ const { loadNuxt, buildNuxt } = await import('@nuxt/kit');
7
+ const nuxt = await loadNuxt({
8
+ cwd: rootDir,
9
+ dev: false,
10
+ overrides: defu(
11
+ {
12
+ ssr: false,
13
+ test: true,
14
+ modules: ["@nuxt/test-utils/module"]
15
+ },
16
+ overrides
17
+ )
18
+ });
19
+ if (!nuxt.options._installedModules.find((i) => i?.meta?.name === "@nuxt/test-utils")) {
20
+ throw new Error(
21
+ "Failed to load `@nuxt/test-utils/module`. You may need to add it to your nuxt.config."
22
+ );
23
+ }
24
+ const promise = new Promise((resolve, reject) => {
25
+ nuxt.hook("vite:configResolved", (viteConfig, { isClient }) => {
26
+ if (isClient) {
27
+ resolve({ nuxt, viteConfig });
28
+ throw new Error("_stop_");
29
+ }
30
+ });
31
+ buildNuxt(nuxt).catch((err) => {
32
+ if (!err.toString().includes("_stop_")) {
33
+ reject(err);
34
+ }
35
+ });
36
+ }).finally(() => nuxt.close());
37
+ return promise;
38
+ }
39
+ const excludedPlugins = [
40
+ "nuxt:import-protection",
41
+ "vite-plugin-checker"
42
+ ];
43
+ async function getVitestConfigFromNuxt(options, overrides) {
44
+ const { rootDir = process.cwd(), ..._overrides } = overrides || {};
45
+ if (!options) {
46
+ options = await startNuxtAndGetViteConfig(rootDir, {
47
+ test: true,
48
+ ..._overrides
49
+ });
50
+ }
51
+ options.viteConfig.plugins = (options.viteConfig.plugins || []).filter((p) => !excludedPlugins.includes(p?.name));
52
+ const resolvedConfig = defu(
53
+ // overrides
54
+ {
55
+ define: {
56
+ ["process.env.NODE_ENV"]: "process.env.NODE_ENV"
57
+ },
58
+ test: {
59
+ dir: process.cwd(),
60
+ environmentOptions: {
61
+ nuxtRuntimeConfig: options.nuxt.options.runtimeConfig,
62
+ nuxtRouteRules: defu(
63
+ {},
64
+ options.nuxt.options.routeRules,
65
+ options.nuxt.options.nitro?.routeRules
66
+ )
67
+ },
68
+ environmentMatchGlobs: [
69
+ ["**/*.nuxt.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}", "nuxt"],
70
+ ["{test,tests}/nuxt/**.*", "nuxt"]
71
+ ],
72
+ server: {
73
+ deps: {
74
+ inline: [
75
+ // vite-node defaults
76
+ /\/node_modules\/(.*\/)?(nuxt|nuxt3|nuxt-nightly)\//,
77
+ /^#/,
78
+ // additional deps
79
+ "@nuxt/test-utils",
80
+ "@nuxt/test-utils-nightly",
81
+ "@nuxt/test-utils-edge",
82
+ "vitest-environment-nuxt",
83
+ ...options.nuxt.options.build.transpile.filter(
84
+ (r) => typeof r === "string" || r instanceof RegExp
85
+ )
86
+ ]
87
+ }
88
+ },
89
+ deps: {
90
+ optimizer: {
91
+ web: {
92
+ enabled: false
93
+ }
94
+ }
95
+ }
96
+ }
97
+ },
98
+ {
99
+ server: { middlewareMode: false },
100
+ plugins: [
101
+ {
102
+ name: "disable-auto-execute",
103
+ enforce: "pre",
104
+ transform(code, id) {
105
+ if (id.match(/nuxt(3|-nightly)?\/.*\/entry\./)) {
106
+ return code.replace(
107
+ /(?<!vueAppPromise = )entry\(\)\.catch/,
108
+ "Promise.resolve().catch"
109
+ );
110
+ }
111
+ }
112
+ }
113
+ ]
114
+ },
115
+ // resolved vite config
116
+ options.viteConfig,
117
+ // (overrideable) defaults
118
+ {
119
+ test: {
120
+ environmentOptions: {
121
+ nuxt: {
122
+ rootId: options.nuxt.options.app.rootId || void 0,
123
+ mock: {
124
+ intersectionObserver: true,
125
+ indexedDb: false
126
+ }
127
+ }
128
+ }
129
+ }
130
+ }
131
+ );
132
+ delete resolvedConfig.define["process.browser"];
133
+ if (!Array.isArray(resolvedConfig.test.setupFiles)) {
134
+ resolvedConfig.test.setupFiles = [resolvedConfig.test.setupFiles].filter(Boolean);
135
+ }
136
+ const resolver = createResolver(import.meta.url);
137
+ resolvedConfig.test.setupFiles.unshift(resolver.resolve("./runtime/entry"));
138
+ return resolvedConfig;
139
+ }
140
+ function defineVitestConfig(config = {}) {
141
+ return defineConfig(async () => {
142
+ if (process.env.__NUXT_VITEST_RESOLVED__)
143
+ return config;
144
+ const overrides = config.test?.environmentOptions?.nuxt?.overrides || {};
145
+ overrides.rootDir = config.test?.environmentOptions?.nuxt?.rootDir;
146
+ if (config.test?.setupFiles && !Array.isArray(config.test.setupFiles)) {
147
+ config.test.setupFiles = [config.test.setupFiles].filter(Boolean);
148
+ }
149
+ return defu(
150
+ config,
151
+ await getVitestConfigFromNuxt(void 0, structuredClone(overrides))
152
+ );
153
+ });
154
+ }
155
+
156
+ export { defineVitestConfig, getVitestConfigFromNuxt };
@@ -37,6 +37,11 @@ interface TestContext {
37
37
  url?: string;
38
38
  serverProcess?: ExecaChildProcess;
39
39
  mockFn?: Function;
40
+ /**
41
+ * Functions to run on the vitest `afterAll` hook.
42
+ * Useful for removing anything created during the test.
43
+ */
44
+ teardown?: (() => void)[];
40
45
  }
41
46
  interface TestHooks {
42
47
  beforeEach: () => void;
@@ -37,6 +37,11 @@ interface TestContext {
37
37
  url?: string;
38
38
  serverProcess?: ExecaChildProcess;
39
39
  mockFn?: Function;
40
+ /**
41
+ * Functions to run on the vitest `afterAll` hook.
42
+ * Useful for removing anything created during the test.
43
+ */
44
+ teardown?: (() => void)[];
40
45
  }
41
46
  interface TestHooks {
42
47
  beforeEach: () => void;
@@ -1,7 +1,7 @@
1
- import { u as useTestContext, a as url, c as createTestContext, s as setTestContext, b as stopServer, d as startServer } from './shared/test-utils-nightly.8f432eb9.mjs';
2
- export { $ as $fetch, e as exposeContextToEnv, f as fetch, i as isDev, r as recoverContextFromEnv } from './shared/test-utils-nightly.8f432eb9.mjs';
1
+ import { u as useTestContext, a as url, c as createTestContext, s as setTestContext, b as stopServer, d as startServer } from './shared/test-utils-nightly.ddf5bsCK.mjs';
2
+ export { $ as $fetch, e as exposeContextToEnv, f as fetch, i as isDev, r as recoverContextFromEnv } from './shared/test-utils-nightly.ddf5bsCK.mjs';
3
3
  import { consola } from 'consola';
4
- import { promises, existsSync } from 'node:fs';
4
+ import { existsSync, promises } from 'node:fs';
5
5
  import { resolve } from 'node:path';
6
6
  import { defu } from 'defu';
7
7
  import * as _kit from '@nuxt/kit';
@@ -15,7 +15,10 @@ async function createBrowser() {
15
15
  const ctx = useTestContext();
16
16
  let playwright;
17
17
  try {
18
- playwright = await import(String("playwright-core"));
18
+ playwright = await import(
19
+ /* vite-ignore */
20
+ 'playwright-core'
21
+ );
19
22
  } catch {
20
23
  throw new Error(`
21
24
  The dependency 'playwright-core' not found.
@@ -80,12 +83,12 @@ async function loadFixture() {
80
83
  ctx.options.rootDir = resolveRootDir();
81
84
  if (!ctx.options.dev) {
82
85
  const randomId = Math.random().toString(36).slice(2, 8);
83
- const buildDir = resolve(ctx.options.rootDir, ".nuxt", randomId);
86
+ const buildDir2 = ctx.options.buildDir || resolve(ctx.options.rootDir, ".nuxt", "test", randomId);
84
87
  ctx.options.nuxtConfig = defu(ctx.options.nuxtConfig, {
85
- buildDir,
88
+ buildDir: buildDir2,
86
89
  nitro: {
87
90
  output: {
88
- dir: resolve(buildDir, "output")
91
+ dir: resolve(buildDir2, "output")
89
92
  }
90
93
  }
91
94
  });
@@ -96,7 +99,12 @@ async function loadFixture() {
96
99
  overrides: ctx.options.nuxtConfig,
97
100
  configFile: ctx.options.configFile
98
101
  });
99
- await promises.mkdir(ctx.nuxt.options.buildDir, { recursive: true });
102
+ const buildDir = ctx.nuxt.options.buildDir;
103
+ if (!existsSync(buildDir)) {
104
+ await promises.mkdir(buildDir, { recursive: true });
105
+ ctx.teardown = ctx.teardown || [];
106
+ ctx.teardown.push(() => promises.rm(buildDir, { recursive: true, force: true }));
107
+ }
100
108
  }
101
109
  async function buildFixture() {
102
110
  const ctx = useTestContext();
@@ -148,6 +156,7 @@ function createTest(options) {
148
156
  if (ctx.browser) {
149
157
  await ctx.browser.close();
150
158
  }
159
+ await Promise.all((ctx.teardown || []).map((fn) => fn()));
151
160
  };
152
161
  const setup2 = async () => {
153
162
  if (ctx.options.fixture) {
@@ -218,6 +227,7 @@ async function runTests(opts) {
218
227
  inline: [
219
228
  distDir,
220
229
  "@nuxt/test-utils",
230
+ "@nuxt/test-utils-nightly",
221
231
  "@nuxt/test-utils-edge"
222
232
  ]
223
233
  },
@@ -1,6 +1,6 @@
1
1
  import { resolve } from 'pathe';
2
2
  import { stringifyQuery } from 'ufo';
3
- import { $ as $fetch, u as useTestContext } from './shared/test-utils-nightly.8f432eb9.mjs';
3
+ import { $ as $fetch, u as useTestContext } from './shared/test-utils-nightly.ddf5bsCK.mjs';
4
4
  import 'execa';
5
5
  import 'get-port-please';
6
6
  import 'ofetch';
@@ -0,0 +1,11 @@
1
+ import * as _nuxt_schema from '@nuxt/schema';
2
+ import { UserConfig } from 'vitest';
3
+
4
+ interface NuxtVitestOptions {
5
+ startOnBoot?: boolean;
6
+ logToConsole?: boolean;
7
+ vitestConfig?: UserConfig;
8
+ }
9
+ declare const _default: _nuxt_schema.NuxtModule<NuxtVitestOptions>;
10
+
11
+ export { type NuxtVitestOptions, _default as default };
@@ -0,0 +1,11 @@
1
+ import * as _nuxt_schema from '@nuxt/schema';
2
+ import { UserConfig } from 'vitest';
3
+
4
+ interface NuxtVitestOptions {
5
+ startOnBoot?: boolean;
6
+ logToConsole?: boolean;
7
+ vitestConfig?: UserConfig;
8
+ }
9
+ declare const _default: _nuxt_schema.NuxtModule<NuxtVitestOptions>;
10
+
11
+ export { type NuxtVitestOptions, _default as default };