Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Fix tests #1288

Merged
merged 4 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const {jest: jestConfig} = require('kcd-scripts/config')

module.exports = Object.assign(jestConfig, {
coverageThreshold: {
...jestConfig.coverageThreshold,
// Full coverage across the build matrix (React versions) but not in a single job
// Ful coverage is checked via codecov
'./src/pure.js': {
// minimum coverage of jobs using different React versions
branches: 97,
functions: 88,
lines: 94,
statements: 94,
},
},
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@testing-library/jest-dom": "^5.11.6",
"chalk": "^4.1.2",
"dotenv-cli": "^4.0.0",
"jest-diff": "^29.4.1",
"jest-diff": "^29.7.0",
"kcd-scripts": "^13.0.0",
"npm-run-all": "^4.1.5",
"react": "^18.0.0",
Expand Down
31 changes: 23 additions & 8 deletions src/__tests__/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import ReactDOM from 'react-dom'
import ReactDOMServer from 'react-dom/server'
import {fireEvent, render, screen, configure} from '../'

// Needs to be changed to 19.0.0 once alpha started.
const isReactExperimental = React.version.startsWith('18.3.0-experimental')
const isReactCanary = React.version.startsWith('18.3.0')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we check for React.version.startsWith('18.3.0-canary') here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep


// Needs to be changed to isReactExperimental || isReactCanary once alpha started.
const testGateReact18 = isReactExperimental ? test.skip : test

describe('render API', () => {
let originalConfig
beforeEach(() => {
Expand Down Expand Up @@ -213,27 +220,35 @@ describe('render API', () => {
expect(wrapperComponentMountEffect).toHaveBeenCalledTimes(1)
})

test('legacyRoot uses legacy ReactDOM.render', () => {
testGateReact18('legacyRoot uses legacy ReactDOM.render', () => {
expect(() => {
render(<div />, {legacyRoot: true})
}).toErrorDev(
[
"Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot",
],
isReactCanary
? [
"Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://react.dev/link/switch-to-createroot",
]
: [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this? the reactjs.org link has a redirect to the react.dev one. Is this some sort of preparation for future stuff?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what React is logging

"Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot",
],
{withoutStack: true},
)
})

test('legacyRoot uses legacy ReactDOM.hydrate', () => {
testGateReact18('legacyRoot uses legacy ReactDOM.hydrate', () => {
const ui = <div />
const container = document.createElement('div')
container.innerHTML = ReactDOMServer.renderToString(ui)
expect(() => {
render(ui, {container, hydrate: true, legacyRoot: true})
}).toErrorDev(
[
"Warning: ReactDOM.hydrate is no longer supported in React 18. Use hydrateRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot",
],
isReactCanary
? [
"Warning: ReactDOM.hydrate is no longer supported in React 18. Use hydrateRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://react.dev/link/switch-to-createroot",
]
: [
"Warning: ReactDOM.hydrate is no longer supported in React 18. Use hydrateRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot",
],
{withoutStack: true},
)
})
Expand Down
19 changes: 15 additions & 4 deletions src/__tests__/renderHook.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import React from 'react'
import {renderHook} from '../pure'

// Needs to be changed to 19.0.0 once alpha started.
const isReactExperimental = React.version.startsWith('18.3.0-experimental')
const isReactCanary = React.version.startsWith('18.3.0')

// Needs to be changed to isReactExperimental || isReactCanary once alpha started.
const testGateReact18 = isReactExperimental ? test.skip : test

test('gives committed result', () => {
const {result} = renderHook(() => {
const [state, setState] = React.useState(1)
Expand Down Expand Up @@ -61,7 +68,7 @@ test('allows wrapper components', async () => {
expect(result.current).toEqual('provided')
})

test('legacyRoot uses legacy ReactDOM.render', () => {
testGateReact18('legacyRoot uses legacy ReactDOM.render', () => {
const Context = React.createContext('default')
function Wrapper({children}) {
return <Context.Provider value="provided">{children}</Context.Provider>
Expand All @@ -78,9 +85,13 @@ test('legacyRoot uses legacy ReactDOM.render', () => {
},
).result
}).toErrorDev(
[
"Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot",
],
isReactCanary
? [
"Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://react.dev/link/switch-to-createroot",
]
: [
"Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot",
],
{withoutStack: true},
)
expect(result.current).toEqual('provided')
Expand Down
2 changes: 1 addition & 1 deletion tests/toWarnDev.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ SOFTWARE.
/* eslint-disable func-names */
/* eslint-disable complexity */
const util = require('util')
const jestDiff = require('jest-diff').default
const jestDiff = require('jest-diff').diff
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea why this ever worked. It didn't change in jest-diff between 29.4.1 and 29.7.0 so it must have been some other thing that made it work incidentally.

const shouldIgnoreConsoleError = require('./shouldIgnoreConsoleError')

function normalizeCodeLocInfo(str) {
Expand Down
Loading