Skip to content

Commit

Permalink
fix: Order of dotenv files loaded (#97)
Browse files Browse the repository at this point in the history
Syncing fix from here facebook/create-react-app#9037
  • Loading branch information
djdmbrwsk authored Sep 3, 2020
1 parent 968fad5 commit eb00be8
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions .releaserc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
{
"preset": "conventionalcommits",
"presetConfig": {
"header": "Changelog",
"types": [
{
"section": "Features",
Expand Down
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Changelog

## [2.0.0-beta.1](https://github.com/djdmbrwsk/dotenv-cra/compare/v1.0.0...v2.0.0-beta.1) (2020-09-03)


Expand All @@ -9,8 +11,6 @@

* Drop support for Node 8 ([#96](https://github.com/djdmbrwsk/dotenv-cra/issues/96)) ([b13d42d](https://github.com/djdmbrwsk/dotenv-cra/commit/b13d42de64bdc948b50b51ca9fa52e769958aa44))

# Changelog

## 1.0.0 (2019-12-29)

Initial release
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ PORT=3001

Files on the left have more priority than files on the right:

- `npm start`: `.env.development.local`, `.env.development`, `.env.local`, `.env`
- `npm start`: `.env.development.local`, `.env.local`, `.env.development`, `.env`
- `npm test`: `.env.test.local`, `.env.test`, `.env` (note `.env.local` is missing)

[CRA Reference](https://create-react-app.dev/docs/adding-custom-environment-variables/#what-other-env-files-can-be-used)
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { existsSync } from 'fs';
import { basename, resolve } from 'path';

import {
config as dotenvConfig,
DotenvConfigOptions,
DotenvConfigOutput,
config as dotenvConfig,
} from 'dotenv';
import dotenvExpand from 'dotenv-expand';

Expand All @@ -31,8 +31,8 @@ export function config(options?: DotenvCraOptions): DotenvConfigOutput {
// https://github.com/facebook/create-react-app/blob/8b7b819b4b9e6ba457e011e92e33266690e26957/packages/react-scripts/config/env.js#L25-L34
const dotenvFiles = [
`${dotenvPath}.${env}.local`,
`${dotenvPath}.${env}`,
process.env.NODE_ENV !== 'test' && `${dotenvPath}.local`,
`${dotenvPath}.${env}`,
dotenvPath,
];

Expand Down
14 changes: 7 additions & 7 deletions test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { basename } from 'path';

import dotenv, { DotenvConfigOutput } from 'dotenv';

import { config, DotenvCraOptions } from '../src/main';
import { DotenvCraOptions, config } from '../src/main';

const originalNodeEnv = process.env.NODE_ENV;
beforeEach(() => {
Expand Down Expand Up @@ -80,10 +80,10 @@ test('should log files loaded when `debug` supplied in options', () => {
'[dotenv-cra][DEBUG] loading `.env.development.local`',
);
expect(consoleLogSpy.mock.calls[1][0]).toEqual(
'[dotenv-cra][DEBUG] loading `.env.development`',
'[dotenv-cra][DEBUG] loading `.env.local`',
);
expect(consoleLogSpy.mock.calls[2][0]).toEqual(
'[dotenv-cra][DEBUG] loading `.env.local`',
'[dotenv-cra][DEBUG] loading `.env.development`',
);
expect(consoleLogSpy.mock.calls[3][0]).toEqual(
'[dotenv-cra][DEBUG] loading `.env`',
Expand All @@ -99,8 +99,8 @@ test('should exclude .env.local when NODE_ENV set to test', () => {
.mockImplementation(makeMockDotenvConfig());
config();
expect(dotenvConfigSpy).toBeCalledTimes(3);
let thirdCallPath = dotenvConfigSpy.mock.calls[2][0]?.path;
expect(thirdCallPath?.endsWith('.env.local')).toEqual(false);
let secondCallPath = dotenvConfigSpy.mock.calls[1][0]?.path;
expect(secondCallPath?.endsWith('.env.local')).toEqual(false);

process.env.NODE_ENV = 'development';
dotenvConfigSpy.mockRestore();
Expand All @@ -109,8 +109,8 @@ test('should exclude .env.local when NODE_ENV set to test', () => {
.mockImplementation(makeMockDotenvConfig());
config();
expect(dotenvConfigSpy).toBeCalledTimes(4);
thirdCallPath = dotenvConfigSpy.mock.calls[2][0]?.path;
expect(thirdCallPath?.endsWith('.env.local')).toEqual(true);
secondCallPath = dotenvConfigSpy.mock.calls[1][0]?.path;
expect(secondCallPath?.endsWith('.env.local')).toEqual(true);
});

test('should return error objects', () => {
Expand Down

0 comments on commit eb00be8

Please sign in to comment.