Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions __tests__/esm/import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ export default code
expect(res.cwd).toBe(process.cwd())
})

it('should work with provided globals defined by accessors in the current global', async () => {
const OriginalBuffer = Buffer
Object.defineProperty(global, 'Buffer', {
get: () => OriginalBuffer,
set: () => {
// ok
}
})
try {
const res = await importFromString('export const bufferType = typeof Buffer;', {
globals: { Buffer }
})
expect(res.bufferType).toBe('function')
} finally {
Object.defineProperty(global, 'Buffer', {
value: OriginalBuffer
})
}
})

it('should have access the global object', async () => {
const res = await importFromString('export const { greet } = global', {
globals: { greet: 'hi' }
Expand Down
7 changes: 6 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,13 @@ export const createGlobalObject = (globals: Context, useCurrentGlobal: boolean):
})
forEachPropertyKey(globals, propertyKey => {
if (propertyKey in __GLOBAL__) {
const descriptor = Object.getOwnPropertyDescriptor(__GLOBAL__, propertyKey)
if (descriptor !== undefined) {
delete descriptor.get
delete descriptor.set
}
Object.defineProperty(globalObject, propertyKey, {
...Object.getOwnPropertyDescriptor(__GLOBAL__, propertyKey),
...descriptor,
value: globals[propertyKey as keyof Context]
})
} else {
Expand Down