<h1 dir="auto">Bug report</h1>
<p dir="auto">When unit testing a utility function that needs access to the publicRuntimeConfig with jest, it throws error <code class="notranslate">Cannot read property 'publicRuntimeConfig' of undefined</code></p>
<h2 dir="auto">Describe the bug</h2>
<p dir="auto">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.</p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">// next.config.js</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="module.exports = () =&gt; {
  return {
    publicRuntimeConfig: {
      accessToken: '1234565',
    }
  };
};"><pre class="notranslate"><code class="notranslate">module.exports = () =&gt; {
  return {
    publicRuntimeConfig: {
      accessToken: '1234565',
    }
  };
};
</code></pre></div>
<p dir="auto">// util.js</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import getConfig from 'next/config'
const { publicRuntimeConfig } = getConfig()
const client = createClient({
   accessToken: publicRuntimeConfig.accessToken
})
function mathCaculation(){
  return 1+1
}"><pre class="notranslate"><code class="notranslate">import getConfig from 'next/config'
const { publicRuntimeConfig } = getConfig()
const client = createClient({
   accessToken: publicRuntimeConfig.accessToken
})
function mathCaculation(){
  return 1+1
}
</code></pre></div>
<p dir="auto">util.test.js</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="expect(mathCaculation()).toBe(2)"><pre class="notranslate"><code class="notranslate">expect(mathCaculation()).toBe(2)
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Cannot read property 'publicRuntimeConfig' of undefined"><pre class="notranslate"><code class="notranslate">Cannot read property 'publicRuntimeConfig' of undefined
</code></pre></div>
<h2 dir="auto">System information</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&quot;next&quot;: &quot;^7.0.2&quot;,"><pre class="notranslate"><code class="notranslate">"next": "^7.0.2",
</code></pre></div>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have searched the <a href="https://github.com/zeit/next.js/issues?q=is%3Aissue">issues</a> of this repository and believe that this is not a duplicate.</li>
</ul>
<h2 dir="auto">Expected Behavior</h2>
<h2 dir="auto">Steps to Reproduce (for bugs)</h2>
<p dir="auto">next.config.js</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="module.exports = (phase, { defaultConfig }) =&gt; {
  return {
    publicRuntimeConfig: {
      defaultEnvironmentSettings: 'test',
    },
    webpack: (config, { buildId, dev, isServer, defaultLoaders }) =&gt; {
      // XXX https://github.com/evanw/node-source-map-support/issues/155
      config.node = {
        fs: 'empty',
        module: 'empty',
      };
      return config;
    },
  };
};
"><pre class="notranslate"><span class="pl-smi">module</span><span class="pl-kos">.</span><span class="pl-en">exports</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-s1">phase</span><span class="pl-kos">,</span> <span class="pl-kos">{</span> defaultConfig <span class="pl-kos">}</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-k">return</span> <span class="pl-kos">{</span>
    <span class="pl-c1">publicRuntimeConfig</span>: <span class="pl-kos">{</span>
      <span class="pl-c1">defaultEnvironmentSettings</span>: <span class="pl-s">'test'</span><span class="pl-kos">,</span>
    <span class="pl-kos">}</span><span class="pl-kos">,</span>
    <span class="pl-en">webpack</span>: <span class="pl-kos">(</span><span class="pl-s1">config</span><span class="pl-kos">,</span> <span class="pl-kos">{</span> buildId<span class="pl-kos">,</span> dev<span class="pl-kos">,</span> isServer<span class="pl-kos">,</span> defaultLoaders <span class="pl-kos">}</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
      <span class="pl-c">// XXX https://github.com/evanw/node-source-map-support/issues/155</span>
      <span class="pl-s1">config</span><span class="pl-kos">.</span><span class="pl-c1">node</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span>
        <span class="pl-c1">fs</span>: <span class="pl-s">'empty'</span><span class="pl-kos">,</span>
        <span class="pl-c1">module</span>: <span class="pl-s">'empty'</span><span class="pl-kos">,</span>
      <span class="pl-kos">}</span><span class="pl-kos">;</span>
      <span class="pl-k">return</span> <span class="pl-s1">config</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos">,</span>
  <span class="pl-kos">}</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">page</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const { publicRuntimeConfig } = getConfig();
const { groupSettings, url } = this.props;
const schoolName = url.query.schoolName;
console.log('publicRuntimeConfig', publicRuntimeConfig) // publicRuntimeConfig undefined
console.log('getConfig()', getConfig()) // getConfig()  serverRuntimeConfig:{}, publicRuntimeConfig: undefined
"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-kos">{</span> publicRuntimeConfig <span class="pl-kos">}</span> <span class="pl-c1">=</span> <span class="pl-en">getConfig</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">const</span> <span class="pl-kos">{</span> groupSettings<span class="pl-kos">,</span> url <span class="pl-kos">}</span> <span class="pl-c1">=</span> <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">props</span><span class="pl-kos">;</span>
<span class="pl-k">const</span> <span class="pl-s1">schoolName</span> <span class="pl-c1">=</span> <span class="pl-s1">url</span><span class="pl-kos">.</span><span class="pl-c1">query</span><span class="pl-kos">.</span><span class="pl-c1">schoolName</span><span class="pl-kos">;</span>
<span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">'publicRuntimeConfig'</span><span class="pl-kos">,</span> <span class="pl-s1">publicRuntimeConfig</span><span class="pl-kos">)</span> <span class="pl-c">// publicRuntimeConfig undefined</span>
<span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">'getConfig()'</span><span class="pl-kos">,</span> <span class="pl-en">getConfig</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">)</span> <span class="pl-c">// getConfig()  serverRuntimeConfig:{}, publicRuntimeConfig: undefined</span></pre></div>
<h2 dir="auto">Context</h2>
<p dir="auto"><code class="notranslate">publicRuntimeConfig</code> is undefined</p>
<p dir="auto">This only happens when I use <code class="notranslate">NODE_ENV staging</code>, it works fine in development mode. (I haven't tested against production env)</p>
<p dir="auto">Note that my staging environment is hosted on AWS lambda, but it's maybe not related to the issue.</p>