Skip to content

Commit 54e1167

Browse files
committed
feat: init loader tests (getting closer)
1 parent 75c03d0 commit 54e1167

File tree

1 file changed

+18
-59
lines changed

1 file changed

+18
-59
lines changed

src/__tests__/loader.test.ts

Lines changed: 18 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
import { resolve } from "src/loader";
2-
import { parseUrlPkg } from "@jspm/generator";
3-
import { ImportMap } from "@jspm/import-map";
4-
import * as utils from "src/utils";
5-
import * as config from "src/config";
6-
import * as parser from "src/parser";
7-
8-
91
jest.mock("node-fetch", () =>
102
jest.fn().mockResolvedValue({
113
ok: true,
@@ -17,86 +9,53 @@ jest.mock("@jspm/generator", () => ({
179
parseUrlPkg: jest.fn(),
1810
}));
1911

20-
let mockImportMapResolve = jest.fn();
2112

2213
jest.mock("@jspm/import-map", () => ({
2314
ImportMap: jest.fn(() => ({
24-
resolve: mockImportMapResolve,
15+
resolve: jest.fn(),
2516
})),
2617
}));
2718

28-
jest.mock("src/utils", () => {
29-
const actual = jest.requireActual("src/utils");
19+
jest.mock("../utils", () => {
20+
const actual = jest.requireActual("../utils");
3021
return {
3122
__esModule: true,
3223
...actual,
24+
ensureDirSync: jest.fn(),
3325
};
3426
});
27+
import * as utils from "../utils";
3528

36-
jest.mock("src/parser", () => {
37-
const actual = jest.requireActual("src/parser");
38-
return {
39-
__esModule: true,
40-
...actual,
41-
};
42-
});
29+
jest.mock("../parser");
4330

44-
jest.mock("src/config", () => {
45-
const actual = jest.requireActual("src/config");
31+
jest.mock("../config", () => {
32+
const actual = jest.requireActual("../config");
4633
return {
4734
__esModule: true,
4835
...actual,
4936
cache: '',
5037
}
5138
});
5239

40+
import { resolve } from "../loader";
41+
5342
const nextResolve = jest.fn();
5443
const specifier = "specifier";
5544
describe('loader', () => {
5645
afterEach(() => {
5746
jest.clearAllMocks();
5847
})
5948

60-
test("resolved with parseUrlPkg error", async () => {
61-
const cachePath = 'test/.cache';
62-
(jest.mocked(config).cache as string) = cachePath
63-
mockImportMapResolve = jest.fn().mockReturnValue("https://ga.jspm.io/some/path");
64-
const constructImportMapSpy = jest.spyOn(utils, "constructImportMap").mockReturnValue(new ImportMap({}));
65-
const nextResolve = jest.fn();
49+
test("resolved with basic config", async () => {
50+
// const nodeImportMapPath = 'test/node.importmap';
51+
// (jest.mocked(config).nodeImportMapPath as any) = nodeImportMapPath;
52+
// (jest.mocked(config).cacheMap as any) = (new Map() as any).mockImplementation(() => ({
53+
// get: jest.fn().mockReturnValue(undefined),
54+
// }));
55+
const checkIfNodeOrFileProtocolSpy = jest.spyOn(utils, 'checkIfNodeOrFileProtocol').mockReturnValue(true);
6656
const context = { parentURL: "parentURL" };
67-
const specifier = "specifier";
6857
await resolve(specifier, context, nextResolve);
69-
expect(constructImportMapSpy).toHaveBeenCalled();
58+
expect(checkIfNodeOrFileProtocolSpy).toHaveBeenCalled();
7059
expect(nextResolve).toHaveBeenCalledWith('specifier');
7160
});
72-
73-
test("resolved with parsing node module cache path error", async () => {
74-
const cachePath = 'test/.cache';
75-
(jest.mocked(config).cache as string) = cachePath
76-
mockImportMapResolve = jest.fn().mockReturnValue("https://ga.jspm.io/some/path");
77-
await (parseUrlPkg as jest.Mock).mockResolvedValue({ pkg: { name: "name", version: "version" } });
78-
const constructImportMapSpy = jest.spyOn(utils, "constructImportMap").mockReturnValue(new ImportMap({}));
79-
const context = { parentURL: "parentURL" };
80-
await resolve(specifier, context, nextResolve);
81-
expect(constructImportMapSpy).toHaveBeenCalled();
82-
expect(parseUrlPkg).toHaveBeenCalled();
83-
expect(nextResolve).toHaveBeenCalledWith("test/.cache/name@version/path");
84-
});
85-
86-
test("resolved without an error", async () => {
87-
const cachePath = 'test/.cache';
88-
(jest.mocked(config).cache as string) = cachePath
89-
mockImportMapResolve = jest.fn().mockReturnValue("https://ga.jspm.io/some/path");
90-
await (parseUrlPkg as jest.Mock).mockResolvedValue({ pkg: { name: "name", version: "version" } });
91-
const constructImportMapSpy = jest.spyOn(utils, "constructImportMap").mockReturnValue(new ImportMap({}));
92-
const context = { parentURL: "parentURL" };
93-
const parseNodeModuleCachePathSpy = jest
94-
.spyOn(parser, "parseNodeModuleCachePath")
95-
.mockResolvedValue("node_modules/name/version");
96-
await resolve(specifier, context, nextResolve);
97-
expect(constructImportMapSpy).toHaveBeenCalled();
98-
expect(parseUrlPkg).toHaveBeenCalled();
99-
expect(parseNodeModuleCachePathSpy).toHaveBeenCalled();
100-
expect(nextResolve).toHaveBeenCalledWith("node_modules/name/version");
101-
});
10261
});

0 commit comments

Comments
 (0)