Skip to content

Commit a918960

Browse files
alexeyr-ci2alexeyr
andauthored
Make RSC tests await act (#1734)
* Remove act from render * Add eslint-plugin-testing-library * Remove more packages in convert script --------- Co-authored-by: Alexey Romanov <alexey.v.romanov@gmail.com>
1 parent 2cd3a29 commit a918960

File tree

6 files changed

+130
-102
lines changed

6 files changed

+130
-102
lines changed

eslint.config.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'node:path';
22
import { globalIgnores } from 'eslint/config';
33
import jest from 'eslint-plugin-jest';
44
import prettierRecommended from 'eslint-plugin-prettier/recommended';
5+
import testingLibrary from 'eslint-plugin-testing-library';
56
import globals from 'globals';
67
import tsEslint from 'typescript-eslint';
78
import { includeIgnoreFile } from '@eslint/compat';
@@ -198,11 +199,17 @@ const config = tsEslint.config([
198199
{
199200
files: ['node_package/tests/**', '**/*.test.{js,jsx,ts,tsx}'],
200201

201-
extends: [jest.configs['flat/recommended'], jest.configs['flat/style']],
202+
extends: [
203+
jest.configs['flat/recommended'],
204+
jest.configs['flat/style'],
205+
testingLibrary.configs['flat/dom'],
206+
],
202207

203208
rules: {
204209
// Allows Jest mocks before import
205210
'import/first': 'off',
211+
// Avoiding these methods complicates tests and isn't useful for our purposes
212+
'testing-library/no-node-access': 'off',
206213
},
207214
},
208215
// must be the last config in the array

node_package/tests/SuspenseHydration.test.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,15 @@ async function renderAndHydrate() {
167167
hydrate();
168168
await writeFirstChunk();
169169
});
170-
await waitFor(() => {
171-
expect(screen.queryByText('Loading...')).toBeInTheDocument();
172-
});
170+
expect(await screen.findByText('Loading...')).toBeInTheDocument();
173171
expect(onContainerHydrated).toHaveBeenCalled();
174172

175173
// The async component is not hydrated until the second chunk is written to the document
176174
await new Promise((resolve) => {
177175
setTimeout(resolve, 1000);
178176
});
179177
expect(onAsyncComponentHydrated).not.toHaveBeenCalled();
180-
expect(screen.queryByText('Loading...')).toBeInTheDocument();
178+
expect(screen.getByText('Loading...')).toBeInTheDocument();
181179
expect(screen.queryByText('Hello World')).not.toBeInTheDocument();
182180
});
183181

@@ -189,18 +187,16 @@ async function renderAndHydrate() {
189187
hydrate();
190188
await writeFirstChunk();
191189
});
192-
await waitFor(() => {
193-
expect(screen.queryByText('Loading...')).toBeInTheDocument();
194-
});
190+
expect(await screen.findByText('Loading...')).toBeInTheDocument();
195191

196192
await act(async () => {
197193
const secondChunk = await writeSecondChunk();
198194
expect(secondChunk).toContain('script');
199195
});
200196
await waitFor(() => {
201197
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
202-
expect(screen.queryByText('Hello World')).toBeInTheDocument();
203198
});
199+
expect(screen.getByText('Hello World')).toBeInTheDocument();
204200

205201
expect(onContainerHydrated).toHaveBeenCalled();
206202
expect(onAsyncComponentHydrated).toHaveBeenCalled();

node_package/tests/registerServerComponent.client.test.jsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { clear as clearComponentRegistry } from '../src/ComponentRegistry.ts';
1717
enableFetchMocks();
1818

