|
| 1 | +import { |
| 2 | + checkIfNodeOrFileProtocol, |
| 3 | + ensureDirSync, |
| 4 | + ensureFileSync, |
| 5 | + resolveModulePath, |
| 6 | + resolveNodeModuleCachePath, |
| 7 | + resolveParsedModulePath |
| 8 | +} from '../utils'; |
| 9 | + |
| 10 | +jest.mock('node:fs'); |
| 11 | +jest.mock('node:path'); |
1 | 12 | import * as fs from "node:fs"; |
2 | | -import { ensureDirSync } from 'src/utils'; |
| 13 | +import * as path from "node:path"; |
| 14 | + |
| 15 | +jest.mock("@jspm/import-map", () => ({ |
| 16 | + ImportMap: jest.fn(() => ({ |
| 17 | + resolve: jest.fn(), |
| 18 | + })), |
| 19 | +})); |
| 20 | + |
| 21 | +jest.mock('@jspm/generator', () => ({ |
| 22 | + parseUrlPkg: jest.fn(), |
| 23 | +})) |
3 | 24 |
|
4 | | -jest.mock("node:fs"); |
| 25 | +import * as generator from '@jspm/generator'; |
| 26 | + |
| 27 | +jest.mock('../config') |
| 28 | +import * as config from '../config'; |
| 29 | + |
| 30 | +jest.mock('../parser', () => ({ |
| 31 | + parseNodeModuleCachePath: jest.fn(), |
| 32 | +})) |
| 33 | +import * as parser from '../parser'; |
5 | 34 |
|
6 | 35 | test("ensureDirSync has dir", () => { |
7 | 36 | const dir = "/path/to/dir"; |
8 | 37 | const existsSyncMock = jest.spyOn(fs, 'existsSync').mockReturnValue(true); |
| 38 | + const dirnameMock = jest.spyOn(path, 'dirname') |
9 | 39 | ensureDirSync(dir); |
10 | 40 | expect(existsSyncMock).toBeCalledWith(dir); |
11 | | - expect(fs.mkdirSync).not.toBeCalled(); |
| 41 | + expect(dirnameMock).not.toBeCalled(); |
12 | 42 | }); |
13 | 43 |
|
14 | | -test("ensureDirSync has no dir", () => { |
| 44 | +test("ensureDirSync has parent dir", () => { |
15 | 45 | const dir = "/path/to/dir"; |
16 | 46 | const existsSyncMock = jest.spyOn(fs, 'existsSync').mockReturnValue(false); |
| 47 | + const dirnameMock = jest.spyOn(path, 'dirname').mockReturnValue("/path/to/dir"); |
17 | 48 | const mkdirSyncMock = jest.spyOn(fs, 'mkdirSync') |
18 | 49 | ensureDirSync(dir); |
19 | 50 | expect(existsSyncMock).toBeCalledWith(dir); |
20 | | - expect(mkdirSyncMock).toBeCalledWith(dir); |
| 51 | + expect(dirnameMock).toBeCalledWith(dir); |
| 52 | + expect(mkdirSyncMock).toHaveBeenCalledTimes(1); |
21 | 53 | }); |
| 54 | + |
| 55 | +test("ensureDirSync to have recursion", () => { |
| 56 | + const dir = "/path/to/dir"; |
| 57 | + const existsSyncMock = jest.spyOn(fs, 'existsSync').mockReturnValue(false); |
| 58 | + const dirnameMock = jest.spyOn(path, 'dirname').mockReturnValue("/path/"); |
| 59 | + const mkdirSyncMock = jest.spyOn(fs, 'mkdirSync') |
| 60 | + ensureDirSync(dir); |
| 61 | + expect(existsSyncMock).toBeCalledWith(dir); |
| 62 | + expect(dirnameMock).toBeCalledWith(dir); |
| 63 | + expect(mkdirSyncMock).toHaveBeenCalledTimes(2); |
| 64 | +}); |
| 65 | + |
| 66 | +test("ensureFileSync has file", () => { |
| 67 | + const dir = 'path/to/file'; |
| 68 | + const dirnameMock = jest.spyOn(path, 'dirname').mockReturnValue("/path/to/dir"); |
| 69 | + const existsSyncMock = jest.spyOn(fs, 'existsSync').mockReturnValue(true); |
| 70 | + const writeFileSyncSpy = jest.spyOn(fs, 'writeFileSync'); |
| 71 | + ensureFileSync(dir); |
| 72 | + expect(dirnameMock).toBeCalled(); |
| 73 | + expect(existsSyncMock).toBeCalled(); |
| 74 | + expect(writeFileSyncSpy).toBeCalled(); |
| 75 | +}); |
| 76 | + |
| 77 | +test('checkIfNodeOrFileProtocol returns true', () => { |
| 78 | + const protocol = 'node:foo'; |
| 79 | + expect(checkIfNodeOrFileProtocol(protocol)).toBeTruthy(); |
| 80 | +}) |
| 81 | + |
| 82 | +test('checkIfNodeOrFileProtocol returns false', () => { |
| 83 | + const protocol = 'http:foo'; |
| 84 | + expect(checkIfNodeOrFileProtocol(protocol)).toBeFalsy(); |
| 85 | +}) |
| 86 | + |
| 87 | +test('resolveModulePath', () => { |
| 88 | + const specifier = 'foo'; |
| 89 | + const cacheMapPath = 'file:///bar'; |
| 90 | + const resolve = jest.fn().mockReturnValue('file:///bar/foo'); |
| 91 | + (jest.mocked(config).importmap as unknown) = { |
| 92 | + resolve |
| 93 | + } |
| 94 | + const result = resolveModulePath(specifier, cacheMapPath); |
| 95 | + expect(result).toBe('file:///bar/foo'); |
| 96 | +}) |
| 97 | + |
| 98 | +test('resolveModulePath with modulePath', async () => { |
| 99 | + const parseUrlPkgSpy = await jest.spyOn(generator, 'parseUrlPkg').mockResolvedValue({ |
| 100 | + pkg: { |
| 101 | + name: 'foo', |
| 102 | + version: '1.0.0', |
| 103 | + } |
| 104 | + } as any); |
| 105 | + (jest.mocked(config).cache as unknown) = 'test/.cache' |
| 106 | + const modulePath = 'file:///bar/index.js'; |
| 107 | + const joinSpy = jest.spyOn(path, 'join').mockReturnValue('test/.cache/foo@1.0.0/bar/index.js'); |
| 108 | + const result = await resolveNodeModuleCachePath(modulePath); |
| 109 | + expect(parseUrlPkgSpy).toBeCalledWith(modulePath); |
| 110 | + expect(joinSpy).toBeCalled(); |
| 111 | + expect(result).toBe('test/.cache/foo@1.0.0/bar/index.js'); |
| 112 | +}); |
| 113 | + |
| 114 | +test('resolveParsedModulePath', async () => { |
| 115 | + const parseNodeModuleCachePathSpy = await jest.spyOn(parser, 'parseNodeModuleCachePath').mockResolvedValue('file:///foo/bar'); |
| 116 | + const result = await resolveParsedModulePath('file:///foo/bar', 'file:///foo/bar'); |
| 117 | + expect(parseNodeModuleCachePathSpy).toBeCalled(); |
| 118 | + expect(result).toBe('file:///foo/bar'); |
| 119 | +}) |
0 commit comments