Skip to content

Commit 5126730

Browse files
committed
got multi env tests to pass
1 parent 46269ff commit 5126730

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

__tests__/index.test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const {transformFileSync} = require('@babel/core')
22

33
const FIXTURES = '__tests__/__fixtures__/'
4-
const env = Object.apply({}, process.env)
54

65
describe('react-native-dotenv', () => {
6+
const OLD_ENV = process.env
77
afterEach(() => {
8-
process.env = Object.apply({}, env)
8+
jest.resetModules()
9+
process.env = { ...OLD_ENV }
10+
911
})
1012

1113
it('should throw if the variable does not exist', () => {
@@ -50,6 +52,11 @@ describe('react-native-dotenv', () => {
5052
expect(code).toBe('console.log("abc123456");\nconsole.log("username123456");')
5153
})
5254

55+
it('should load multiple env files', () => {
56+
const {code} = transformFileSync(FIXTURES + 'multi-env/source.js')
57+
expect(code).toBe('console.log("abc123456");\nconsole.log("username123456");')
58+
})
59+
5360
it('should support `as alias` import syntax', () => {
5461
const {code} = transformFileSync(FIXTURES + 'as-alias/source.js')
5562
expect(code).toBe('const a = "abc123";\nconst b = "username";')

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ module.exports = ({types: t}) => ({
3232
...this.opts
3333
}
3434

35-
const babelMode = process.env.BABEL_ENV || 'development'
35+
const babelMode = process.env.BABEL_ENV || process.env.NODE_ENV || 'development'
3636
if (this.opts.safe) {
3737
const parsed = parseDotenvFile(this.opts.path, this.opts.verbose)
3838
const modeParsed = parseDotenvFile(this.opts.path + '.' + babelMode)
3939
this.env = Object.assign(parsed, modeParsed)
4040
} else {
41-
dotenv.config({
42-
path: this.opts.path
43-
})
4441
dotenv.config({
4542
path: this.opts.path + '.' + babelMode,
4643
silent: true
4744
})
45+
dotenv.config({
46+
path: this.opts.path
47+
})
4848
this.env = process.env
4949
}
5050
},

0 commit comments

Comments
 (0)