From 338307cd83f43f18b2a093dead2b96307a653c8e Mon Sep 17 00:00:00 2001 From: Gal Schlezinger Date: Thu, 13 Jan 2022 18:44:55 +0200 Subject: [PATCH] Fix global process testing for the process polyfill (#33220) 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. --- packages/next/build/polyfills/process.js | 5 ++++- test/integration/polyfills/pages/process.js | 7 +++++++ test/integration/polyfills/test/index.test.js | 13 +++++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 test/integration/polyfills/pages/process.js diff --git a/packages/next/build/polyfills/process.js b/packages/next/build/polyfills/process.js index 054c87b91088681..26a02f5b34f7f84 100644 --- a/packages/next/build/polyfills/process.js +++ b/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') diff --git a/test/integration/polyfills/pages/process.js b/test/integration/polyfills/pages/process.js new file mode 100644 index 000000000000000..c293e0f5df0dabd --- /dev/null +++ b/test/integration/polyfills/pages/process.js @@ -0,0 +1,7 @@ +export default function Page() { + return ( +
+ Hello, {process.env.NEXT_PUBLIC_VISITOR ?? 'stranger'} +
+ ) +} diff --git a/test/integration/polyfills/test/index.test.js b/test/integration/polyfills/test/index.test.js index b37d3417a004a9b..48734881d92b82e 100644 --- a/test/integration/polyfills/test/index.test.js +++ b/test/integration/polyfills/test/index.test.js @@ -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) })