1
1
import { describe , expect , it } from "vitest" ;
2
2
3
- import { optionalDepRule } from "./optional-deps.js" ;
3
+ import { buildOptionalDepRule } from "./optional-deps.js" ;
4
4
import { patchCode } from "./util.js" ;
5
5
6
6
describe ( "optional dependecy" , ( ) => {
7
7
it ( 'should wrap a top-level require("caniuse-lite") in a try-catch' , ( ) => {
8
8
const code = `t = require("caniuse-lite");` ;
9
- expect ( patchCode ( code , optionalDepRule ) ) . toMatchInlineSnapshot ( `
9
+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot ( `
10
10
"try {
11
11
t = require("caniuse-lite");
12
12
} catch {
@@ -17,7 +17,7 @@ describe("optional dependecy", () => {
17
17
18
18
it ( 'should wrap a top-level require("caniuse-lite/data") in a try-catch' , ( ) => {
19
19
const code = `t = require("caniuse-lite/data");` ;
20
- expect ( patchCode ( code , optionalDepRule ) ) . toMatchInlineSnapshot (
20
+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot (
21
21
`
22
22
"try {
23
23
t = require("caniuse-lite/data");
@@ -30,7 +30,7 @@ describe("optional dependecy", () => {
30
30
31
31
it ( 'should wrap e.exports = require("caniuse-lite") in a try-catch' , ( ) => {
32
32
const code = 'e.exports = require("caniuse-lite");' ;
33
- expect ( patchCode ( code , optionalDepRule ) ) . toMatchInlineSnapshot ( `
33
+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot ( `
34
34
"try {
35
35
e.exports = require("caniuse-lite");
36
36
} catch {
@@ -41,7 +41,7 @@ describe("optional dependecy", () => {
41
41
42
42
it ( 'should wrap module.exports = require("caniuse-lite") in a try-catch' , ( ) => {
43
43
const code = 'module.exports = require("caniuse-lite");' ;
44
- expect ( patchCode ( code , optionalDepRule ) ) . toMatchInlineSnapshot ( `
44
+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot ( `
45
45
"try {
46
46
module.exports = require("caniuse-lite");
47
47
} catch {
@@ -52,7 +52,7 @@ describe("optional dependecy", () => {
52
52
53
53
it ( 'should wrap exports.foo = require("caniuse-lite") in a try-catch' , ( ) => {
54
54
const code = 'exports.foo = require("caniuse-lite");' ;
55
- expect ( patchCode ( code , optionalDepRule ) ) . toMatchInlineSnapshot ( `
55
+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot ( `
56
56
"try {
57
57
exports.foo = require("caniuse-lite");
58
58
} catch {
@@ -63,23 +63,27 @@ describe("optional dependecy", () => {
63
63
64
64
it ( 'should not wrap require("lodash") in a try-catch' , ( ) => {
65
65
const code = 't = require("lodash");' ;
66
- expect ( patchCode ( code , optionalDepRule ) ) . toMatchInlineSnapshot ( `"t = require("lodash");"` ) ;
66
+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot (
67
+ `"t = require("lodash");"`
68
+ ) ;
67
69
} ) ;
68
70
69
71
it ( 'should not wrap require("other-module") if it does not match caniuse-lite regex' , ( ) => {
70
72
const code = 't = require("other-module");' ;
71
- expect ( patchCode ( code , optionalDepRule ) ) . toMatchInlineSnapshot ( `"t = require("other-module");"` ) ;
73
+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot (
74
+ `"t = require("other-module");"`
75
+ ) ;
72
76
} ) ;
73
77
74
78
it ( "should not wrap a require() call already inside a try-catch" , ( ) => {
75
79
const code = `
76
80
try {
77
- const t = require("caniuse-lite");
81
+ t = require("caniuse-lite");
78
82
} catch {}
79
83
` ;
80
- expect ( patchCode ( code , optionalDepRule ) ) . toMatchInlineSnapshot ( `
84
+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot ( `
81
85
"try {
82
- const t = require("caniuse-lite");
86
+ t = require("caniuse-lite");
83
87
} catch {}
84
88
"
85
89
` ) ;
@@ -88,14 +92,62 @@ try {
88
92
it ( "should handle require with subpath and not wrap if already in try-catch" , ( ) => {
89
93
const code = `
90
94
try {
91
- const t = require("caniuse-lite/path");
95
+ t = require("caniuse-lite/path");
92
96
} catch {}
93
97
` ;
94
- expect ( patchCode ( code , optionalDepRule ) ) . toMatchInlineSnapshot ( `
98
+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot ( `
95
99
"try {
96
- const t = require("caniuse-lite/path");
100
+ t = require("caniuse-lite/path");
97
101
} catch {}
98
102
"
99
103
` ) ;
100
104
} ) ;
105
+
106
+ it ( "should handle multiple dependencies" , ( ) => {
107
+ const code = `
108
+ t1 = require("caniuse-lite");
109
+ t2 = require("caniuse-lite/path");
110
+ t3 = require("jimp");
111
+ t4 = require("jimp/path");
112
+ ` ;
113
+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" , "jimp" ] ) ) ) . toMatchInlineSnapshot ( `
114
+ "try {
115
+ t1 = require("caniuse-lite");
116
+ } catch {
117
+ throw new Error('The optional dependency "caniuse-lite" is not installed');
118
+ };
119
+ try {
120
+ t2 = require("caniuse-lite/path");
121
+ } catch {
122
+ throw new Error('The optional dependency "caniuse-lite/path" is not installed');
123
+ };
124
+ try {
125
+ t3 = require("jimp");
126
+ } catch {
127
+ throw new Error('The optional dependency "jimp" is not installed');
128
+ };
129
+ try {
130
+ t4 = require("jimp/path");
131
+ } catch {
132
+ throw new Error('The optional dependency "jimp/path" is not installed');
133
+ };
134
+ "
135
+ ` ) ;
136
+ } ) ;
137
+
138
+ it ( "should not update partial matches" , ( ) => {
139
+ const code = `
140
+ t1 = require("before-caniuse-lite");
141
+ t2 = require("before-caniuse-lite/path");
142
+ t3 = require("caniuse-lite-after");
143
+ t4 = require("caniuse-lite-after/path");
144
+ ` ;
145
+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot ( `
146
+ "t1 = require("before-caniuse-lite");
147
+ t2 = require("before-caniuse-lite/path");
148
+ t3 = require("caniuse-lite-after");
149
+ t4 = require("caniuse-lite-after/path");
150
+ "
151
+ ` ) ;
152
+ } ) ;
101
153
} ) ;
0 commit comments