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
-
9
1
jest . mock ( "node-fetch" , ( ) =>
10
2
jest . fn ( ) . mockResolvedValue ( {
11
3
ok : true ,
@@ -17,86 +9,53 @@ jest.mock("@jspm/generator", () => ({
17
9
parseUrlPkg : jest . fn ( ) ,
18
10
} ) ) ;
19
11
20
- let mockImportMapResolve = jest . fn ( ) ;
21
12
22
13
jest . mock ( "@jspm/import-map" , ( ) => ( {
23
14
ImportMap : jest . fn ( ( ) => ( {
24
- resolve : mockImportMapResolve ,
15
+ resolve : jest . fn ( ) ,
25
16
} ) ) ,
26
17
} ) ) ;
27
18
28
- jest . mock ( "src /utils" , ( ) => {
29
- const actual = jest . requireActual ( "src /utils" ) ;
19
+ jest . mock ( ".. /utils" , ( ) => {
20
+ const actual = jest . requireActual ( ".. /utils" ) ;
30
21
return {
31
22
__esModule : true ,
32
23
...actual ,
24
+ ensureDirSync : jest . fn ( ) ,
33
25
} ;
34
26
} ) ;
27
+ import * as utils from "../utils" ;
35
28
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" ) ;
43
30
44
- jest . mock ( "src /config" , ( ) => {
45
- const actual = jest . requireActual ( "src /config" ) ;
31
+ jest . mock ( ".. /config" , ( ) => {
32
+ const actual = jest . requireActual ( ".. /config" ) ;
46
33
return {
47
34
__esModule : true ,
48
35
...actual ,
49
36
cache : '' ,
50
37
}
51
38
} ) ;
52
39
40
+ import { resolve } from "../loader" ;
41
+
53
42
const nextResolve = jest . fn ( ) ;
54
43
const specifier = "specifier" ;
55
44
describe ( 'loader' , ( ) => {
56
45
afterEach ( ( ) => {
57
46
jest . clearAllMocks ( ) ;
58
47
} )
59
48
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 ) ;
66
56
const context = { parentURL : "parentURL" } ;
67
- const specifier = "specifier" ;
68
57
await resolve ( specifier , context , nextResolve ) ;
69
- expect ( constructImportMapSpy ) . toHaveBeenCalled ( ) ;
58
+ expect ( checkIfNodeOrFileProtocolSpy ) . toHaveBeenCalled ( ) ;
70
59
expect ( nextResolve ) . toHaveBeenCalledWith ( 'specifier' ) ;
71
60
} ) ;
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
- } ) ;
102
61
} ) ;
0 commit comments