From 39689fe35960e404486509c5ab3dd114c721e6ed Mon Sep 17 00:00:00 2001 From: Rodolphe Stoclin Date: Mon, 23 Nov 2026 16:54:36 +0100 Subject: [PATCH 1/4] test: make negative tests fail when nothing throws (#2953) * test: make negative tests fail when nothing throws * test: pin esbuild and gcc error messages, rename core type test --- packages/core/__tests__/core.test.ts | 74 +++++-------------- packages/cssnano/__tests__/cssnano.test.ts | 8 +- packages/esbuild/__tests__/esbuild.test.ts | 14 +--- .../__tests__/google-closure-compiler.test.ts | 6 +- .../__tests__/lightningcss.test.ts | 6 +- packages/swc/__tests__/swc.test.ts | 6 +- packages/terser/__tests__/terser.test.ts | 6 +- .../uglify-js/__tests__/uglify-js.test.ts | 6 +- 8 files changed, 31 insertions(+), 95 deletions(-) diff --git a/packages/core/__tests__/core.test.ts b/packages/core/__tests__/core.test.ts index 3db839ea9..bca6fcd32 100644 --- a/packages/core/__tests__/core.test.ts +++ b/packages/core/__tests__/core.test.ts @@ -38,15 +38,9 @@ describe("Package: core", async () => { output: filesJS.fileJSOut, }; - try { - return await minify(settings); - } catch (err: unknown) { - if (err instanceof Error) { - return expect(err.toString()).toEqual( - "Error: compressor should be a function, maybe you forgot to install the compressor" - ); - } - } + await expect(minify(settings)).rejects.toThrow( + "compressor should be a function, maybe you forgot to install the compressor" + ); }); }); @@ -58,15 +52,9 @@ describe("Package: core", async () => { output: filesJS.fileJSOut, }; - try { - return await minify(settings); - } catch (err: unknown) { - if (err instanceof Error) { - return expect(err.toString()).toEqual( - "Error: compressor is mandatory." - ); - } - } + await expect(minify(settings)).rejects.toThrow( + "compressor is mandatory." + ); }); test("should throw an error if no input", async () => { @@ -75,15 +63,9 @@ describe("Package: core", async () => { output: filesJS.fileJSOut, }; - try { - return await minify(settings); - } catch (err: unknown) { - if (err instanceof Error) { - return expect(err.toString()).toEqual( - "Error: input is mandatory." - ); - } - } + await expect(minify(settings)).rejects.toThrow( + "input is mandatory." + ); }); test("should throw an error if no output", async () => { @@ -92,20 +74,14 @@ describe("Package: core", async () => { input: filesJS.oneFileWithWildcards, }; - try { - return await minify(settings); - } catch (err: unknown) { - if (err instanceof Error) { - return expect(err.toString()).toEqual( - "Error: output is mandatory." - ); - } - } + await expect(minify(settings)).rejects.toThrow( + "output is mandatory." + ); }); }); describe("Mandatory", () => { - test("should show throw on type option", async () => { + test("should require a compressor even when type is set", async () => { const settings: Settings = { // @ts-expect-error testing invalid input: "uglifyjs" is not a valid FileType ("js" | "css") type: "uglifyjs", @@ -113,15 +89,9 @@ describe("Package: core", async () => { output: filesJS.fileJSOut, }; - try { - return await minify(settings); - } catch (err: unknown) { - if (err instanceof Error) { - return expect(err.toString()).toEqual( - "Error: compressor is mandatory." - ); - } - } + await expect(minify(settings)).rejects.toThrow( + "compressor is mandatory." + ); }); }); @@ -167,15 +137,9 @@ describe("Package: core", async () => { "
content
", }; - try { - return await minify(settings); - } catch (err: unknown) { - if (err instanceof Error) { - return expect(err.toString()).toEqual( - "Error: compressor should be a function, maybe you forgot to install the compressor" - ); - } - } + await expect(minify(settings)).rejects.toThrow( + "compressor should be a function, maybe you forgot to install the compressor" + ); }); }); diff --git a/packages/cssnano/__tests__/cssnano.test.ts b/packages/cssnano/__tests__/cssnano.test.ts index 5b9c65de0..65b99b5ab 100644 --- a/packages/cssnano/__tests__/cssnano.test.ts +++ b/packages/cssnano/__tests__/cssnano.test.ts @@ -41,10 +41,8 @@ describe("Package: cssnano", async () => { output: filesCSS.fileCSSOut, }; - try { - return await minify(settings); - } catch (err) { - return expect(err).not.toBeNull(); - } + await expect(minify(settings)).rejects.toThrow( + "cssnano minification failed" + ); }); }); diff --git a/packages/esbuild/__tests__/esbuild.test.ts b/packages/esbuild/__tests__/esbuild.test.ts index 63bcfc2ab..60d4b0ff4 100644 --- a/packages/esbuild/__tests__/esbuild.test.ts +++ b/packages/esbuild/__tests__/esbuild.test.ts @@ -55,11 +55,7 @@ describe("Package: esbuild", async () => { type: "js", }; - try { - return await minify(settings); - } catch (err) { - return expect(err).not.toBeNull(); - } + await expect(minify(settings)).rejects.toThrow("Transform failed"); }); }); @@ -100,10 +96,8 @@ describe("Package: esbuild", async () => { output: filesJS.fileJSOut, }; - try { - return await minify(settings); - } catch (err) { - return expect(err).toBeInstanceOf(Error); - } + await expect(minify(settings)).rejects.toThrow( + "You must specify a type: js or css" + ); }); }); diff --git a/packages/google-closure-compiler/__tests__/google-closure-compiler.test.ts b/packages/google-closure-compiler/__tests__/google-closure-compiler.test.ts index cdd9487ac..6cfeeb7e3 100644 --- a/packages/google-closure-compiler/__tests__/google-closure-compiler.test.ts +++ b/packages/google-closure-compiler/__tests__/google-closure-compiler.test.ts @@ -51,11 +51,7 @@ describe("Package: google-closure-compiler", async () => { output: filesJS.fileJSOut, }; - try { - return await minify(settings); - } catch (err) { - return expect(err).not.toBeNull(); - } + await expect(minify(settings)).rejects.toThrow("exited with code"); }); test("should compress with boolean options", async (): Promise => { diff --git a/packages/lightningcss/__tests__/lightningcss.test.ts b/packages/lightningcss/__tests__/lightningcss.test.ts index f56db44cf..2764609da 100644 --- a/packages/lightningcss/__tests__/lightningcss.test.ts +++ b/packages/lightningcss/__tests__/lightningcss.test.ts @@ -55,11 +55,7 @@ describe("Package: lightningcss", async () => { output: filesCSS.fileCSSOut, }; - try { - return await minify(settings); - } catch (err) { - return expect(err).not.toBeNull(); - } + await expect(minify(settings)).rejects.toThrow(SyntaxError); }); test("should throw an error for array content", async () => { diff --git a/packages/swc/__tests__/swc.test.ts b/packages/swc/__tests__/swc.test.ts index 819af0c51..ee0313f95 100644 --- a/packages/swc/__tests__/swc.test.ts +++ b/packages/swc/__tests__/swc.test.ts @@ -44,10 +44,6 @@ describe("Package: swc", async () => { output: filesJS.fileJSOut, }; - try { - return await minify(settings); - } catch (err) { - return expect(err).not.toBeNull(); - } + await expect(minify(settings)).rejects.toThrow(); }); }); diff --git a/packages/terser/__tests__/terser.test.ts b/packages/terser/__tests__/terser.test.ts index 4fe19f562..f71e3fcf3 100644 --- a/packages/terser/__tests__/terser.test.ts +++ b/packages/terser/__tests__/terser.test.ts @@ -39,10 +39,6 @@ describe("Package: terser", async () => { }, }; - try { - return await minify(settings); - } catch (err) { - return expect(err).not.toBeNull(); - } + await expect(minify(settings)).rejects.toThrow(); }); }); diff --git a/packages/uglify-js/__tests__/uglify-js.test.ts b/packages/uglify-js/__tests__/uglify-js.test.ts index 8a78805d3..c486121d6 100644 --- a/packages/uglify-js/__tests__/uglify-js.test.ts +++ b/packages/uglify-js/__tests__/uglify-js.test.ts @@ -35,10 +35,6 @@ describe("Package: uglify-js", async () => { output: filesJS.fileJSOut, }; - try { - return await minify(settings); - } catch (err) { - return expect(err).not.toBeNull(); - } + await expect(minify(settings)).rejects.toThrow(); }); }); From ce1a1c2d9105e9afad3a9e211ea141fe33aa1309 Mon Sep 17 00:00:00 2001 From: Rodolphe Stoclin Date: Mon, 23 Nov 2026 16:54:41 +0100 Subject: [PATCH 2/4] fix(oxc): report oxc parse errors instead of an empty result (#2954) --- .changeset/oxc-report-parse-errors.md | 7 ++++ packages/oxc/__tests__/oxc-error.test.ts | 50 ++++++++++++++++++++++++ packages/oxc/__tests__/oxc.test.ts | 8 ++-- packages/oxc/src/index.ts | 9 +++++ 4 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 .changeset/oxc-report-parse-errors.md diff --git a/.changeset/oxc-report-parse-errors.md b/.changeset/oxc-report-parse-errors.md new file mode 100644 index 000000000..6c99bc18c --- /dev/null +++ b/.changeset/oxc-report-parse-errors.md @@ -0,0 +1,7 @@ +--- +"@node-minify/oxc": patch +--- + +Fix the oxc compressor ignoring parse errors reported by `oxc-minify`. + +A JavaScript syntax error used to surface as `No content provided`, or as a silent success when `allowEmptyOutput` was set. It now fails with `oxc minification failed: `. diff --git a/packages/oxc/__tests__/oxc-error.test.ts b/packages/oxc/__tests__/oxc-error.test.ts index 92e738b23..5e5cb58c6 100644 --- a/packages/oxc/__tests__/oxc-error.test.ts +++ b/packages/oxc/__tests__/oxc-error.test.ts @@ -29,4 +29,54 @@ describe("Package: oxc error handling", () => { oxc({ settings: {}, content: "var x = 1;" }) ).rejects.toThrow("oxc"); }); + + test("should throw when oxc reports an error", async () => { + vi.doMock("oxc-minify", () => ({ + minify: async () => ({ + code: "", + errors: [ + { + severity: "Error", + message: "Unexpected token", + labels: [], + helpMessage: null, + codeframe: null, + }, + ], + }), + })); + + const { oxc } = await import("../src/index.ts"); + + await expect( + // @ts-expect-error testing invalid input: settings is missing required fields + oxc({ settings: {}, content: "var a = ;" }) + ).rejects.toThrow("oxc minification failed: Unexpected token"); + }); + + test("should not throw when oxc reports only warnings", async () => { + vi.doMock("oxc-minify", () => ({ + minify: async () => ({ + code: "var x=1;", + errors: [ + { + severity: "Warning", + message: "w", + labels: [], + helpMessage: null, + codeframe: null, + }, + ], + }), + })); + + const { oxc } = await import("../src/index.ts"); + + const result = await ( + // @ts-expect-error testing invalid input: settings is missing required fields + oxc({ settings: {}, content: "var x = 1;" }) + ); + + expect(result.code).toBe("var x=1;"); + }); }); diff --git a/packages/oxc/__tests__/oxc.test.ts b/packages/oxc/__tests__/oxc.test.ts index 54ad1d11e..231098404 100644 --- a/packages/oxc/__tests__/oxc.test.ts +++ b/packages/oxc/__tests__/oxc.test.ts @@ -44,10 +44,8 @@ describe("Package: oxc", async () => { output: filesJS.fileJSOut, }; - try { - return await minify(settings); - } catch (err) { - return expect(err).not.toBeNull(); - } + await expect(minify(settings)).rejects.toThrow( + "oxc minification failed" + ); }); }); diff --git a/packages/oxc/src/index.ts b/packages/oxc/src/index.ts index dda7be28d..b1ddcd05c 100644 --- a/packages/oxc/src/index.ts +++ b/packages/oxc/src/index.ts @@ -19,6 +19,7 @@ import { minify as oxcMinify } from "oxc-minify"; * @param settings - Minifier settings; `settings.options` are forwarded to oxc-minify (sourcemap enabled when `options.sourceMap` is truthy). * @param content - Input to minify; converted to a string before minification. * @returns The minified output: `code` contains the minified JavaScript, `map` contains the source map when available. + * @throws {Error} When oxc reports a parse error */ export async function oxc({ settings, @@ -35,6 +36,14 @@ export async function oxc({ ...restOptions, }); + // String(): Severity is an ambient const enum, unusable under isolatedModules + const errors = result.errors.filter( + (error) => String(error.severity) === "Error" + ); + if (errors.length > 0) { + throw new Error(errors.map((error) => error.message).join("; ")); + } + validateMinifyResult(result, "oxc"); return { From 8903c652bd65ef93e720ecdc1749436fc26a25ce Mon Sep 17 00:00:00 2001 From: Rodolphe Stoclin Date: Mon, 23 Nov 2026 16:54:46 +0100 Subject: [PATCH 3/4] fix(core): fail when an input glob matches no file (#2955) * fix(core): fail when an input glob matches no file * test(core): unique no-match glob path, precise @throws wording --- .changeset/fail-on-glob-with-no-match.md | 7 ++ .../core/__tests__/setup-no-match.test.ts | 78 +++++++++++++++++++ packages/core/src/setup.ts | 17 +++- 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 .changeset/fail-on-glob-with-no-match.md create mode 100644 packages/core/__tests__/setup-no-match.test.ts diff --git a/.changeset/fail-on-glob-with-no-match.md b/.changeset/fail-on-glob-with-no-match.md new file mode 100644 index 000000000..660486130 --- /dev/null +++ b/.changeset/fail-on-glob-with-no-match.md @@ -0,0 +1,7 @@ +--- +"@node-minify/core": patch +--- + +Fail when an `input` glob matches no file. + +`minify()` used to resolve with an empty string and write nothing when a wildcard input such as `src/**/*.js` matched nothing and the output used `$1`; with a plain output it failed with `No content provided`. Both cases now throw `No files matched input pattern: `. Explicit empty arrays and in-memory `content` are unaffected. diff --git a/packages/core/__tests__/setup-no-match.test.ts b/packages/core/__tests__/setup-no-match.test.ts new file mode 100644 index 000000000..2bf8fb656 --- /dev/null +++ b/packages/core/__tests__/setup-no-match.test.ts @@ -0,0 +1,78 @@ +/*! + * node-minify + * Copyright (c) 2011-2026 Rodolphe Stoclin + * MIT Licensed + */ + +import { randomUUID } from "node:crypto"; +import os from "node:os"; +import path from "node:path"; +import type { Compressor } from "@node-minify/types"; +import { describe, expect, test } from "vitest"; +import { setup } from "../src/setup.ts"; + +const compressor: Compressor = async () => ({ code: "" }); +const noMatch = path + .join(os.tmpdir(), `node-minify-no-such-dir-${randomUUID()}`, "**", "*.js") + .replaceAll("\\", "/"); + +describe("setup glob with no matching files", () => { + test("throws when a string glob with $1 output matches no file", () => { + expect(() => + setup({ + compressor, + input: noMatch, + output: "out/$1.min.js", + }) + ).toThrow("No files matched input pattern"); + }); + + test("throws when a string glob with plain output matches no file", () => { + expect(() => + setup({ + compressor, + input: noMatch, + output: "out.js", + }) + ).toThrow("No files matched input pattern"); + }); + + test("throws when an array containing only an unmatched glob is given", () => { + expect(() => + setup({ + compressor, + input: [noMatch], + output: "out.js", + }) + ).toThrow("No files matched input pattern"); + }); + + test("includes the unmatched pattern in the error message", () => { + expect(() => + setup({ + compressor, + input: noMatch, + output: "out.js", + }) + ).toThrow(noMatch); + }); + + test("does not throw from setup for a non-glob missing file", () => { + expect(() => + setup({ + compressor, + input: "missing.js", + output: "out.js", + }) + ).not.toThrow(); + }); + + test("does not throw from setup in in-memory mode", () => { + expect(() => + setup({ + compressor, + content: "var a = 1;", + }) + ).not.toThrow(); + }); +}); diff --git a/packages/core/src/setup.ts b/packages/core/src/setup.ts index a04b9c34e..50f10fb5c 100644 --- a/packages/core/src/setup.ts +++ b/packages/core/src/setup.ts @@ -22,6 +22,7 @@ const defaultSettings: Partial = { * * @param inputSettings - User-provided settings that override defaults * @returns The validated and enhanced Settings object ready for use + * @throws {Error} When wildcard expansion of the input leaves no file */ function setup( inputSettings: Settings @@ -66,6 +67,7 @@ function setup( * * @param settings - The initial settings to enhance * @returns The enhanced Settings object with derived inputs, outputs, and public-folder values applied + * @throws {Error} When wildcard expansion of the input leaves no file */ function enhanceSettings( settings: Settings @@ -73,10 +75,23 @@ function enhanceSettings( let enhancedSettings = settings; if (enhancedSettings.input) { + const requestedInput = enhancedSettings.input; enhancedSettings = { ...enhancedSettings, - ...wildcards(enhancedSettings.input, enhancedSettings.publicFolder), + ...wildcards(requestedInput, enhancedSettings.publicFolder), }; + + const patterns = [requestedInput].flat(); + const hasWildcard = patterns.some((pattern) => pattern.includes("*")); + if ( + hasWildcard && + Array.isArray(enhancedSettings.input) && + enhancedSettings.input.length === 0 + ) { + throw new Error( + `No files matched input pattern: ${patterns.join(", ")}` + ); + } } const optionsWithFormats = enhancedSettings.options as | { formats?: unknown[] } From c47416b69c66e4126a36062e58b44b95de2de6c6 Mon Sep 17 00:00:00 2001 From: Rodolphe Stoclin Date: Mon, 23 Nov 2026 17:24:57 +0100 Subject: [PATCH 4/4] docs(changeset): narrow the glob no-match wording (#2957) --- .changeset/fail-on-glob-with-no-match.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/fail-on-glob-with-no-match.md b/.changeset/fail-on-glob-with-no-match.md index 660486130..a67fa859e 100644 --- a/.changeset/fail-on-glob-with-no-match.md +++ b/.changeset/fail-on-glob-with-no-match.md @@ -2,6 +2,6 @@ "@node-minify/core": patch --- -Fail when an `input` glob matches no file. +Fail when no input file is left after glob expansion. -`minify()` used to resolve with an empty string and write nothing when a wildcard input such as `src/**/*.js` matched nothing and the output used `$1`; with a plain output it failed with `No content provided`. Both cases now throw `No files matched input pattern: `. Explicit empty arrays and in-memory `content` are unaffected. +`minify()` used to resolve with an empty string and write nothing when a wildcard input such as `src/**/*.js` matched nothing and the output used `$1`; with a plain output it failed with `No content provided`. Both cases now throw `No files matched input pattern: `. An array where at least one entry matches keeps working, and unmatched globs in it are skipped as before. Explicit empty arrays and in-memory `content` are unaffected.