1
1
const resolve = require ( "resolve" ) ;
2
+ const fs = require ( "fs" ) ;
2
3
const resolver = require ( "./index" ) ;
3
4
4
5
jest . mock ( "resolve" ) ;
5
6
jest . mock ( "fs" ) ;
6
7
7
8
describe ( "Resolver Plugin Tests" , ( ) => {
8
- beforeEach ( ( ) => {
9
- jest . restoreAllMocks ( ) ;
10
- } ) ;
11
-
12
9
test ( "should resolve core module" , ( ) => {
13
10
resolve . isCore . mockReturnValue ( true ) ;
14
11
@@ -18,8 +15,9 @@ describe("Resolver Plugin Tests", () => {
18
15
expect ( result . path ) . toBe ( null ) ;
19
16
} ) ;
20
17
21
- test ( "should resolve non-core module with ESM vite config " , ( ) => {
18
+ test ( "should resolve non-core module" , ( ) => {
22
19
resolve . sync = jest . fn ( ( ) => "/path/to/resolved.js" ) ;
20
+ fs . existsSync = jest . fn ( ( ) => true ) ;
23
21
24
22
jest . mock ( "/path/to/vite.config.js" , ( ) => ( {
25
23
default : {
@@ -37,39 +35,89 @@ describe("Resolver Plugin Tests", () => {
37
35
38
36
expect ( result . found ) . toBe ( true ) ;
39
37
expect ( result . path ) . toBe ( "/path/to/resolved.js" ) ;
40
- expect ( resolve . sync ) . toHaveBeenCalledWith ( "/path/to/src/module" , {
41
- basedir : "/path/to" ,
42
- extensions : [ ".js" ] ,
38
+ expect ( resolve . sync ) . toHaveBeenCalledWith (
39
+ "/path/to/src/module" ,
40
+ { basedir : "/path/to" , extensions : [ ".js" ] }
41
+ ) ;
42
+ } ) ;
43
+
44
+ test ( "should resolve non-core module with publicDir" , ( ) => {
45
+ resolve . sync = jest . fn ( ( ) => "/path/to/resolved.js" ) ;
46
+ fs . existsSync = jest . fn ( ( param ) => {
47
+ if ( param === "/path/to/vite.config.js" ) {
48
+ return true ;
49
+ }
50
+ return false ;
43
51
} ) ;
52
+
53
+ jest . mock ( "/path/to/vite.config.js" , ( ) => ( {
54
+ default : {
55
+ resolve : {
56
+ extensions : [ ".js" ] ,
57
+ alias : {
58
+ "_" : "/path/to/src" ,
59
+ } ,
60
+ } ,
61
+ publicDir : "/path/to/public" ,
62
+ } ,
63
+ } ) , { virtual : true } ) ;
64
+
65
+ // JS module
66
+ const result = resolver . resolve ( "/_/module" , "/path/to/file.js" , { configPath : "/path/to/vite.config.js" } ) ;
67
+
68
+ expect ( result . found ) . toBe ( true ) ;
69
+ expect ( result . path ) . toBe ( "/path/to/resolved.js" ) ;
70
+ expect ( resolve . sync ) . toHaveBeenCalledWith (
71
+ "/path/to/public/path/to/src/module" ,
72
+ { basedir : "/path/to" , extensions : [ ".js" ] }
73
+ ) ;
44
74
} ) ;
45
75
46
- test ( "should resolve non-core module with CJS vite config " , ( ) => {
76
+ test ( "should resolve non-core module with absolute path " , ( ) => {
47
77
resolve . sync = jest . fn ( ( ) => "/path/to/resolved.js" ) ;
78
+ fs . existsSync = jest . fn ( ( ) => true ) ;
48
79
49
80
jest . mock ( "/path/to/vite.config.js" , ( ) => ( {
50
- resolve : {
51
- extensions : [ ".js" ] ,
52
- alias : {
53
- "_" : "/path/to/src" ,
81
+ default : {
82
+ resolve : {
83
+ extensions : [ ".js" ] ,
84
+ alias : {
85
+ "_" : "/path/to/src" ,
86
+ } ,
54
87
} ,
88
+ publicDir : "/path/to/public" ,
55
89
} ,
56
90
} ) , { virtual : true } ) ;
57
91
58
92
// JS module
59
- let result = resolver . resolve ( "_ /module" , "/path/to/file.js" , { configPath : "/path/to/vite.config.js" } ) ;
93
+ const result = resolver . resolve ( "/path/to /module" , "/path/to/file.js" , { configPath : "/path/to/vite.config.js" } ) ;
60
94
61
95
expect ( result . found ) . toBe ( true ) ;
62
96
expect ( result . path ) . toBe ( "/path/to/resolved.js" ) ;
63
- expect ( resolve . sync ) . toHaveBeenCalledWith ( "/path/to/src/module" , {
64
- basedir : "/path/to" ,
65
- extensions : [ ".js" ] ,
97
+ expect ( resolve . sync ) . toHaveBeenCalledWith (
98
+ "/path/to/module" ,
99
+ { basedir : "/path/to" , extensions : [ ".js" ] }
100
+ ) ;
101
+ } ) ;
102
+
103
+ test ( "should handle vite config DNE error" , ( ) => {
104
+ fs . existsSync = jest . fn ( ( param ) => {
105
+ if ( param === "/path/to/vite.config.js" ) {
106
+ return false ;
107
+ }
108
+ return true ;
66
109
} ) ;
110
+
111
+ const result = resolver . resolve ( "module" , "/path/to/file.js" , { configPath : "/path/to/vite.config.js" } ) ;
112
+
113
+ expect ( result . found ) . toBe ( false ) ;
67
114
} ) ;
68
115
69
116
test ( "should handle resolve error" , ( ) => {
70
117
resolve . sync = jest . fn ( ( ) => {
71
118
throw new Error ( "Resolve error" ) ;
72
119
} ) ;
120
+ fs . existsSync = jest . fn ( ( param ) => true ) ;
73
121
74
122
const result = resolver . resolve ( "module" , "/path/to/file.js" , { } ) ;
75
123
0 commit comments