next.js: [Next.js 9.0.2] Shallow rendering issue: App cannot be invoked without 'new'

Bug report

Describe the bug

After updating our app from Next.js 7.0.2 to 9.0.2 enzyme’s shallow rendering (same issue with enzyme’s mount) for the custom <App> (_app.is) not working anymore. The local development server is serving the app perfectly fine, though.

For experimental purposes we also tried running our tests with Next.js 8.1.0 and the rendering caused no problems.

To Reproduce

Clone the issue repository: https://github.com/rzschoch/next-9-issues/tree/shallow-issue

  1. Run npm install
  2. Run npm run test
  3. See error: TypeError: Class constructor App cannot be invoked without 'new'

Expected behavior

The test in /src/pagesSpecs/_app.spec.js should be rendered without issues and compared against the existing snapshot (from Next.js 7.0.2).

Screenshots

image

System information

  • OS: MacOS 10.14.5 / Windows 10.0.17763 Build 17763
  • Version of Next.js: 9.0.2

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Never mind, removing "@babel/preset-env" from "presets" solve this issue.

after i removed the preset-env in presets, all i had in .babelrc was

   "presets": [
    "next/babel"
  ] 

and it didn’t work

After that i changed to

"presets": [
    [
      "next/babel",
      {
        "preset-env": { "targets": { "node": "current" } }
      }
    ]
  ]

and it works 👍

Never mind, removing "@babel/preset-env" from "presets" solve this issue.

It is now working fine with the latest canary (9.0.3-canary.6 in my case) and a .babelrc like so:

{
  "presets": ["next/babel"],
  "plugins": ["inline-react-svg"]
}

Same issue in 9.0.3 not happen in 9.0.2

That’s @huv1k solution for now (thank you): “We updated our build to use modern javascript with esmodules in this PR (https://github.com/zeit/next.js/pull/7014). There is a problem that node doesn’t support it yet so you need to use for your tests current node. You can achieve this with this .babelrc:“

{
  "presets": ["next/babel"],
  "plugins": ["inline-react-svg"],
  "env": {
    "test": {
      "presets": [
        ["next/babel", { "preset-env": { "targets": { "node": "current" } } }]
      ],
      "plugins": ["dynamic-import-node"]
    }
  }
}

Never mind, removing "@babel/preset-env" from "presets" solve this issue.

This also worked for me, I replaced

["@babel/preset-env", { "targets": "> 0.5% in US" } ]

with

["next/babel", { "preset-env": { "targets": "> 0.5% in US" } }]

Just a data point, I was able to resolve this issue with unit testing a custom _app.js by giving my App subclass a no-op constructor:

  // This no-op constructor helps with an error:
  // TypeError: Class constructor App cannot be invoked without 'new'
  constructor(props) {
    super(props);
  }

The latest canary didn’t work for me, but the following did:

{
  "plugins": [],
  "presets": [
    [
      "next/babel",
      {
        "preset-env": { "targets": { "node": "current" } }
      }
    ]
  ]
}