Skip to content

Commit

Permalink
Fix global process testing for the process polyfill (#33220)
Browse files Browse the repository at this point in the history
When there is a DOM element with id of `process`, the DOM marks it as a global, so `window.process` would exist. We should check for `process.env` to make sure it is available too.
  • Loading branch information
Schniz committed Jan 13, 2022
1 parent 1f685ae commit 338307c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/next/build/polyfills/process.js
@@ -1 +1,4 @@
module.exports = global.process || require('../../compiled/process')
module.exports =
global.process?.env && typeof global.process?.env === 'object'
? global.process
: require('../../compiled/process')
7 changes: 7 additions & 0 deletions test/integration/polyfills/pages/process.js
@@ -0,0 +1,7 @@
export default function Page() {
return (
<div id="process">
Hello, {process.env.NEXT_PUBLIC_VISITOR ?? 'stranger'}
</div>
)
}
13 changes: 11 additions & 2 deletions test/integration/polyfills/test/index.test.js
Expand Up @@ -36,9 +36,18 @@ describe('Polyfills', () => {
await browser.close()
})

it('should allow using process.env when there is an element with `id` of `process`', async () => {
const browser = await webdriver(appPort, '/process')
const text = await browser.elementByCss('#process').text()

expect(text).toBe('Hello, stranger')

await browser.close()
})

it('should contain generated page count in output', async () => {
expect(output).toContain('Generating static pages (0/4)')
expect(output).toContain('Generating static pages (4/4)')
expect(output).toContain('Generating static pages (0/5)')
expect(output).toContain('Generating static pages (5/5)')
// we should only have 1 segment and the initial message logged out
expect(output.match(/Generating static pages/g).length).toBe(5)
})
Expand Down

0 comments on commit 338307c

Please sign in to comment.