-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: skip interceptors for non-configurable globals (#665)
- Loading branch information
1 parent
7e62e17
commit 2ec79d7
Showing
6 changed files
with
102 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { vi, beforeAll, afterEach, afterAll, it, expect } from 'vitest' | ||
import { hasConfigurableGlobal } from './hasConfigurableGlobal' | ||
|
||
beforeAll(() => { | ||
vi.spyOn(console, 'error').mockImplementation(() => {}) | ||
}) | ||
|
||
afterEach(() => { | ||
vi.resetAllMocks() | ||
}) | ||
|
||
afterAll(() => { | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
it('returns true if the global property exists and is configurable', () => { | ||
Object.defineProperty(global, '_existsAndConfigurable', { | ||
value: 'something', | ||
configurable: true, | ||
}) | ||
|
||
expect(hasConfigurableGlobal('_existsAndConfigurable')).toBe(true) | ||
}) | ||
|
||
it('returns false if the global property does not exist', () => { | ||
expect(hasConfigurableGlobal('_non-existing')).toBe(false) | ||
}) | ||
|
||
it('returns false and prints an error for implicitly non-configurable global property', () => { | ||
Object.defineProperty(global, '_implicitlyNonConfigurable', { | ||
value: 'something', | ||
}) | ||
|
||
expect(hasConfigurableGlobal('_implicitlyNonConfigurable')).toBe(false) | ||
expect(console.error).toHaveBeenCalledWith( | ||
'[MSW] Failed to apply interceptor: the global `_implicitlyNonConfigurable` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.' | ||
) | ||
}) | ||
|
||
it('returns false and prints an error for explicitly non-configurable global property', () => { | ||
Object.defineProperty(global, '_explicitlyNonConfigurable', { | ||
value: 'something', | ||
configurable: false, | ||
}) | ||
|
||
expect(hasConfigurableGlobal('_explicitlyNonConfigurable')).toBe(false) | ||
expect(console.error).toHaveBeenCalledWith( | ||
'[MSW] Failed to apply interceptor: the global `_explicitlyNonConfigurable` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.' | ||
) | ||
}) | ||
|
||
it('returns false and prints an error for global property that only has a getter', () => { | ||
Object.defineProperty(global, '_onlyGetter', { get: () => 'something' }) | ||
|
||
expect(hasConfigurableGlobal('_onlyGetter')).toBe(false) | ||
expect(console.error).toHaveBeenCalledWith( | ||
'[MSW] Failed to apply interceptor: the global `_onlyGetter` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.' | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Returns a boolean indicating whether the given global property | ||
* is defined and is configurable. | ||
*/ | ||
export function hasConfigurableGlobal(propertyName: string): boolean { | ||
const descriptor = Object.getOwnPropertyDescriptor(globalThis, propertyName) | ||
|
||
if (typeof descriptor === 'undefined') { | ||
return false | ||
} | ||
|
||
if (typeof descriptor.set === 'undefined' && !descriptor.configurable) { | ||
console.error( | ||
`[MSW] Failed to apply interceptor: the global \`${propertyName}\` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.` | ||
) | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,7 @@ export default defineConfig({ | |
test: { | ||
include: ['./src/**/*.test.ts'], | ||
}, | ||
esbuild: { | ||
target: 'es2022', | ||
}, | ||
}) |