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

publicRuntimeConfig undefined in test environment with Jest. #6187

Closed
YIZHUANG opened this issue Jan 31, 2019 · 4 comments · Fixed by #6458
Closed

publicRuntimeConfig undefined in test environment with Jest. #6187

YIZHUANG opened this issue Jan 31, 2019 · 4 comments · Fixed by #6458
Assignees

Comments

@YIZHUANG
Copy link

YIZHUANG commented Jan 31, 2019

Bug report

When unit testing a utility function that needs access to the publicRuntimeConfig with jest, it throws error Cannot read property 'publicRuntimeConfig' of undefined

Describe the bug

For example if i am testing a file that has Contentful stuff in it, and the access token those kind of stuff comes from publicRuntimeConfig, then the test won't past and shows above error.

To Reproduce

// next.config.js

module.exports = () => {
  return {
    publicRuntimeConfig: {
      accessToken: '1234565',
    }
  };
};

// util.js

import getConfig from 'next/config'
const { publicRuntimeConfig } = getConfig()
const client = createClient({
   accessToken: publicRuntimeConfig.accessToken
})
function mathCaculation(){
  return 1+1
}

util.test.js

expect(mathCaculation()).toBe(2)
Cannot read property 'publicRuntimeConfig' of undefined

System information

"next": "^7.0.2",
@timneutkens
Copy link
Member

Duplicate of #4024

Specifically: #4024 (comment)

@damusnet
Copy link
Contributor

Hi all,

I think I have a different solution/explanation. I too was hit by tests not passing anymore when upgrading from Next.js 7 to 8.

Previously I had a Jest mock in __mocks__/next/config.js. However, it looks to me like even though we do import getConfig from 'next/config', the mock must now be at __mocks__/next-server/config.js. Not entirely quite sure yet why, or what changed recently, but this at least fixes it for me.

The same is true for __mocks__/next/dynamic.js which is now __mocks__/next-server/dynamic.js.

It also applies if you used to do jest.mock('next/config', () => () => ({ publicRuntimeConfig: { ... } })); for instance.

But I think it makes sense:
image

Maybe the docs should be updated to reflect the new structure and have import getConfig from 'next-server/config'?

@gabrieledarrigo
Copy link

@damusnet I occurred into the same problem, thank you so much for spotting this!
@timneutkens should this be documented?

@timneutkens
Copy link
Member

This is something we can solve on the Next.js side of things I think 🤔

@timneutkens timneutkens reopened this Feb 26, 2019
timneutkens pushed a commit that referenced this issue Feb 27, 2019
After discussion it was decided instead of rewriting `next/config` and `next/head` imports to `next-server/config` and `next-server/head` we should just allow importing them as `next/config` and `next/head`
 
Fixes: #6187
@lock lock bot locked as resolved and limited conversation to collaborators Feb 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants