-
Notifications
You must be signed in to change notification settings - Fork 27.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial process.env stubbing for new env support (#11893)
* Add initial process.env stubbing for new env support * Fix server process.env being stubbed in production * bump Co-authored-by: Joe Haddad <joe.haddad@zeit.co>
- Loading branch information
Showing
10 changed files
with
180 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
experimental: { | ||
pageEnv: true, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default () => { | ||
console.log(process.env.I_SHOULD_BE_HERE) | ||
return <p>hi there 👋</p> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default (req, res) => { | ||
console.log(process.env.where_is_it) | ||
res.end('done') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export default () => { | ||
return <p>hi there 👋</p> | ||
} | ||
|
||
export const getStaticProps = () => { | ||
console.log(process.env.SECRET) | ||
return { | ||
props: { | ||
hi: 'there', | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export default () => { | ||
return <p>hi there 👋</p> | ||
} | ||
|
||
export const getServerSideProps = () => { | ||
console.log(process.env.SECRET) | ||
return { | ||
props: { | ||
hi: 'there', | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default () => { | ||
console.log(process.env.hi) | ||
return <p>hi there 👋</p> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default () => { | ||
console.log(process.env.NEXT_PUBLIC_HI) | ||
return <p>hi there 👋</p> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* eslint-env jest */ | ||
/* global jasmine */ | ||
import { | ||
findPort, | ||
killApp, | ||
launchApp, | ||
renderViaHTTP, | ||
waitFor, | ||
} from 'next-test-utils' | ||
import { join } from 'path' | ||
import webdriver from 'next-webdriver' | ||
|
||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2 | ||
const appDir = join(__dirname, '..') | ||
let app | ||
let stderr | ||
let appPort | ||
|
||
const buildWarning = prop => | ||
`An environment variable (${prop}) that was not provided in the environment was accessed` | ||
|
||
const checkMissing = async (pathname, prop, shouldWarn = false) => { | ||
stderr = '' | ||
await renderViaHTTP(appPort, pathname) | ||
await waitFor(1000) | ||
|
||
if (shouldWarn) { | ||
expect(stderr).toContain(buildWarning(prop)) | ||
} else { | ||
expect(stderr).not.toContain(buildWarning(prop)) | ||
} | ||
} | ||
|
||
const checkMissingClient = async (pathname, prop, shouldWarn = false) => { | ||
const browser = await webdriver(appPort, '/404') | ||
await browser.eval(`(function() { | ||
window.warnLogs = [] | ||
var origWarn = console.warn | ||
console.warn = function() { | ||
window.warnLogs.push(arguments[0]) | ||
origWarn.apply(this, arguments) | ||
} | ||
window.next.router.push("${pathname}") | ||
})()`) | ||
await waitFor(2000) | ||
|
||
const logs = await browser.eval(`window.warnLogs`) | ||
const warning = buildWarning(prop) | ||
const hasWarn = logs.some(log => log.includes(warning)) | ||
|
||
expect(hasWarn).toBe(shouldWarn) | ||
await browser.close() | ||
} | ||
|
||
describe('process.env stubbing', () => { | ||
beforeAll(async () => { | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort, { | ||
env: { | ||
NEXT_PUBLIC_HI: 'hi', | ||
I_SHOULD_BE_HERE: 'hello', | ||
}, | ||
onStderr(msg) { | ||
stderr += msg || '' | ||
}, | ||
}) | ||
}) | ||
afterAll(() => killApp(app)) | ||
|
||
describe('server side', () => { | ||
it('should not show missing env value when its not missing public', async () => { | ||
await checkMissing('/not-missing', 'NEXT_PUBLIC_HI') | ||
}) | ||
|
||
it('should not show missing env value when its not missing runtime', async () => { | ||
await checkMissing('/also-not-missing', 'I_SHOULD_BE_HERE') | ||
}) | ||
|
||
it('should show missing env value when its missing normal', async () => { | ||
await checkMissing('/missing', 'hi', true) | ||
}) | ||
|
||
it('should show missing env value when its missing GSP', async () => { | ||
await checkMissing('/missing-gsp', 'SECRET', true) | ||
}) | ||
|
||
it('should show missing env value when its missing GSSP', async () => { | ||
await checkMissing('/missing-gssp', 'SECRET', true) | ||
}) | ||
|
||
it('should show missing env value when its missing API', async () => { | ||
await checkMissing('/api/hi', 'where_is_it', true) | ||
}) | ||
}) | ||
|
||
describe('client side', () => { | ||
it('should not show missing env value when its not missing public', async () => { | ||
await checkMissingClient('/not-missing', 'NEXT_PUBLIC_HI') | ||
}) | ||
|
||
it('should show missing env value when its missing runtime', async () => { | ||
await checkMissingClient('/also-not-missing', 'I_SHOULD_BE_HERE', true) | ||
}) | ||
|
||
it('should show missing env value when its missing normal', async () => { | ||
await checkMissingClient('/missing', 'hi', true) | ||
}) | ||
}) | ||
}) |