vercel / next.js

The React Framework
https://nextjs.org
MIT License
125.94k stars 26.87k forks source link

Adding `id="jest"` to any element in Create Next App throws console errors #27322

Open BenjaminWFox opened 3 years ago

BenjaminWFox commented 3 years ago

What version of Next.js are you using?

11.0.1, 11.0.2-canary.17

What version of Node.js are you using?

16.3.0

What browser are you using?

Chrome, Firefox, Safari

What operating system are you using?

MacOS

How are you deploying your application?

Locally

Describe the Bug

Creating a new project using npm init next-app and adding id="jest" to any element on the default index page throws console errors.

Expected Behavior

I do not expect test errors in non-test code.

To Reproduce

Create a new next app via create-next-app or npm init next-app

Add id="jest" to any element on the homepage.

Screen Shot 2021-07-19 at 10 01 21 PM
ijjk commented 3 years ago

Hi, those errors are not coming from Next.js directly as running npm init next-app and then yarn dev does not show those errors, are you adding custom configuration after creating your app with npm init next-app or is a specific example being used?

mattcarlotta commented 3 years ago

@ijjk I'm able to replicate this issue with the following:

1.) yarn create next-app my-app 2.) Edit line 19-21 of pages/index.js to be:

<p id="jest" className={styles.description}>
  Get started by editing{' '}
  <code className={styles.code}>pages/index.js</code>
</p>

3.) Run yarn dev and open localhost:3000 4.) Result: Screenshot from 2021-07-20 11-14-04

node -v: v14.17.2 npm -v: 6.14.13 yarn -v: 1.22.10

Full Log ``` react-dom.development.js?61bb:67 Warning: An update to ReactDevOverlay ran an effect, but was not wrapped in act(...). When testing, code that causes React state updates should be wrapped into act(...): act(() => { /* fire events that update state */ }); /* assert on the output */ This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act at ReactDevOverlay (webpack-internal:///./node_modules/@next/react-dev-overlay/lib/internal/ReactDevOverlay.js:86:23) at Container (webpack-internal:///./node_modules/next/dist/client/index.js:160:5) at AppContainer (webpack-internal:///./node_modules/next/dist/client/index.js:648:24) at Root (webpack-internal:///./node_modules/next/dist/client/index.js:784:25) printWarning @ react-dom.development.js?61bb:67 react-dom.development.js?61bb:67 Warning: An update to Image ran an effect, but was not wrapped in act(...). When testing, code that causes React state updates should be wrapped into act(...): act(() => { /* fire events that update state */ }); /* assert on the output */ This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act at Image (webpack-internal:///./node_modules/next/dist/client/image.js:212:18) at span at a at footer at div at Home at MyApp (webpack-internal:///./pages/_app.js:19:24) at ErrorBoundary (webpack-internal:///./node_modules/@next/react-dev-overlay/lib/internal/ErrorBoundary.js:26:47) at ReactDevOverlay (webpack-internal:///./node_modules/@next/react-dev-overlay/lib/internal/ReactDevOverlay.js:86:23) at Container (webpack-internal:///./node_modules/next/dist/client/index.js:160:5) at AppContainer (webpack-internal:///./node_modules/next/dist/client/index.js:648:24) at Root (webpack-internal:///./node_modules/next/dist/client/index.js:784:25) printWarning @ react-dom.development.js?61bb:67 2react-dom.development.js?61bb:67 Warning: An update to Portal ran an effect, but was not wrapped in act(...). When testing, code that causes React state updates should be wrapped into act(...): act(() => { /* fire events that update state */ }); /* assert on the output */ This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act at Portal (webpack-internal:///./node_modules/next/dist/client/portal/index.js:20:23) at ErrorBoundary (webpack-internal:///./node_modules/@next/react-dev-overlay/lib/internal/ErrorBoundary.js:26:47) at ReactDevOverlay (webpack-internal:///./node_modules/@next/react-dev-overlay/lib/internal/ReactDevOverlay.js:86:23) at Container (webpack-internal:///./node_modules/next/dist/client/index.js:160:5) at AppContainer (webpack-internal:///./node_modules/next/dist/client/index.js:648:24) at Root (webpack-internal:///./node_modules/next/dist/client/index.js:784:25) printWarning @ react-dom.development.js?61bb:67 react-dom.development.js?61bb:67 Warning: An update to RouteAnnouncer ran an effect, but was not wrapped in act(...). When testing, code that causes React state updates should be wrapped into act(...): act(() => { /* fire events that update state */ }); /* assert on the output */ This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act at RouteAnnouncer (webpack-internal:///./node_modules/next/dist/client/route-announcer.js:17:36) at Portal (webpack-internal:///./node_modules/next/dist/client/portal/index.js:20:23) at ErrorBoundary (webpack-internal:///./node_modules/@next/react-dev-overlay/lib/internal/ErrorBoundary.js:26:47) at ReactDevOverlay (webpack-internal:///./node_modules/@next/react-dev-overlay/lib/internal/ReactDevOverlay.js:86:23) at Container (webpack-internal:///./node_modules/next/dist/client/index.js:160:5) at AppContainer (webpack-internal:///./node_modules/next/dist/client/index.js:648:24) at Root (webpack-internal:///./node_modules/next/dist/client/index.js:784:25) printWarning @ react-dom.development.js?61bb:67 react-dom.development.js?61bb:67 Warning: An update to Image inside a test was not wrapped in act(...). When testing, code that causes React state updates should be wrapped into act(...): act(() => { /* fire events that update state */ }); /* assert on the output */ This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act at Image (webpack-internal:///./node_modules/next/dist/client/image.js:212:18) at span at a at footer at div at Home at MyApp (webpack-internal:///./pages/_app.js:19:24) at ErrorBoundary (webpack-internal:///./node_modules/@next/react-dev-overlay/lib/internal/ErrorBoundary.js:26:47) at ReactDevOverlay (webpack-internal:///./node_modules/@next/react-dev-overlay/lib/internal/ReactDevOverlay.js:86:23) at Container (webpack-internal:///./node_modules/next/dist/client/index.js:160:5) at AppContainer (webpack-internal:///./node_modules/next/dist/client/index.js:648:24) at Root (webpack-internal:///./node_modules/next/dist/client/index.js:784:25) printWarning @ react-dom.development.js?61bb:67 react-dom.development.js?61bb:67 Warning: An update to Image ran an effect, but was not wrapped in act(...). When testing, code that causes React state updates should be wrapped into act(...): act(() => { /* fire events that update state */ }); /* assert on the output */ This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act at Image (webpack-internal:///./node_modules/next/dist/client/image.js:212:18) at span at a at footer at div at Home at MyApp (webpack-internal:///./pages/_app.js:19:24) at ErrorBoundary (webpack-internal:///./node_modules/@next/react-dev-overlay/lib/internal/ErrorBoundary.js:26:47) at ReactDevOverlay (webpack-internal:///./node_modules/@next/react-dev-overlay/lib/internal/ReactDevOverlay.js:86:23) at Container (webpack-internal:///./node_modules/next/dist/client/index.js:160:5) at AppContainer (webpack-internal:///./node_modules/next/dist/client/index.js:648:24) at Root (webpack-internal:///./node_modules/next/dist/client/index.js:784:25) ```
ijjk commented 3 years ago

@mattcarlotta hmm can you confirm the Next.js version and react version being used with npm ls next react react-dom? This warning should only be triggered when a global jest is set it seems per the react source here

mattcarlotta commented 3 years ago

@ijjk Here's the result:

npm ls next react react-dom
my-app@0.1.0 /home/m6d/Desktop/my-app
├── next@11.0.1 
├── react@17.0.2 
└── react-dom@17.0.2 

Possibly related to the fact that any HTMLElement with an id attribute is being interpolated as a global:

For example:

if (typeof window !== "undefined") {
  setTimeout(() => {
    console.log("window.jest", window.jest);
    console.log("window.example", window.example);
  }, 1000);
}

...etc

 <p id="jest" className={styles.description}>
  Get started by editing{" "}
  <code className={styles.code}>pages/index.js</code>
 </p>
 <p id="example">Example</p>

Result: Screenshot from 2021-07-20 12-53-19

If I just set window.jest to "123", I also get the warning:

if (typeof window !== "undefined") {
  window.jest = "123";
  setTimeout(() => {
    console.log("window.jest", window.jest);
  }, 1000);
}

Screenshot from 2021-07-20 13-08-14

However, this doesn't affect the CRA (no warnings): Screenshot from 2021-07-20 13-17-06

rsylvian commented 1 year ago

Confirming this issue. I just run into that as well.

"react": "17.0.2",
"react-dom": "17.0.2",
"next": "12.3.1",