Skip to content

Commit a91c0f3

Browse files
committed
add npm test
1 parent f2d5719 commit a91c0f3

File tree

5 files changed

+5187
-1
lines changed

5 files changed

+5187
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ container := eslint-import-resolver-vite
22

33
.PHONY: dev
44
dev:
5-
docker run -t -i -v `pwd`:/usr/app -w /usr/app --rm --name $(container) node:12-alpine
5+
docker run -t -i -v `pwd`:/usr/app -w /usr/app --rm --name $(container) node:18-alpine
66

77
.PHONY: shell
88
shell:

index.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const resolve = require("resolve");
2+
const resolver = require("./index");
3+
4+
jest.mock("resolve");
5+
jest.mock("fs");
6+
7+
describe("Resolver Plugin Tests", () => {
8+
beforeEach(() => {
9+
jest.restoreAllMocks();
10+
});
11+
12+
test("should resolve core module", () => {
13+
resolve.isCore.mockReturnValue(true);
14+
15+
const result = resolver.resolve("fs", "/path/to/file.js", {});
16+
17+
expect(result.found).toBe(true);
18+
expect(result.path).toBe(null);
19+
});
20+
21+
test("should resolve non-core module", () => {
22+
resolve.sync = jest.fn((source) => "/path/to/resolved.js");
23+
24+
jest.mock("/path/to/vite.config.js", () => ({
25+
default: {
26+
resolve: {
27+
extensions: [".js"],
28+
alias: {
29+
"_": "/path/to/src",
30+
},
31+
},
32+
}
33+
}), { virtual: true });
34+
35+
const result = resolver.resolve("_/module", "/path/to/file.js", { configPath: "/path/to/vite.config.js" });
36+
37+
expect(result.found).toBe(true);
38+
expect(result.path).toBe("/path/to/resolved.js");
39+
expect(resolve.sync).toHaveBeenCalledWith("/path/to/src/module", {
40+
basedir: "/path/to",
41+
extensions: [".js"],
42+
});
43+
});
44+
45+
test("should handle resolve error", () => {
46+
resolve.sync = jest.fn(() => {
47+
throw new Error("Resolve error");
48+
});
49+
50+
const result = resolver.resolve("module", "/path/to/file.js", {});
51+
52+
expect(result.found).toBe(false);
53+
});
54+
});

0 commit comments

Comments
 (0)