question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

publicRuntimeConfig undefined in test environment with Jest.

See original GitHub issue

Bug report

TypeError: Cannot read property ‘CMS_URL’ of undefined

Describe the bug

when I call service in getInitialProps

class IndexService {
  getPage(lang = 'en') {
    return axios
      .get(
        `${publicRuntimeConfig.CMS_URL}

A clear and concise description of what the bug is.

To Reproduce

I have jest.config.js as

module.exports = {
  setupFiles: ['./jest.setup.js'],
  testPathIgnorePatterns: ['./.next/', './node_modules/'],
  moduleNameMapper: {
    "^.+\\.(css|less|scss)$": "babel-jest"
  },
};

I have jest.setup.js as

import { configure } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import { setConfig } from 'next/config'
import config from './next.config'

// Make sure you can use "publicRuntimeConfig" within tests.
setConfig(config.publicRuntimeConfig)
configure({ adapter: new Adapter() })

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Go to ‘…’
  2. Click on ‘…’
  3. Scroll down to ‘…’
  4. See error

Expected behavior

The thing is when I call a service class from other files as indexService I got this error

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • Version of Next.js: 9

Additional context

Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
Harrisonkamaucommented, Nov 3, 2020

In my case, I mocked the next/config as shown:

// jest.setup.js
jest.mock('next/config', () => () => ({
  publicRuntimeConfig: { // put test stuff here },
}));

Then call it in your jest.config.js - I did this so it can run in every component. If you don’t like this behavior, then mock the next/config inside of the component you’re testing.

module.exports = {
 setupFiles: ['./jest.setup.js'],
};
1reaction
kachkaevcommented, Dec 17, 2019

@ShintaroNippon if you’re using any plugins in next.config.js, there is a chance that its default export will be a function, not an object. Therefore, config.publicRuntimeConfig will be undefined.

Try console.log(config) in your jestSetup.js. If you don’t see any logs at all, your jest setup file is not invoked – that’s a separate issue, solved via jest docs. If you see a printed function in the output, try the following:

import { setConfig } from "next/config"
import generateNextConfig from "./next.config"
// or
// import * as generateNextConfig from "./next.config"

// Make sure you can use "publicRuntimeConfig" within tests
setConfig({
  publicRuntimeConfig: generateNextConfig("", {}).publicRuntimeConfig,
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

publicRuntimeConfig undefined in test environment with Jest.
A clear and concise description of what the bug is. To Reproduce. I have jest.config.js as. module.exports = { setupFiles: ['./jest.setup.
Read more >
Next.JS & JEST - publicRuntimeConfig undefined
I've tried using jest mock in test case file. jest.mock('next/config', () => () => ({ publicRuntimeConfig ...
Read more >
next.config.js: Runtime Configuration
Place any server-only runtime config under serverRuntimeConfig . Anything accessible to both client and server-side code should be under publicRuntimeConfig . A ...
Read more >
Next.JS & JEST - publicRuntimeConfig undefined-Reactjs
I solved this problem by creating a jest.setup.js file and adding this line of code // jest.config.js module.exports = { // Your config...
Read more >
Configuring package.json · Jest
The test environment that will be used for testing. The default environment in Jest is a browser-like environment through jsdom. If you are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found