Skip to content

Commit cb1efa1

Browse files
committed
refactor(tests): modernize __dirname to import.meta.url
Replace legacy __dirname with import.meta.url pattern across 11 test files. ESM-compatible and consistent with source file conventions.
1 parent ed39b84 commit cb1efa1

File tree

11 files changed

+39
-2
lines changed

11 files changed

+39
-2
lines changed

packages/core/__tests__/compress_async.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import fs from "node:fs";
22
import path from "node:path";
3+
import { fileURLToPath } from "node:url";
34
import type { Settings } from "@node-minify/types";
45
import { afterAll, describe, expect, test, vi } from "vitest";
56
import { noCompress } from "../../no-compress/src/index.ts";
67
import { compress } from "../src/compress.ts";
78

9+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
10+
811
describe("compress async", () => {
912
const tempDir = path.join(__dirname, "temp_async");
1013
if (!fs.existsSync(tempDir)) {

packages/core/__tests__/core.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import childProcess from "node:child_process";
88
import { statSync } from "node:fs";
9+
import path from "node:path";
10+
import { fileURLToPath } from "node:url";
911
import type { Compressor, Settings } from "@node-minify/types";
1012
import {
1113
afterAll,
@@ -17,6 +19,8 @@ import {
1719
vi,
1820
} from "vitest";
1921

22+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
23+
2024
vi.mock("node:fs", async (importOriginal) => {
2125
const actual = await importOriginal<typeof import("node:fs")>();
2226
return {

packages/core/__tests__/image-integration.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
*/
66

77
import { readFileSync } from "node:fs";
8-
import { resolve } from "node:path";
8+
import path, { resolve } from "node:path";
9+
import { fileURLToPath } from "node:url";
910
import { describe, expect, test } from "vitest";
1011
import { sharp } from "../../sharp/src/index.ts";
1112
import { svgo } from "../../svgo/src/index.ts";
1213
import { minify } from "../src/index.ts";
1314

15+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1416
const testPng = resolve(__dirname, "../../../tests/fixtures/images/test.png");
1517
const testSvg = resolve(__dirname, "../../../tests/fixtures/images/test.svg");
1618

packages/run/__tests__/run.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import childProcess from "node:child_process";
88
import { EventEmitter } from "node:events";
9+
import path from "node:path";
10+
import { fileURLToPath } from "node:url";
911
import {
1012
afterAll,
1113
beforeAll,
@@ -17,6 +19,7 @@ import {
1719
} from "vitest";
1820
import { type RunCommandLineParams, runCommandLine } from "../src/index.ts";
1921

22+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
2023
const jar = `${__dirname}/../../yui/src/binaries/yuicompressor-2.4.7.jar`;
2124

2225
type Command = {

packages/svgo/__tests__/svgo.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
*/
66

77
import { readFileSync } from "node:fs";
8-
import { resolve } from "node:path";
8+
import path, { resolve } from "node:path";
9+
import { fileURLToPath } from "node:url";
910
import { describe, expect, test } from "vitest";
1011
import { svgo } from "../src/index.ts";
1112

13+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1214
const testSvg = resolve(__dirname, "../../../tests/fixtures/images/test.svg");
1315

1416
describe("svgo", () => {

packages/utils/__tests__/compressor-resolver.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import { existsSync } from "node:fs";
88
import path from "node:path";
9+
import { fileURLToPath } from "node:url";
910
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
1011
import {
1112
getKnownExportName,
@@ -14,6 +15,7 @@ import {
1415
} from "../src/compressor-resolver.ts";
1516
import { deleteFile, writeFile } from "../src/index.ts";
1617

18+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1719
const tmpDir = path.resolve(__dirname, "../../../tests/tmp");
1820
const testCompressorPath = path.join(tmpDir, "test-compressor.mjs");
1921

packages/utils/__tests__/getContentFromFilesAsync.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
*/
66

77
import { readFile } from "node:fs/promises";
8+
import path from "node:path";
9+
import { fileURLToPath } from "node:url";
810
import { describe, expect, test, vi } from "vitest";
911
import { getContentFromFilesAsync } from "../src/getContentFromFiles.ts";
1012

13+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
14+
1115
vi.mock("node:fs/promises", async (importOriginal) => {
1216
const actual = await importOriginal<typeof import("node:fs/promises")>();
1317
return {

packages/utils/__tests__/isValidFileAsync.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
*/
66

77
import { lstat } from "node:fs/promises";
8+
import path from "node:path";
9+
import { fileURLToPath } from "node:url";
810
import { describe, expect, test, vi } from "vitest";
911
import { FileOperationError } from "../src/error.ts";
1012
import { isValidFileAsync } from "../src/isValidFile.ts";
1113

14+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
15+
const __filename = fileURLToPath(import.meta.url);
16+
1217
vi.mock("node:fs/promises", async (importOriginal) => {
1318
const actual = await importOriginal<typeof import("node:fs/promises")>();
1419
return {

packages/utils/__tests__/utils.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
*/
66

77
import { existsSync, lstatSync, unlinkSync } from "node:fs";
8+
import path from "node:path";
9+
import { fileURLToPath } from "node:url";
810
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
911

12+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
13+
1014
vi.mock("node:fs", async (importOriginal) => {
1115
const actual = await importOriginal<typeof import("node:fs")>();
1216
return {

tests/files-path.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import path from "node:path";
2+
import { fileURLToPath } from "node:url";
3+
4+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
5+
16
/**
27
* JavaScript
38
*/

0 commit comments

Comments
 (0)