Skip to content

Commit

Permalink
Test fetch function only with node version 18+
Browse files Browse the repository at this point in the history
  • Loading branch information
AbanoubGhadban committed Feb 20, 2025
1 parent dc0c0c3 commit dcc32be
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
6 changes: 2 additions & 4 deletions node_package/tests/RSCClientRoot.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ import { render, screen, act } from '@testing-library/react';
import '@testing-library/jest-dom';
import path from 'path';
import fs from 'fs';
import { createNodeReadableStream } from './testUtils';
import { createNodeReadableStream, getNodeVersion } from './testUtils';

import RSCClientRoot, { resetRenderCache } from '../src/RSCClientRoot';

enableFetchMocks();

const nodeVersion = parseInt(process.version.slice(1), 10);

// React Server Components tests are not compatible with Experimental React 18 and React 19
// That only run with node version 18 and above
(nodeVersion >= 18 ? describe : describe.skip)('RSCClientRoot', () => {
(getNodeVersion() >= 18 ? describe : describe.skip)('RSCClientRoot', () => {
beforeEach(() => {
jest.clearAllMocks();

Expand Down
3 changes: 2 additions & 1 deletion node_package/tests/testUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Readable } from 'stream';
* push: (chunk: any) => void
* }} Object containing the stream and push function
*/
// eslint-disable-next-line import/prefer-default-export
export const createNodeReadableStream = () => {
const pendingChunks = [];
let pushFn;
Expand All @@ -31,3 +30,5 @@ export const createNodeReadableStream = () => {

return { stream, push };
};

export const getNodeVersion = () => parseInt(process.version.slice(1), 10);
7 changes: 5 additions & 2 deletions node_package/tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { enableFetchMocks } from 'jest-fetch-mock';

import { fetch } from '../src/utils';
import { createNodeReadableStream } from './testUtils';
import { createNodeReadableStream, getNodeVersion } from './testUtils';

enableFetchMocks();

describe('fetch', () => {
// The fetch mock functionality that returns a ReadableStream is not supported in Node.js v16.
// Additionally, fetch function is used in RSCClientRoot only that is compatible with Node.js v18+,
// so these tests are conditionally skipped on older Node versions.
(getNodeVersion() >= 18 ? describe : describe.skip)('fetch', () => {
it('streams body as ReadableStream', async () => {
// create Readable stream that emits 5 chunks with 10ms delay between each chunk
const { stream, push } = createNodeReadableStream();
Expand Down

0 comments on commit dcc32be

Please sign in to comment.