Skip to content

Commit

Permalink
refactor(env): warn when using non-conventional NODE_ENV values (#2…
Browse files Browse the repository at this point in the history
…7111)

# Why

Fixes ENG-9686

# How

Instead of erroring, we now warn when using a non-conventional
`NODE_ENV` value.

# Test Plan

See added tests.

- `NODE_ENV=yolo expod start` should warn

# Checklist

<!--
Please check the appropriate items below if they apply to your diff.
This is required for changes to Expo modules.
-->

- [ ] Documentation is up to date to reflect these changes (eg:
https://docs.expo.dev and README.md).
- [ ] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
- [ ] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).
  • Loading branch information
byCedric committed Mar 6, 2024
1 parent 01b9c0a commit 8057542
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
2 changes: 2 additions & 0 deletions packages/@expo/env/CHANGELOG.md
Expand Up @@ -10,6 +10,8 @@

### 💡 Others

- Warn instead of error when `NODE_ENV` is set to non-conventional value. ([#27111](https://github.com/expo/expo/pull/27111) by [@byCedric](https://github.com/byCedric))

## 0.2.1 — 2023-12-15

_This version does not introduce any user-facing changes._
Expand Down
6 changes: 5 additions & 1 deletion packages/@expo/env/build/env.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/@expo/env/build/env.js.map

Large diffs are not rendered by default.

22 changes: 19 additions & 3 deletions packages/@expo/env/src/__tests__/env.test.ts
Expand Up @@ -67,10 +67,26 @@ describe(getFiles, () => {
expect.stringContaining('Proceeding without mode-specific .env')
);
});
it(`throws if NODE_ENV is not valid`, () => {
expect(() => getFiles('invalid')).toThrowErrorMatchingInlineSnapshot(
`"Environment variable "NODE_ENV=invalid" is invalid. Valid values are "development", "test", and "production"`
it(`warns if NODE_ENV is not valid`, () => {
const warnSpy = jest.spyOn(console, 'warn');

expect(() => getFiles('invalid')).not.toThrow();
expect(warnSpy).toBeCalledWith(
expect.stringContaining('"NODE_ENV=invalid" is non-conventional')
);
expect(warnSpy).toBeCalledWith(
expect.stringContaining('Use "development", "test", or "production"')
);

warnSpy.mockClear();
});
it(`does not warn if NODE_ENV is not valid when in silent mode`, () => {
const warnSpy = jest.spyOn(console, 'warn');

expect(() => getFiles('invalid', { silent: true })).not.toThrow();
expect(warnSpy).not.toBeCalled();

warnSpy.mockClear();
});
});

Expand Down
14 changes: 11 additions & 3 deletions packages/@expo/env/src/env.ts
Expand Up @@ -198,9 +198,17 @@ export function getFiles(
}

if (mode && !['development', 'test', 'production'].includes(mode)) {
throw new Error(
`Environment variable "NODE_ENV=${mode}" is invalid. Valid values are "development", "test", and "production`
);
if (silent) {
debug(
`NODE_ENV="${mode}" is non-conventional and might cause development code to run in production. Use "development", "test", or "production" instead.`
);
} else {
console.warn(
chalk.yellow(
`"NODE_ENV=${mode}" is non-conventional and might cause development code to run in production. Use "development", "test", or "production" instead`
)
);
}
}

if (!mode) {
Expand Down

0 comments on commit 8057542

Please sign in to comment.