Skip to content

Commit 758d026

Browse files
committed
test(env): fix env tests
1 parent c8e327b commit 758d026

File tree

1 file changed

+55
-31
lines changed

1 file changed

+55
-31
lines changed

__tests__/config/utils/env.test.ts

+55-31
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ describe('readLocalEnvFile', () => {
4040
let backupSystemEnv = {};
4141

4242
const baseFlags = {
43-
cwd: '/tmp/project',
43+
cwd: normalize('/tmp/project'),
4444
env: undefined,
4545
loadSystemEnv: false,
4646
};
4747

48-
beforeEach(() => {
48+
beforeAll(() => {
4949
mockFs({
5050
'/tmp/project': {
5151
'.env': stripIndent`
@@ -63,29 +63,37 @@ describe('readLocalEnvFile', () => {
6363
},
6464
'/tmp/project-two': {
6565
'.env': stripIndent`
66-
ACCOUNT_SID=ACyyyyyyy
67-
AUTH_TOKEN=123456789a
68-
TWILIO=https://www.twilio.com
66+
ACCOUNT_SID=ACyyyyyyyyy
67+
AUTH_TOKEN=a987654321
68+
MY_PHONE_NUMBER=+99999
69+
SECRET_API_KEY=ahoy
6970
`,
7071
'.env.prod': stripIndent`
7172
ACCOUNT_SID=
7273
AUTH_TOKEN=
73-
TWILIO=https://www.twilio.com
74+
MY_PHONE_NUMBER=+444444444
75+
SECRET_API_KEY=
7476
`,
7577
},
7678
});
79+
});
80+
81+
beforeEach(() => {
7782
backupSystemEnv = { ...process.env };
7883
});
7984

80-
afterEach(() => {
85+
afterAll(() => {
8186
mockFs.restore();
87+
});
88+
89+
afterEach(() => {
8290
process.env = { ...backupSystemEnv };
8391
});
8492

8593
it('should throw an error if you use --load-system-env without --env', async () => {
8694
const errorMessage = stripIndent`
8795
If you are using --load-system-env you'll also have to supply a --env flag.
88-
96+
8997
The .env file you are pointing at will be used to primarily load environment variables.
9098
Any empty entries in the .env file will fall back to the system's environment variables.
9199
`;
@@ -95,8 +103,21 @@ describe('readLocalEnvFile', () => {
95103
).rejects.toEqual(new Error(errorMessage));
96104
});
97105

106+
it('should throw an error if the specified file does not exist', async () => {
107+
expect(
108+
readLocalEnvFile({ ...baseFlags, env: '/tmp/invalid-project/.env' })
109+
).rejects.toEqual(
110+
new Error(
111+
`Failed to find .env file at "${path.resolve(
112+
'/tmp/invalid-project/.env'
113+
)}"`
114+
)
115+
);
116+
});
117+
98118
it('should load the default env variables', async () => {
99-
expect(await readLocalEnvFile(baseFlags)).toEqual({
119+
const result = await readLocalEnvFile(baseFlags);
120+
expect(result).toEqual({
100121
localEnv: {
101122
ACCOUNT_SID: 'ACxxxxxxx',
102123
AUTH_TOKEN: '123456789f',
@@ -108,7 +129,8 @@ describe('readLocalEnvFile', () => {
108129
});
109130

110131
it('should load env variables from a different filename', async () => {
111-
expect(await readLocalEnvFile({ ...baseFlags, env: '.env.prod' })).toEqual({
132+
const result = await readLocalEnvFile({ ...baseFlags, env: '.env.prod' });
133+
expect(result).toEqual({
112134
localEnv: {
113135
ACCOUNT_SID: '',
114136
AUTH_TOKEN: '',
@@ -120,30 +142,33 @@ describe('readLocalEnvFile', () => {
120142
});
121143

122144
it('should load the default env variables with different cwd', async () => {
123-
expect(
124-
await readLocalEnvFile({ ...baseFlags, cwd: '/tmp/project-two' })
125-
).toEqual({
145+
const result = await readLocalEnvFile({
146+
...baseFlags,
147+
cwd: normalize('/tmp/project-two'),
148+
});
149+
expect(result).toEqual({
126150
localEnv: {
127-
ACCOUNT_SID: 'ACyyyyyyy',
128-
AUTH_TOKEN: '123456789a',
129-
TWILIO: 'https://www.twilio.com',
151+
ACCOUNT_SID: 'ACyyyyyyyyy',
152+
AUTH_TOKEN: 'a987654321',
153+
MY_PHONE_NUMBER: '+99999',
154+
SECRET_API_KEY: 'ahoy',
130155
},
131156
envPath: normalize('/tmp/project-two/.env'),
132157
});
133158
});
134159

135160
it('should load env variables from a different filename & cwd', async () => {
136-
expect(
137-
await readLocalEnvFile({
138-
...baseFlags,
139-
cwd: '/tmp/project-two',
140-
env: '.env.prod',
141-
})
142-
).toEqual({
161+
const result = await readLocalEnvFile({
162+
...baseFlags,
163+
cwd: normalize('/tmp/project-two'),
164+
env: '.env.prod',
165+
});
166+
expect(result).toEqual({
143167
localEnv: {
144168
ACCOUNT_SID: '',
145169
AUTH_TOKEN: '',
146-
TWILIO: 'https://www.twilio.com',
170+
MY_PHONE_NUMBER: '+444444444',
171+
SECRET_API_KEY: '',
147172
},
148173
envPath: normalize('/tmp/project-two/.env.prod'),
149174
});
@@ -156,13 +181,12 @@ describe('readLocalEnvFile', () => {
156181
SECRET_API_KEY: 'psst',
157182
};
158183

159-
expect(
160-
await readLocalEnvFile({
161-
...baseFlags,
162-
env: '.env.prod',
163-
loadSystemEnv: true,
164-
})
165-
).toEqual({
184+
const result = await readLocalEnvFile({
185+
...baseFlags,
186+
env: '.env.prod',
187+
loadSystemEnv: true,
188+
});
189+
expect(result).toEqual({
166190
localEnv: {
167191
ACCOUNT_SID: 'ACzzzzzzz',
168192
AUTH_TOKEN: '',

0 commit comments

Comments
 (0)