Skip to content

Commit a8a8a7f

Browse files
justin808claude
andcommitted
Fix Jest tests for React 19 compatibility
Migrated tests from deprecated react-dom/test-utils to @testing-library/react. Changes: - Install @testing-library/react, @testing-library/dom, @testing-library/jest-dom - Install jest-environment-jsdom (required in Jest 28+) - Update Jest config to use jsdom test environment - Create jestSetup.js for @testing-library/jest-dom matchers - Update testHelper.js to export React Testing Library utilities - Migrate Comment.spec.jsx to use React Testing Library - Migrate CommentList.spec.jsx to use React Testing Library The old tests used renderIntoDocument and other utilities from react-dom/test-utils which were removed in React 18+. All tests now pass locally. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 2f80b94 commit a8a8a7f

File tree

6 files changed

+558
-55
lines changed

6 files changed

+558
-55
lines changed
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
1-
import { React, TestUtils } from '../../../../../../libs/testHelper';
1+
import { React, render, screen } from '../../../../../../libs/testHelper';
22

33
import Comment from './Comment.jsx';
44

5-
const { renderIntoDocument, findRenderedDOMComponentWithClass, findRenderedDOMComponentWithTag } = TestUtils;
6-
75
describe('Comment', () => {
86
it('renders an author and comment with proper css classes', () => {
9-
const component = renderIntoDocument(<Comment author="Frank" text="Hi!" />);
7+
const { container } = render(<Comment author="Frank" text="Hi!" />);
108

11-
const comment = findRenderedDOMComponentWithTag(component, 'div');
12-
expect(comment.className).toEqual('comment');
13-
const author = findRenderedDOMComponentWithTag(component, 'h2');
14-
expect(author.className).toEqual('commentAuthor js-comment-author');
15-
const text = findRenderedDOMComponentWithTag(component, 'span');
16-
expect(text.className).toEqual('js-comment-text');
9+
const author = container.querySelector('h2.js-comment-author');
10+
expect(author).toBeInTheDocument();
11+
const text = container.querySelector('span.js-comment-text');
12+
expect(text).toBeInTheDocument();
1713
});
1814

1915
it('shows the author', () => {
20-
const component = renderIntoDocument(<Comment author="Frank" text="Hi!" />);
16+
render(<Comment author="Frank" text="Hi!" />);
2117

22-
const author = findRenderedDOMComponentWithClass(component, 'js-comment-author');
23-
expect(author.textContent).toEqual('Frank');
18+
const author = screen.getByText('Frank');
19+
expect(author).toHaveClass('js-comment-author');
2420
});
2521

2622
it('shows the comment text in markdown', () => {
27-
const component = renderIntoDocument(<Comment author="Frank" text="Hi!" />);
23+
const { container } = render(<Comment author="Frank" text="Hi!" />);
2824

29-
const comment = findRenderedDOMComponentWithClass(component, 'js-comment-text');
30-
expect(comment.textContent).toEqual('Hi!\n');
25+
// The text is rendered inside a span with dangerouslySetInnerHTML
26+
const comment = container.querySelector('span.js-comment-text');
27+
expect(comment).toBeInTheDocument();
28+
expect(comment.innerHTML).toContain('Hi!');
3129
});
3230
});

client/app/bundles/comments/components/CommentBox/CommentList/CommentList.spec.jsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { List, Map } from 'immutable';
2-
import { React, TestUtils } from '../../../../../libs/testHelper';
2+
import { React, render, screen } from '../../../../../libs/testHelper';
33

44
import CommentList from './CommentList.jsx';
5-
import Comment from './Comment/Comment.jsx';
6-
7-
const { renderIntoDocument, findRenderedDOMComponentWithTag, scryRenderedComponentsWithType } = TestUtils;
85

96
const cssTransitionGroupClassNames = {
107
enter: 'elementEnter',
@@ -30,25 +27,30 @@ describe('CommentList', () => {
3027
);
3128

3229
it('renders a list of Comments in normal order', () => {
33-
const component = renderIntoDocument(
30+
render(
3431
<CommentList $$comments={comments} cssTransitionGroupClassNames={cssTransitionGroupClassNames} />,
3532
);
36-
const list = scryRenderedComponentsWithType(component, Comment);
37-
expect(list.length).toEqual(2);
38-
expect(list[0].props.author).toEqual('Frank');
39-
expect(list[1].props.author).toEqual('Furter');
33+
34+
// Verify both authors are rendered in order
35+
expect(screen.getByText('Frank')).toBeInTheDocument();
36+
expect(screen.getByText('Furter')).toBeInTheDocument();
37+
38+
// Verify order by checking their positions in the DOM
39+
const authors = screen.getAllByRole('heading', { level: 2 });
40+
expect(authors[0]).toHaveTextContent('Frank');
41+
expect(authors[1]).toHaveTextContent('Furter');
4042
});
4143

4244
it('renders an alert if errors', () => {
43-
const component = renderIntoDocument(
45+
render(
4446
<CommentList
4547
$$comments={comments}
4648
error="zomg"
4749
cssTransitionGroupClassNames={cssTransitionGroupClassNames}
4850
/>,
4951
);
5052

51-
const alert = findRenderedDOMComponentWithTag(component, 'strong');
52-
expect(alert.textContent).toEqual('Comments could not be retrieved. ');
53+
const alert = screen.getByText('Comments could not be retrieved.');
54+
expect(alert.tagName).toBe('STRONG');
5355
});
5456
});

client/app/libs/jestSetup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/jest-dom';

client/app/libs/testHelper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable import/no-extraneous-dependencies */
22
import React from 'react';
3-
import TestUtils from 'react-dom/test-utils';
3+
import { render, screen, within } from '@testing-library/react';
44

5-
export { React, TestUtils };
5+
export { React, render, screen, within };

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@
109109
"@babel/preset-react": "^7.18.6",
110110
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.10",
111111
"@tailwindcss/typography": "^0.5.10",
112+
"@testing-library/dom": "^10.4.1",
113+
"@testing-library/jest-dom": "^6.9.1",
114+
"@testing-library/react": "^16.3.0",
112115
"@webpack-cli/serve": "^2.0.5",
113116
"babel-jest": "^29.5.0",
114117
"body-parser": "^1.20.2",
@@ -126,6 +129,7 @@
126129
"express": "^4.18.2",
127130
"identity-obj-proxy": "^3.0.0",
128131
"jest": "^29.5.0",
132+
"jest-environment-jsdom": "^30.2.0",
129133
"mini-css-extract-plugin": "^2.7.2",
130134
"preload-webpack-plugin": "^3.0.0-alpha.1",
131135
"prettier": "^2.2.1",
@@ -147,11 +151,12 @@
147151
"not IE 11"
148152
],
149153
"jest": {
154+
"testEnvironment": "jsdom",
150155
"moduleNameMapper": {
151156
"\\.scss$": "identity-obj-proxy"
152157
},
153-
"setupFiles": [
154-
"./client/app/libs/testHelper.js"
158+
"setupFilesAfterEnv": [
159+
"./client/app/libs/jestSetup.js"
155160
],
156161
"testRegex": "./client/(app|__tests__)/.*.spec\\.jsx?$",
157162
"transform": {

0 commit comments

Comments
 (0)