Skip to content

Commit 8caa9a1

Browse files
Refactor Jest configuration and enhance testing utilities
- Removed Node version checks and related module name mappings from jest.config.js to simplify configuration. - Cleaned up jest.setup.js by removing the mock for fetch that converted Node's Readable stream to a web-standard ReadableStream. - Introduced new utility functions in testUtils.js for creating Node.js Readable streams and retrieving the Node version. - Added new tests for fetch functionality that validate streaming behavior with Readable streams. - Created emptyForTesting.js to facilitate testing in environments where certain modules need to be mocked. These changes improve the clarity and maintainability of the testing setup in the react-on-rails and react-on-rails-pro packages.
1 parent b1e6462 commit 8caa9a1

File tree

7 files changed

+0
-37
lines changed

7 files changed

+0
-37
lines changed
Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// eslint-disable-next-line import/no-relative-packages
22
import rootConfig from '../../jest.config.base.js';
33

4-
const nodeVersion = parseInt(process.version.slice(1), 10);
5-
64
// Package-specific Jest configuration
75
// Inherits from root jest.config.mjs and adds package-specific settings
86
export default {
@@ -15,17 +13,6 @@ export default {
1513
// Package-specific: Jest setup files
1614
setupFiles: ['<rootDir>/tests/jest.setup.js'],
1715

18-
// Package-specific: Module name mapping for React Server Components
19-
// Only mock modules on Node versions < 18 where RSC features aren't available
20-
moduleNameMapper:
21-
nodeVersion < 18
22-
? {
23-
'react-on-rails-rsc/client': '<rootDir>/tests/emptyForTesting.js',
24-
'^@testing-library/dom$': '<rootDir>/tests/emptyForTesting.js',
25-
'^@testing-library/react$': '<rootDir>/tests/emptyForTesting.js',
26-
}
27-
: {},
28-
2916
// Set root directory to current package
3017
rootDir: '.',
3118
};

packages/react-on-rails/tests/jest.setup.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,8 @@ if (typeof window !== 'undefined') {
1414
// eslint-disable-next-line global-require
1515
const { TextEncoder, TextDecoder } = require('util');
1616
// eslint-disable-next-line global-require
17-
const { Readable } = require('stream');
18-
// eslint-disable-next-line global-require
1917
const { ReadableStream, ReadableStreamDefaultReader } = require('stream/web');
2018

21-
// Mock the fetch function to return a ReadableStream instead of Node's Readable stream
22-
// This matches browser behavior where fetch responses have ReadableStream bodies
23-
// Node's fetch and polyfills like jest-fetch-mock return Node's Readable stream,
24-
// so we convert it to a web-standard ReadableStream for consistency
25-
// Note: Node's Readable stream exists in node 'stream' built-in module, can be imported as `import { Readable } from 'stream'`
26-
jest.mock('../src/utils', () => ({
27-
...jest.requireActual('../src/utils'),
28-
fetch: (...args) =>
29-
jest
30-
.requireActual('../src/utils')
31-
.fetch(...args)
32-
.then((res) => {
33-
const originalBody = res.body;
34-
if (originalBody instanceof Readable) {
35-
Object.defineProperty(res, 'body', {
36-
value: Readable.toWeb(originalBody),
37-
});
38-
}
39-
return res;
40-
}),
41-
}));
42-
4319
global.TextEncoder = TextEncoder;
4420
global.TextDecoder = TextDecoder;
4521

0 commit comments

Comments
 (0)