-
-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolve segfault issue caused by "node:events" import (#1885)
Co-authored-by: Artem Zakharchenko <[email protected]>
- Loading branch information
1 parent
e078e91
commit f188d77
Showing
12 changed files
with
144 additions
and
76 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
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,22 @@ | ||
/** | ||
* @see https://github.com/mswjs/msw/pull/1858 | ||
* @see https://github.com/mswjs/msw/issues/1868 | ||
*/ | ||
import { setupServer } from 'msw/native' | ||
|
||
test('calls "setupServer" without errors in React Native', async () => { | ||
/** | ||
* @note Asserting that mocking works is not possible with | ||
* the current testing setup. We force Vitest to alias "msw" | ||
* imports to their ESM build, which is a hack. | ||
* | ||
* We need Vitest to load the ESM build here in order to | ||
* use "vi.mock()" to mock the "node:events" imports to throw. | ||
* Vitest doesn't support module mocking in CommonJS. | ||
* | ||
* But aliasing the build isn't enough for it to function. | ||
* The root-level package.json is still CJS, which, I suspects, | ||
* resolves any subsequent in-build imports to their CJS counterparts. | ||
*/ | ||
expect(() => setupServer()).not.toThrow() | ||
}) |
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,22 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"target": "esnext", | ||
"module": "esnext", | ||
"baseUrl": ".", | ||
"paths": { | ||
"msw": ["../.."], | ||
"msw/*": ["../../*"] | ||
}, | ||
"noEmit": true, | ||
"declaration": false, | ||
"resolveJsonModule": true, | ||
// Support default imports for modules that have no default exports. | ||
// This way "http" imports stay "import http from 'http'". | ||
// Using wildcard there breaks request interception since it | ||
// encapsulates the values under the ".default" key. | ||
"allowSyntheticDefaultImports": true, | ||
"types": ["node", "vitest/globals"] | ||
}, | ||
"include": ["../../global.d.ts", "./**/*.test.ts"] | ||
} |
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,28 @@ | ||
import * as path from 'node:path' | ||
import { defineConfig } from 'vitest/config' | ||
|
||
const LIB_DIR = path.resolve(__dirname, '../../lib') | ||
|
||
export default defineConfig({ | ||
test: { | ||
root: './test/native', | ||
include: ['**/*.native.test.ts'], | ||
globals: true, | ||
setupFiles: ['./vitest.setup.ts'], | ||
alias: { | ||
/** | ||
* @note Force Vitest load ESM targets of MSW. | ||
* If we run ESM in tests, we can use "vi.mock()" to | ||
* emulate certain standard Node.js modules missing | ||
* (like "node:events") in React Native. | ||
* | ||
* Vitest won't pick up the ESM targets because | ||
* the root-level "package.json" is not "module". | ||
*/ | ||
'msw/node': path.resolve(LIB_DIR, 'node/index.mjs'), | ||
'msw/native': path.resolve(LIB_DIR, 'native/index.mjs'), | ||
'msw/browser': path.resolve(LIB_DIR, 'browser/index.mjs'), | ||
msw: path.resolve(LIB_DIR, 'core/index.mjs'), | ||
}, | ||
}, | ||
}) |
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,15 @@ | ||
import { vi } from 'vitest' | ||
|
||
/** | ||
* @note The list of standard Node.js modules missing | ||
* in the React Native runtime. | ||
*/ | ||
const reactNativeMissingModules = ['events', 'node:events'] | ||
|
||
reactNativeMissingModules.forEach((moduleName) => { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
vi.doMock(moduleName, () => { | ||
throw new Error( | ||
`Failed to import module "${moduleName}": it does not exist in React Native. This likely means MSW tries to import something too optimistically in that environment.`, | ||
) | ||
}) | ||
}) |
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
🚀