-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby): allow environment variables to be replaced per environme…
…nt (#10565) * chore: start work * test: add unit tests for webpack config functionality * docs(env-vars): tweak documentation for this functionality * chore: fix silly error * chore: fix typo * chore: switch to GATSBY_ACTIVE_ENV
- Loading branch information
Showing
3 changed files
with
129 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
jest.mock(`webpack`, () => { | ||
return { | ||
...jest.requireActual(`webpack`), | ||
DefinePlugin: jest.fn(), | ||
} | ||
}) | ||
jest.mock(`fs-extra`, () => { | ||
const { readFileSync, ...rest } = jest.requireActual(`fs-extra`) | ||
return { | ||
...rest, | ||
readFileSync: jest | ||
.fn() | ||
.mockImplementation((...args) => readFileSync(...args)), | ||
} | ||
}) | ||
const { DefinePlugin } = require(`webpack`) | ||
const { readFileSync } = require(`fs-extra`) | ||
const webpackConfig = require(`../webpack.config`) | ||
const { store } = require(`../../redux`) | ||
|
||
beforeEach(() => { | ||
DefinePlugin.mockClear() | ||
readFileSync.mockClear() | ||
store.dispatch({ | ||
type: `SET_PROGRAM`, | ||
payload: { | ||
extensions: [], | ||
browserslist: [], | ||
directory: process.cwd(), | ||
}, | ||
}) | ||
}) | ||
|
||
const getConfig = (args = {}) => | ||
webpackConfig( | ||
{ | ||
extensions: [`.js`], | ||
}, | ||
process.cwd(), | ||
`build-html` | ||
) | ||
|
||
describe(`basic functionality`, () => { | ||
it(`returns webpack config with expected shape`, async () => { | ||
const config = await getConfig() | ||
|
||
expect(config).toEqual( | ||
expect.objectContaining( | ||
[`context`, `entry`, `output`, `module`, `plugins`, `resolve`].reduce( | ||
(merged, key) => { | ||
merged[key] = expect.anything() | ||
return merged | ||
}, | ||
{} | ||
) | ||
) | ||
) | ||
}) | ||
}) | ||
|
||
describe(`environment variables`, () => { | ||
afterEach(() => { | ||
delete process.env.GATSBY_ACTIVE_ENV | ||
}) | ||
|
||
it(`sanitizes process.env variables`, async () => { | ||
await getConfig() | ||
|
||
expect(DefinePlugin).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
"process.env": `{}`, | ||
}) | ||
) | ||
}) | ||
|
||
it(`provides expected variables`, async () => { | ||
await getConfig() | ||
|
||
expect(DefinePlugin).toHaveBeenCalledWith( | ||
expect.objectContaining( | ||
[ | ||
`__PATH_PREFIX__`, | ||
`process.env.BUILD_STAGE`, | ||
`process.env.GATSBY_BUILD_STAGE`, | ||
`process.env.NODE_ENV`, | ||
`process.env.PUBLIC_DIR`, | ||
].reduce((merged, key) => { | ||
merged[key] = expect.anything() | ||
return merged | ||
}, {}) | ||
) | ||
) | ||
}) | ||
|
||
describe(`env var overriding`, () => { | ||
it(`allows for GATSBY_ACTIVE_ENV override`, async () => { | ||
process.env.GATSBY_ACTIVE_ENV = `staging` | ||
await getConfig() | ||
|
||
expect(readFileSync).toHaveBeenCalledWith( | ||
expect.stringContaining(`.env.staging`), | ||
expect.anything() | ||
) | ||
}) | ||
|
||
it(`falls back to NODE_ENV`, async () => { | ||
await getConfig() | ||
|
||
expect(readFileSync).toHaveBeenCalledWith( | ||
expect.stringContaining(`.env.${process.env.NODE_ENV}`), | ||
expect.anything() | ||
) | ||
}) | ||
}) | ||
}) |
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