11import { describe , expect , it } from "vitest" ;
22
3- import { optionalDepRule } from "./optional-deps.js" ;
3+ import { buildOptionalDepRule } from "./optional-deps.js" ;
44import { patchCode } from "./util.js" ;
55
66describe ( "optional dependecy" , ( ) => {
77 it ( 'should wrap a top-level require("caniuse-lite") in a try-catch' , ( ) => {
88 const code = `t = require("caniuse-lite");` ;
9- expect ( patchCode ( code , optionalDepRule ) ) . toMatchInlineSnapshot ( `
9+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot ( `
1010 "try {
1111 t = require("caniuse-lite");
1212 } catch {
@@ -17,7 +17,7 @@ describe("optional dependecy", () => {
1717
1818 it ( 'should wrap a top-level require("caniuse-lite/data") in a try-catch' , ( ) => {
1919 const code = `t = require("caniuse-lite/data");` ;
20- expect ( patchCode ( code , optionalDepRule ) ) . toMatchInlineSnapshot (
20+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot (
2121 `
2222 "try {
2323 t = require("caniuse-lite/data");
@@ -30,7 +30,7 @@ describe("optional dependecy", () => {
3030
3131 it ( 'should wrap e.exports = require("caniuse-lite") in a try-catch' , ( ) => {
3232 const code = 'e.exports = require("caniuse-lite");' ;
33- expect ( patchCode ( code , optionalDepRule ) ) . toMatchInlineSnapshot ( `
33+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot ( `
3434 "try {
3535 e.exports = require("caniuse-lite");
3636 } catch {
@@ -41,7 +41,7 @@ describe("optional dependecy", () => {
4141
4242 it ( 'should wrap module.exports = require("caniuse-lite") in a try-catch' , ( ) => {
4343 const code = 'module.exports = require("caniuse-lite");' ;
44- expect ( patchCode ( code , optionalDepRule ) ) . toMatchInlineSnapshot ( `
44+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot ( `
4545 "try {
4646 module.exports = require("caniuse-lite");
4747 } catch {
@@ -52,7 +52,7 @@ describe("optional dependecy", () => {
5252
5353 it ( 'should wrap exports.foo = require("caniuse-lite") in a try-catch' , ( ) => {
5454 const code = 'exports.foo = require("caniuse-lite");' ;
55- expect ( patchCode ( code , optionalDepRule ) ) . toMatchInlineSnapshot ( `
55+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot ( `
5656 "try {
5757 exports.foo = require("caniuse-lite");
5858 } catch {
@@ -63,23 +63,27 @@ describe("optional dependecy", () => {
6363
6464 it ( 'should not wrap require("lodash") in a try-catch' , ( ) => {
6565 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+ ) ;
6769 } ) ;
6870
6971 it ( 'should not wrap require("other-module") if it does not match caniuse-lite regex' , ( ) => {
7072 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+ ) ;
7276 } ) ;
7377
7478 it ( "should not wrap a require() call already inside a try-catch" , ( ) => {
7579 const code = `
7680try {
77- const t = require("caniuse-lite");
81+ t = require("caniuse-lite");
7882} catch {}
7983` ;
80- expect ( patchCode ( code , optionalDepRule ) ) . toMatchInlineSnapshot ( `
84+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot ( `
8185 "try {
82- const t = require("caniuse-lite");
86+ t = require("caniuse-lite");
8387 } catch {}
8488 "
8589 ` ) ;
@@ -88,14 +92,62 @@ try {
8892 it ( "should handle require with subpath and not wrap if already in try-catch" , ( ) => {
8993 const code = `
9094try {
91- const t = require("caniuse-lite/path");
95+ t = require("caniuse-lite/path");
9296} catch {}
9397` ;
94- expect ( patchCode ( code , optionalDepRule ) ) . toMatchInlineSnapshot ( `
98+ expect ( patchCode ( code , buildOptionalDepRule ( [ "caniuse-lite" ] ) ) ) . toMatchInlineSnapshot ( `
9599 "try {
96- const t = require("caniuse-lite/path");
100+ t = require("caniuse-lite/path");
97101 } catch {}
98102 "
99103 ` ) ;
100104 } ) ;
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+ } ) ;
101153} ) ;
0 commit comments