Skip to content

Commit 338307c

Browse files
authored
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.
1 parent 1f685ae commit 338307c

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
module.exports = global.process || require('../../compiled/process')
1+
module.exports =
2+
global.process?.env && typeof global.process?.env === 'object'
3+
? global.process
4+
: require('../../compiled/process')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Page() {
2+
return (
3+
<div id="process">
4+
Hello, {process.env.NEXT_PUBLIC_VISITOR ?? 'stranger'}
5+
</div>
6+
)
7+
}

test/integration/polyfills/test/index.test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,18 @@ describe('Polyfills', () => {
3636
await browser.close()
3737
})
3838

39+
it('should allow using process.env when there is an element with `id` of `process`', async () => {
40+
const browser = await webdriver(appPort, '/process')
41+
const text = await browser.elementByCss('#process').text()
42+
43+
expect(text).toBe('Hello, stranger')
44+
45+
await browser.close()
46+
})
47+
3948
it('should contain generated page count in output', async () => {
40-
expect(output).toContain('Generating static pages (0/4)')
41-
expect(output).toContain('Generating static pages (4/4)')
49+
expect(output).toContain('Generating static pages (0/5)')
50+
expect(output).toContain('Generating static pages (5/5)')
4251
// we should only have 1 segment and the initial message logged out
4352
expect(output.match(/Generating static pages/g).length).toBe(5)
4453
})

0 commit comments

Comments
 (0)