Skip to content

Commit dc30163

Browse files
committed
test: add more test cases for the global object
1 parent 023a67e commit dc30163

File tree

3 files changed

+54
-19
lines changed

3 files changed

+54
-19
lines changed

__tests__/cjs/import.test.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import 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

49
let importFromStringFn: typeof importFromString | typeof importFromStringSync
10+
let createImportFromStringFn: typeof createImportFromString | typeof createImportFromStringSync
511

612
const 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

120135
describe('importFromString', () => {
121136
importFromStringFn = importFromString
137+
createImportFromStringFn = createImportFromString
122138
testImport()
123139
})
124140

125141
describe('importFromStringSync', () => {
126142
importFromStringFn = importFromStringSync
143+
createImportFromStringFn = createImportFromStringSync
127144
testImport()
128145
})

__tests__/cjs/require.test.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path'
2-
import { requireFromString } from '../../src/index'
2+
import { requireFromString, createRequireFromString } from '../../src/index'
33

44
it('should work with `module.exports`', () => {
55
const res = requireFromString("module.exports = 'hi'")
@@ -67,13 +67,22 @@ it('should have access the global object', () => {
6767
})
6868

6969
it('should have same globalThis', () => {
70-
const res = requireFromString('module.exports = global === globalThis')
71-
expect(res).toBeTruthy()
70+
const code = 'module.exports = global.globalThis === globalThis.global'
71+
expect(requireFromString(code)).toBeTruthy()
72+
expect(requireFromString(code, { useCurrentGlobal: true })).toBeTruthy()
7273
})
7374

7475
it('should work with current global', () => {
75-
const res = requireFromString('module.exports = new Error()', {
76+
const requireFromStringFn = createRequireFromString({ useCurrentGlobal: true })
77+
expect(requireFromStringFn('module.exports = new Error()')).toBeInstanceOf(Error)
78+
expect(requireFromStringFn('module.exports = new global.Error()')).toBeInstanceOf(Error)
79+
})
80+
81+
it('should be able to override current global', () => {
82+
const requireFromStringFn = createRequireFromString({
83+
globals: { Error: Array },
7684
useCurrentGlobal: true
7785
})
78-
expect(res).toBeInstanceOf(Error)
86+
expect(requireFromStringFn('module.exports = new Error()')).toBeInstanceOf(Array)
87+
expect(requireFromStringFn('module.exports = new global.Error()')).toBeInstanceOf(Array)
7988
})

__tests__/esm/import.test.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path'
22
import { fileURLToPath, pathToFileURL } from 'url'
3-
import { importFromString, importFromStringSync } from '../../src/index'
3+
import { importFromString, createImportFromString, importFromStringSync } from '../../src/index'
44

55
const __dirname = path.dirname(fileURLToPath(import.meta.url))
66

@@ -88,8 +88,8 @@ export default code
8888
const res = (): Promise<string> => importFromString('export default process.cwd()')
8989
try {
9090
await res()
91-
} catch (err) {
92-
expect(Object.getPrototypeOf(err)).toHaveProperty('name', 'ReferenceError')
91+
} catch (error) {
92+
expect(Object.getPrototypeOf(error)).toHaveProperty('name', 'ReferenceError')
9393
}
9494
})
9595

@@ -108,15 +108,24 @@ export default code
108108
})
109109

110110
it('should have same globalThis', async () => {
111-
const res = await importFromString('export default global === globalThis')
112-
expect(res.default).toBeTruthy()
111+
const code = 'export default global.globalThis === globalThis.global'
112+
expect((await importFromString(code)).default).toBeTruthy()
113+
expect((await importFromString(code, { useCurrentGlobal: true })).default).toBeTruthy()
113114
})
114115

115116
it('should work with current global', async () => {
116-
const res = await importFromString('export default new Error()', {
117+
const importFromStringFn = createImportFromString({ useCurrentGlobal: true })
118+
expect((await importFromStringFn('export default new Error()')).default).toBeInstanceOf(Error)
119+
expect((await importFromStringFn('export default new global.Error()')).default).toBeInstanceOf(Error) // prettier-ignore
120+
})
121+
122+
it('should be able to override current global', async () => {
123+
const importFromStringFn = createImportFromString({
124+
globals: { Error: Array },
117125
useCurrentGlobal: true
118126
})
119-
expect(res.default).toBeInstanceOf(Error)
127+
expect((await importFromStringFn('export default new Error()')).default).toBeInstanceOf(Array)
128+
expect((await importFromStringFn('export default new global.Error()')).default).toBeInstanceOf(Array) // prettier-ignore
120129
})
121130

122131
it('should work if transformOption is provided', async () => {

0 commit comments

Comments
 (0)