11import path from 'path'
2- import { importFromString , importFromStringSync } from '../../src/index'
2+ import {
3+ importFromString ,
4+ createImportFromString ,
5+ importFromStringSync ,
6+ createImportFromStringSync
7+ } from '../../src/index'
38
49let importFromStringFn : typeof importFromString | typeof importFromStringSync
10+ let createImportFromStringFn : typeof createImportFromString | typeof createImportFromStringSync
511
612const testImport = ( ) : void => {
713 it ( 'should work with named export' , async ( ) => {
@@ -64,8 +70,8 @@ export default code
6470 const res = ( ) : Promise < string > => importFromStringFn ( 'export default process.cwd()' )
6571 try {
6672 await res ( )
67- } catch ( err ) {
68- expect ( Object . getPrototypeOf ( err ) ) . toHaveProperty ( 'name' , 'ReferenceError' )
73+ } catch ( error ) {
74+ expect ( Object . getPrototypeOf ( error ) ) . toHaveProperty ( 'name' , 'ReferenceError' )
6975 }
7076 } )
7177
@@ -84,15 +90,24 @@ export default code
8490 } )
8591
8692 it ( 'should have same globalThis' , async ( ) => {
87- const res = await importFromStringFn ( 'export default global === globalThis' )
88- expect ( res . default ) . toBeTruthy ( )
93+ const code = 'export default global.globalThis === globalThis.global'
94+ expect ( ( await importFromStringFn ( code ) ) . default ) . toBeTruthy ( )
95+ expect ( ( await importFromStringFn ( code , { useCurrentGlobal : true } ) ) . default ) . toBeTruthy ( )
8996 } )
9097
9198 it ( 'should work with current global' , async ( ) => {
92- const res = await importFromStringFn ( 'export default new Error()' , {
99+ const importFromStringFn = createImportFromStringFn ( { useCurrentGlobal : true } )
100+ expect ( ( await importFromStringFn ( 'export default new Error()' ) ) . default ) . toBeInstanceOf ( Error )
101+ expect ( ( await importFromStringFn ( 'export default new global.Error()' ) ) . default ) . toBeInstanceOf ( Error ) // prettier-ignore
102+ } )
103+
104+ it ( 'should be able to override current global' , async ( ) => {
105+ const importFromStringFn = createImportFromStringFn ( {
106+ globals : { Error : Array } ,
93107 useCurrentGlobal : true
94108 } )
95- expect ( res . default ) . toBeInstanceOf ( Error )
109+ expect ( ( await importFromStringFn ( 'export default new Error()' ) ) . default ) . toBeInstanceOf ( Array )
110+ expect ( ( await importFromStringFn ( 'export default new global.Error()' ) ) . default ) . toBeInstanceOf ( Array ) // prettier-ignore
96111 } )
97112
98113 it ( 'should work if transformOption is provided' , async ( ) => {
@@ -119,10 +134,12 @@ export default code
119134
120135describe ( 'importFromString' , ( ) => {
121136 importFromStringFn = importFromString
137+ createImportFromStringFn = createImportFromString
122138 testImport ( )
123139} )
124140
125141describe ( 'importFromStringSync' , ( ) => {
126142 importFromStringFn = importFromStringSync
143+ createImportFromStringFn = createImportFromStringSync
127144 testImport ( )
128145} )
0 commit comments