publicRuntimeConfig undefined in test environment with Jest.
See original GitHub issueBug 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:
- Go to ‘…’
- Click on ‘…’
- Scroll down to ‘…’
- 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:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
In my case, I mocked the
next/config
as shown: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 thenext/config
inside of the component you’re testing.@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 yourjestSetup.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: