@neon-pixels/vitest-preset 0.0.0 → 0.2.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) 2026 Neon Pixels Studio
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,70 @@
1
+ # @neon-pixels/vitest-preset
2
+
3
+ Shared [Vitest](https://vitest.dev) presets for Neon Pixels projects. Each preset is a plain config object you merge into your own `vitest.config.ts`.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ pnpm add -D @neon-pixels/vitest-preset vitest
9
+ ```
10
+
11
+ `vitest` is a peer dependency. The `vitepress` and `nuxt` presets also need `happy-dom`, `vue`, and `@vitejs/plugin-vue`:
12
+
13
+ ```sh
14
+ pnpm add -D happy-dom vue @vitejs/plugin-vue
15
+ ```
16
+
17
+ These three are optional peers, so a `node`-only project does not need them.
18
+
19
+ ## Usage
20
+
21
+ ```ts
22
+ import { fileURLToPath } from "node:url";
23
+ import { defineConfig, mergeConfig } from "vitest/config";
24
+ import preset from "@neon-pixels/vitest-preset/nuxt";
25
+
26
+ export default mergeConfig(
27
+ preset,
28
+ defineConfig({
29
+ resolve: {
30
+ alias: { "~": fileURLToPath(new URL("./app", import.meta.url)) },
31
+ },
32
+ test: {
33
+ include: ["tests/**/*.test.ts"],
34
+ setupFiles: ["./tests/setup.ts"],
35
+ },
36
+ }),
37
+ );
38
+ ```
39
+
40
+ The presets take no options. Aliases, `include` globs, and setup files belong to your repo: pass them in the config you merge. Arrays such as `exclude`, `setupFiles`, and `plugins` are concatenated, so yours run after the preset's.
41
+
42
+ ## Entry points
43
+
44
+ | Import | Environment | Adds |
45
+ | -------------------------------------- | ----------- | ---------------------------------------------------------------------------------- |
46
+ | `@neon-pixels/vitest-preset/node` | `node` | Shared exclusions |
47
+ | `@neon-pixels/vitest-preset/vitepress` | `happy-dom` | Shared exclusions, `TZ=UTC`, the Vue plugin, the `Comment`/`Text` global fix |
48
+ | `@neon-pixels/vitest-preset/nuxt` | `happy-dom` | Everything in `vitepress`, plus stubs for Nuxt and Vue auto-imports (listed below) |
49
+
50
+ The `vitepress` and `nuxt` presets already register `@vitejs/plugin-vue`. Do not add it again in your own config, or `.vue` files are transformed twice.
51
+
52
+ Every preset excludes `**/e2e/**`, `**/.netlify/**`, `**/.nuxt/**`, and `**/.output/**` on top of Vitest's default exclusions.
53
+
54
+ ### The `Comment` and `Text` global fix
55
+
56
+ Vitest 5 sets happy-dom's `Comment` and `Text` classes on `globalThis`. The usual way to fake Nuxt auto-imports is `Object.assign(globalThis, vue)`, which also overwrites those two names with Vue's `Comment` and `Text` symbols, and happy-dom then throws `Cannot read properties of undefined (reading 'Symbol(window)')` on the next `document.createComment()`.
57
+
58
+ The `vitepress` and `nuxt` presets pin both names to happy-dom's classes. Assigning to them is ignored, so your own setup file can copy Vue's exports without breaking the DOM. `vi.stubGlobal` can still replace them on purpose.
59
+
60
+ ### Nuxt stubs
61
+
62
+ The `nuxt` preset assigns these to `globalThis` before your setup files run, so a setup file or test can override any of them:
63
+
64
+ - Vue auto-imports: `computed`, `defineComponent`, `nextTick`, `onBeforeUnmount`, `onMounted`, `onScopeDispose`, `onUnmounted`, `reactive`, `ref`, `useAttrs`, `useSlots`, `watch`, `watchEffect`
65
+ - `defineNuxtRouteMiddleware` and `defineEventHandler`: identity functions
66
+ - `useRuntimeConfig`: returns `{ public: {} }`
67
+ - `useHead`: no-op
68
+ - `createError`: returns an `Error` carrying `statusCode`, `statusMessage`, and `data`; accepts a message string or an options object like h3. Targets h3 v1 (Nuxt 4): it reads `statusCode` and `statusMessage`, not h3 v2's `status` and `statusText`.
69
+
70
+ Anything else your code auto-imports (your own composables, `useFetch`) you stub in your setup file.
package/node.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import type { ViteUserConfig } from "vitest/config";
2
+
3
+ declare const preset: ViteUserConfig;
4
+
5
+ export default preset;
package/node.js ADDED
@@ -0,0 +1,8 @@
1
+ import { exclude } from "./shared.js";
2
+
3
+ export default {
4
+ test: {
5
+ environment: "node",
6
+ exclude,
7
+ },
8
+ };
package/nuxt.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import type { ViteUserConfig } from "vitest/config";
2
+
3
+ declare const preset: ViteUserConfig;
4
+
5
+ export default preset;
package/nuxt.js ADDED
@@ -0,0 +1,16 @@
1
+ import vue from "@vitejs/plugin-vue";
2
+ import { exclude, setupFile, timezone } from "./shared.js";
3
+
4
+ export default {
5
+ // A getter so every consumer gets its own plugin instance: the Vue plugin keeps
6
+ // per-server state in a closure.
7
+ get plugins() {
8
+ return [vue()];
9
+ },
10
+ test: {
11
+ environment: "happy-dom",
12
+ env: timezone,
13
+ exclude,
14
+ setupFiles: [setupFile("dom-globals.js"), setupFile("nuxt-stubs.js")],
15
+ },
16
+ };
package/package.json CHANGED
@@ -1 +1,70 @@
1
- {"name":"@neon-pixels/vitest-preset","version":"0.0.0","description":"Placeholder. The first real release follows.","license":"MIT","repository":{"type":"git","url":"git+https://github.com/neonpixels-studio/packages.git"}}
1
+ {
2
+ "name": "@neon-pixels/vitest-preset",
3
+ "version": "0.2.0",
4
+ "description": "Shared Vitest presets for Neon Pixels Node, VitePress, and Nuxt projects.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/neonpixels-studio/packages.git",
10
+ "directory": "packages/vitest-preset"
11
+ },
12
+ "exports": {
13
+ "./node": {
14
+ "types": "./node.d.ts",
15
+ "default": "./node.js"
16
+ },
17
+ "./vitepress": {
18
+ "types": "./vitepress.d.ts",
19
+ "default": "./vitepress.js"
20
+ },
21
+ "./nuxt": {
22
+ "types": "./nuxt.d.ts",
23
+ "default": "./nuxt.js"
24
+ },
25
+ "./package.json": "./package.json"
26
+ },
27
+ "files": [
28
+ "node.js",
29
+ "node.d.ts",
30
+ "vitepress.js",
31
+ "vitepress.d.ts",
32
+ "nuxt.js",
33
+ "nuxt.d.ts",
34
+ "shared.js",
35
+ "setup"
36
+ ],
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "engines": {
41
+ "node": "^22.17.0 || ^24.0.0 || >=26.0.0"
42
+ },
43
+ "peerDependencies": {
44
+ "@vitejs/plugin-vue": "^6.0.0",
45
+ "happy-dom": ">=20.0.0",
46
+ "vitest": "^5.0.0",
47
+ "vue": "^3.5.0"
48
+ },
49
+ "peerDependenciesMeta": {
50
+ "@vitejs/plugin-vue": {
51
+ "optional": true
52
+ },
53
+ "happy-dom": {
54
+ "optional": true
55
+ },
56
+ "vue": {
57
+ "optional": true
58
+ }
59
+ },
60
+ "devDependencies": {
61
+ "@vitejs/plugin-vue": "^6.0.11",
62
+ "happy-dom": "^21.0.4",
63
+ "vite": "^8.3.8",
64
+ "vitest": "^5.1.1",
65
+ "vue": "^3.5.46"
66
+ },
67
+ "scripts": {
68
+ "typecheck": "tsc --noEmit -p tsconfig.json"
69
+ }
70
+ }
@@ -0,0 +1,27 @@
1
+ // Vitest 5 populates `globalThis` with happy-dom's `Comment` and `Text`. A
2
+ // setup file that copies Vue's exports onto `globalThis` (the usual way to fake
3
+ // Nuxt auto-imports) replaces them with Vue's `Comment` and `Text` symbols, and
4
+ // happy-dom then fails with "Cannot read properties of undefined (reading
5
+ // 'Symbol(window)')" on the next `document.createComment()`.
6
+ //
7
+ // Pin both names to happy-dom's classes. Assignments are ignored rather than
8
+ // thrown, so `Object.assign(globalThis, vue)` keeps working. `configurable`
9
+ // stays true so `vi.stubGlobal` can still replace them on purpose.
10
+ /** @type {("Comment" | "Text")[]} */
11
+ const PROTECTED_GLOBALS = ["Comment", "Text"];
12
+
13
+ /** @param {"Comment" | "Text"} name */
14
+ function protectGlobal(name) {
15
+ const original = /** @type {Record<string, unknown>} */ (globalThis)[name];
16
+ if (typeof original !== "function") {
17
+ return;
18
+ }
19
+ Object.defineProperty(globalThis, name, {
20
+ configurable: true,
21
+ enumerable: true,
22
+ get: () => original,
23
+ set: () => {},
24
+ });
25
+ }
26
+
27
+ PROTECTED_GLOBALS.forEach(protectGlobal);
@@ -0,0 +1,70 @@
1
+ import {
2
+ computed,
3
+ defineComponent,
4
+ nextTick,
5
+ onBeforeUnmount,
6
+ onMounted,
7
+ onScopeDispose,
8
+ onUnmounted,
9
+ reactive,
10
+ ref,
11
+ useAttrs,
12
+ useSlots,
13
+ watch,
14
+ watchEffect,
15
+ } from "vue";
16
+
17
+ // Nuxt auto-imports Vue's API, so source files use these without importing
18
+ // them. Listed by name: copying every Vue export would also overwrite
19
+ // `Comment` and `Text` (see dom-globals.js).
20
+ const vueAutoImports = {
21
+ computed,
22
+ defineComponent,
23
+ nextTick,
24
+ onBeforeUnmount,
25
+ onMounted,
26
+ onScopeDispose,
27
+ onUnmounted,
28
+ reactive,
29
+ ref,
30
+ useAttrs,
31
+ useSlots,
32
+ watch,
33
+ watchEffect,
34
+ };
35
+
36
+ // Handler wrappers are identity functions so the module under test exports the
37
+ // inner function, which a test can call directly.
38
+ /** @param {unknown} handler */
39
+ const identity = (handler) => handler;
40
+
41
+ // h3 reports 500 when no status code is given.
42
+ const DEFAULT_STATUS_CODE = 500;
43
+
44
+ /**
45
+ * h3's createError as a real Error carrying the status fields, so tests can
46
+ * assert with `rejects.toMatchObject({ statusCode })`. Takes a message string
47
+ * or an options object, like h3.
48
+ * @param {string | { statusCode?: number, statusMessage?: string, message?: string, data?: unknown }} input
49
+ */
50
+ function createError(input) {
51
+ const options = typeof input === "string" ? { message: input } : input;
52
+ const { statusCode, statusMessage, message, data } = options;
53
+ return Object.assign(new Error(message ?? statusMessage), {
54
+ statusCode: statusCode ?? DEFAULT_STATUS_CODE,
55
+ statusMessage,
56
+ data,
57
+ });
58
+ }
59
+
60
+ const nuxtAndNitroStubs = {
61
+ defineNuxtRouteMiddleware: identity,
62
+ defineEventHandler: identity,
63
+ // Empty so code reading runtime config finds nothing. Tests that need values
64
+ // stub their own with `vi.stubGlobal`.
65
+ useRuntimeConfig: () => ({ public: {} }),
66
+ useHead: () => {},
67
+ createError,
68
+ };
69
+
70
+ Object.assign(globalThis, vueAutoImports, nuxtAndNitroStubs);
package/shared.js ADDED
@@ -0,0 +1,23 @@
1
+ import { fileURLToPath, URL } from "node:url";
2
+ import { configDefaults } from "vitest/config";
3
+
4
+ // Extends Vitest's defaults rather than replacing them: the defaults carry the
5
+ // `**/node_modules/**` glob, and Netlify installs build plugins into
6
+ // `.netlify/plugins/node_modules`, which a bare "node_modules" would miss.
7
+ export const exclude = [
8
+ ...configDefaults.exclude,
9
+ "**/e2e/**",
10
+ "**/.netlify/**",
11
+ "**/.nuxt/**",
12
+ "**/.output/**",
13
+ ];
14
+
15
+ export const timezone = { TZ: "UTC" };
16
+
17
+ /**
18
+ * Resolves a file in this package's `setup` directory to an absolute path.
19
+ * @param {string} name
20
+ */
21
+ export function setupFile(name) {
22
+ return fileURLToPath(new URL(`./setup/${name}`, import.meta.url));
23
+ }
package/vitepress.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import type { ViteUserConfig } from "vitest/config";
2
+
3
+ declare const preset: ViteUserConfig;
4
+
5
+ export default preset;
package/vitepress.js ADDED
@@ -0,0 +1,16 @@
1
+ import vue from "@vitejs/plugin-vue";
2
+ import { exclude, setupFile, timezone } from "./shared.js";
3
+
4
+ export default {
5
+ // A getter so every consumer gets its own plugin instance: the Vue plugin keeps
6
+ // per-server state in a closure.
7
+ get plugins() {
8
+ return [vue()];
9
+ },
10
+ test: {
11
+ environment: "happy-dom",
12
+ env: timezone,
13
+ exclude,
14
+ setupFiles: [setupFile("dom-globals.js")],
15
+ },
16
+ };