Skip to content

Commit 4956cda

Browse files
committed
fix: fix react test setup
1 parent 3c2cb62 commit 4956cda

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

client/src/skeletons/react.js

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,37 +40,39 @@ export default function App() {
4040
}
4141
`;
4242

43-
const jasmineTest = `import React from 'react';
44-
import ShallowRenderer from 'react-shallow-renderer';
43+
const testSetup = `global.IS_REACT_ACT_ENVIRONMENT = true;`;
44+
45+
const jasmineTest = `import React, {act} from 'react';
46+
import ReactDOMClient from 'react-dom/client';
4547
import App from '../src/App';
4648
4749
describe('Component App', () => {
48-
it('should render message', () => {
49-
const renderer = new ShallowRenderer();
50-
renderer.render(<App />);
51-
let result = renderer.getRenderOutput();
52-
expect(result.type).toBe('div');
53-
expect(result.props.children).toEqual(
54-
<h1>Hello React!</h1>
55-
);
50+
it('should render message', async () => {
51+
const container = document.createElement('div');
52+
53+
await act( async () => {
54+
ReactDOMClient.createRoot(container).render(<App />);
55+
});
56+
const h1 = container.querySelector('h1');
57+
expect(h1.textContent).toBe('Hello React!');
5658
});
5759
});
5860
`;
5961

60-
const mochaTest = `import React from 'react';
61-
import ShallowRenderer from 'react-shallow-renderer';
62-
import {expect} from 'chai';
62+
const mochaTest = `import React, {act} from 'react';
63+
import ReactDOMClient from 'react-dom/client';
64+
import { expect } from 'chai';
6365
import App from '../src/App';
6466
6567
describe('Component App', () => {
66-
it('should render message', () => {
67-
const renderer = new ShallowRenderer();
68-
renderer.render(<App />);
69-
let result = renderer.getRenderOutput();
70-
expect(result.type).to.equal('div');
71-
expect(result.props.children).to.deep.equal(
72-
<h1>Hello React!</h1>
73-
);
68+
it('should render message', async () => {
69+
const container = document.createElement('div');
70+
71+
await act( async () => {
72+
ReactDOMClient.createRoot(container).render(<App />);
73+
});
74+
const h1 = container.querySelector('h1');
75+
expect(h1.textContent).to.equal('Hello React!');
7476
});
7577
});
7678
`;
@@ -96,16 +98,24 @@ export default function({transpiler, testFramework}) {
9698
}
9799
];
98100

99-
if (testFramework === 'jasmine') {
100-
files.push({
101-
filename: `test/app.spec${ext}`,
102-
content: jasmineTest
103-
});
104-
} else if (testFramework === 'mocha') {
101+
102+
if (testFramework !== 'none') {
105103
files.push({
106-
filename: `test/app.spec${ext}`,
107-
content: mochaTest
104+
filename: `test/setup${ext}`,
105+
content: testSetup
108106
});
107+
108+
if (testFramework === 'jasmine') {
109+
files.push({
110+
filename: `test/app.spec${ext}`,
111+
content: jasmineTest
112+
});
113+
} else if (testFramework === 'mocha') {
114+
files.push({
115+
filename: `test/app.spec${ext}`,
116+
content: mochaTest
117+
});
118+
}
109119
}
110120

111121
return files;

0 commit comments

Comments
 (0)