@@ -3,15 +3,22 @@ import { parseUrlPkg } from "@jspm/generator";
33import { ImportMap } from "@jspm/import-map" ;
44import * as utils from "src/utils" ;
55import * as config from "src/config" ;
6+ import * as parser from "src/parser" ;
67
78
8- jest . mock ( "node-fetch" , ( ) => jest . fn ( ) ) ;
9+ jest . mock ( "node-fetch" , ( ) =>
10+ jest . fn ( ) . mockResolvedValue ( {
11+ ok : true ,
12+ text : jest . fn ( ) . mockResolvedValue ( "module code" ) ,
13+ } )
14+ ) ;
915
1016jest . mock ( "@jspm/generator" , ( ) => ( {
1117 parseUrlPkg : jest . fn ( ) ,
1218} ) ) ;
1319
1420let mockImportMapResolve = jest . fn ( ) ;
21+
1522jest . mock ( "@jspm/import-map" , ( ) => ( {
1623 ImportMap : jest . fn ( ( ) => ( {
1724 resolve : mockImportMapResolve ,
@@ -26,6 +33,14 @@ jest.mock("src/utils", () => {
2633 } ;
2734} ) ;
2835
36+ jest . mock ( "src/parser" , ( ) => {
37+ const actual = jest . requireActual ( "src/parser" ) ;
38+ return {
39+ __esModule : true ,
40+ ...actual ,
41+ } ;
42+ } ) ;
43+
2944jest . mock ( "src/config" , ( ) => {
3045 const actual = jest . requireActual ( "src/config" ) ;
3146 return {
@@ -43,56 +58,6 @@ describe('loader', () => {
4358 jest . clearAllMocks ( ) ;
4459 } )
4560
46- test ( "resolved with no cache path error" , async ( ) => {
47- const context = { parentURL : undefined } ;
48- await resolve ( specifier , context , nextResolve ) ;
49- expect ( errorSpy ) . toHaveBeenCalledWith ( "jspm:[loader]: resolve: Error: Failed in resolving cache path" ) ;
50- } ) ;
51-
52- test ( "resolved with no modulePath error" , async ( ) => {
53- const cachePath = 'test/.cache' ;
54- ( jest . mocked ( config ) . cache as string ) = cachePath
55- mockImportMapResolve = jest . fn ( ) . mockReturnValue ( undefined ) ;
56- const constructImportMapSpy = jest . spyOn ( utils , "constructImportMap" ) . mockReturnValue ( new ImportMap ( { } ) ) ;
57- const context = { parentURL : "parentURL" } ;
58- await resolve ( specifier , context , nextResolve ) ;
59- expect ( constructImportMapSpy ) . toHaveBeenCalled ( ) ;
60- expect ( errorSpy ) . toHaveBeenCalledWith ( "jspm:[loader]: resolve: Error: Failed in resolving module path" ) ;
61- } ) ;
62-
63- test ( "resolved with URL error" , async ( ) => {
64- const cachePath = 'test/.cache' ;
65- ( jest . mocked ( config ) . cache as string ) = cachePath
66- mockImportMapResolve = jest . fn ( ) . mockReturnValue ( "./some/path" ) ;
67- const constructImportMapSpy = jest . spyOn ( utils , "constructImportMap" ) . mockReturnValue ( new ImportMap ( { } ) ) ;
68- const context = { parentURL : "parentURL" } ;
69- await resolve ( specifier , context , nextResolve ) ;
70- expect ( constructImportMapSpy ) . toHaveBeenCalled ( ) ;
71- expect ( errorSpy ) . toHaveBeenCalledWith ( "jspm:[loader]: resolve: TypeError [ERR_INVALID_URL]: Invalid URL" ) ;
72- } ) ;
73-
74- test ( "resolved with `isNode` protocol error" , async ( ) => {
75- const cachePath = 'test/.cache' ;
76- ( jest . mocked ( config ) . cache as string ) = cachePath
77- mockImportMapResolve = jest . fn ( ) . mockReturnValue ( "node:/some/path" ) ;
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 ( nextResolve ) . toHaveBeenCalledWith ( 'specifier' ) ;
83- } ) ;
84-
85- test ( "resolved with `isFile` protocol error" , async ( ) => {
86- const cachePath = 'test/.cache' ;
87- ( jest . mocked ( config ) . cache as string ) = cachePath
88- mockImportMapResolve = jest . fn ( ) . mockReturnValue ( "node:/some/path" ) ;
89- const constructImportMapSpy = jest . spyOn ( utils , "constructImportMap" ) . mockReturnValue ( new ImportMap ( { } ) ) ;
90- const context = { parentURL : "parentURL" } ;
91- await resolve ( specifier , context , nextResolve ) ;
92- expect ( constructImportMapSpy ) . toHaveBeenCalled ( ) ;
93- expect ( nextResolve ) . toHaveBeenCalledWith ( 'specifier' ) ;
94- } ) ;
95-
9661 test ( "resolved with parseUrlPkg error" , async ( ) => {
9762 const cachePath = 'test/.cache' ;
9863 ( jest . mocked ( config ) . cache as string ) = cachePath
@@ -116,7 +81,7 @@ describe('loader', () => {
11681 await resolve ( specifier , context , nextResolve ) ;
11782 expect ( constructImportMapSpy ) . toHaveBeenCalled ( ) ;
11883 expect ( parseUrlPkg ) . toHaveBeenCalled ( ) ;
119- expect ( errorSpy ) . toHaveBeenCalledWith ( "jspm:[loader]: resolve: Error: Failed in parsing node module cache path" ) ;
84+ expect ( nextResolve ) . toHaveBeenCalledWith ( "test/. cache/name@version/ path" ) ;
12085 } ) ;
12186
12287 test ( "resolved without an error" , async ( ) => {
@@ -127,14 +92,12 @@ describe('loader', () => {
12792 const constructImportMapSpy = jest . spyOn ( utils , "constructImportMap" ) . mockReturnValue ( new ImportMap ( { } ) ) ;
12893 const context = { parentURL : "parentURL" } ;
12994 const parseNodeModuleCachePathSpy = jest
130- . spyOn ( utils , "parseNodeModuleCachePath" )
95+ . spyOn ( parser , "parseNodeModuleCachePath" )
13196 . mockResolvedValue ( "node_modules/name/version" ) ;
132- const nodeModuleCachePathSpy = jest . spyOn ( utils , 'constructPath' ) . mockReturnValue ( 'node_modules/name/version' )
13397 await resolve ( specifier , context , nextResolve ) ;
13498 expect ( constructImportMapSpy ) . toHaveBeenCalled ( ) ;
13599 expect ( parseUrlPkg ) . toHaveBeenCalled ( ) ;
136100 expect ( parseNodeModuleCachePathSpy ) . toHaveBeenCalled ( ) ;
137- expect ( nodeModuleCachePathSpy ) . toHaveBeenCalled ( ) ;
138101 expect ( nextResolve ) . toHaveBeenCalledWith ( "node_modules/name/version" ) ;
139102 } ) ;
140103} ) ;
0 commit comments