|
| 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