1919
// React Server Components tests require React 19 and only run with Node version 18 (`newest` in our CI matrix)
20-
(getNodeVersion() >= 18 ? describe : describe.skip)('RSCClientRoot', () => {
20+
(getNodeVersion() >= 18 ? describe : describe.skip)('registerServerComponent', () => {
2121
let container;
2222
const mockDomNodeId = 'test-container';
2323

@@ -70,11 +70,10 @@ enableFetchMocks();
7070
};
7171

7272
// Execute the render
73-
const render = () =>
74-
act(async () => {
75-
const Component = ReactOnRails.getComponent('TestComponent');
76-
await Component.component({}, railsContext, mockDomNodeId);
77-
});
73+
const render = async () => {
74+
const Component = ReactOnRails.getComponent('TestComponent');
75+
await Component.component({}, railsContext, mockDomNodeId);
76+
};
7877

7978
return {
8079
render,
@@ -92,7 +91,7 @@ enableFetchMocks();
9291

9392
await act(async () => {
9493
pushFirstChunk();
95-
render();
94+
await render();
9695
});
9796
expect(window.fetch).toHaveBeenCalledWith('/rsc-render/TestComponent?props=%7B%7D');
9897
expect(window.fetch).toHaveBeenCalledTimes(1);
@@ -113,7 +112,7 @@ enableFetchMocks();
113112
const { render, pushFirstChunk, pushSecondChunk, endStream } = await mockRSCRequest();
114113

115114
await act(async () => {
116-
render();
115+
await render();
117116
pushFirstChunk();
118117
});
119118
expect(consoleSpy).toHaveBeenCalledWith(
@@ -143,7 +142,7 @@ enableFetchMocks();
143142
const { render, pushFirstChunk, pushSecondChunk, endStream } = await mockRSCRequest('/rsc-render/');
144143

145144
await act(async () => {
146-
render();
145+
await render();
147146
pushFirstChunk();
148147
pushSecondChunk();
149148
endStream();

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"eslint-plugin-prettier": "^5.2.3",
5656
"eslint-plugin-react": "^7.37.4",
5757
"eslint-plugin-react-hooks": "^5.2.0",
58+
"eslint-plugin-testing-library": "^7.5.3",
5859
"globals": "^16.0.0",
5960
"jest": "^29.7.0",
6061
"jest-environment-jsdom": "^29.7.0",
@@ -71,7 +72,7 @@
7172
"redux": "^4.2.1",
7273
"ts-jest": "^29.2.5",
7374
"typescript": "^5.8.3",
74-
"typescript-eslint": "^8.29.1"
75+
"typescript-eslint": "^8.35.0"
7576
},
7677
"peerDependencies": {
7778
"react": ">= 16",

script/convert

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,12 @@ gsub_file_content("../Gemfile.development_dependencies", /gem "shakapacker", "[^
2121
gsub_file_content("../spec/dummy/package.json", /"shakapacker": "[^"]*",/, '"shakapacker": "6.6.0",')
2222

2323
# The below packages don't work on the oldest supported Node version and aren't needed there anyway
24-
gsub_file_content("../package.json", /"eslint": "[^"]*",/, "")
25-
gsub_file_content("../package.json", /"eslint-plugin-jest": "[^"]*",/, "")
24+
gsub_file_content("../package.json", /"[^"]*eslint[^"]*": "[^"]*",?/, "")
2625
gsub_file_content("../package.json", /"globals": "[^"]*",/, "")
2726
gsub_file_content("../package.json", /"knip": "[^"]*",/, "")
2827
gsub_file_content("../package.json", /"publint": "[^"]*",/, "")
29-
gsub_file_content("../package.json", /"typescript-eslint": "[^"]*",?/, "")
3028
gsub_file_content("../package.json", %r{"@arethetypeswrong/cli": "[^"]*",}, "")
31-
gsub_file_content("../package.json", %r{"@eslint/compat": "[^"]*",}, "")
32-
gsub_file_content("../package.json", %r{"@testing-library/dom": "[^"]*",}, "")
33-
gsub_file_content("../package.json", %r{"@testing-library/react": "[^"]*",}, "")
29+
gsub_file_content("../package.json", %r{"@testing-library/[^"]*": "[^"]*",}, "")
3430

3531
# Clean up any trailing commas before closing braces
3632
gsub_file_content("../package.json", /,(\s*})/, "\\1")

0 commit comments

Comments
 (0)