Skip to content

Commit

Permalink
add npm test
Browse files Browse the repository at this point in the history
  • Loading branch information
pzmosquito committed Jun 23, 2023
1 parent f2d5719 commit a91c0f3
Show file tree
Hide file tree
Showing 5 changed files with 5,187 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ container := eslint-import-resolver-vite

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

.PHONY: shell
shell:
Expand Down
54 changes: 54 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const resolve = require("resolve");
const resolver = require("./index");

jest.mock("resolve");
jest.mock("fs");

describe("Resolver Plugin Tests", () => {
beforeEach(() => {
jest.restoreAllMocks();
});

test("should resolve core module", () => {
resolve.isCore.mockReturnValue(true);

const result = resolver.resolve("fs", "/path/to/file.js", {});

expect(result.found).toBe(true);
expect(result.path).toBe(null);
});

test("should resolve non-core module", () => {
resolve.sync = jest.fn((source) => "/path/to/resolved.js");

jest.mock("/path/to/vite.config.js", () => ({
default: {
resolve: {
extensions: [".js"],
alias: {
"_": "/path/to/src",
},
},
}
}), { virtual: true });

const result = resolver.resolve("_/module", "/path/to/file.js", { configPath: "/path/to/vite.config.js" });

expect(result.found).toBe(true);
expect(result.path).toBe("/path/to/resolved.js");
expect(resolve.sync).toHaveBeenCalledWith("/path/to/src/module", {
basedir: "/path/to",
extensions: [".js"],
});
});

test("should handle resolve error", () => {
resolve.sync = jest.fn(() => {
throw new Error("Resolve error");
});

const result = resolver.resolve("module", "/path/to/file.js", {});

expect(result.found).toBe(false);
});
});
Loading

0 comments on commit a91c0f3

Please sign in to comment.