/
index.test.js
76 lines (61 loc) · 2.43 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
jest.mock('fs');
const mockWriteFile = jest.fn(() => Promise.resolve());
jest.mock('util', () => ({
promisify: () => mockWriteFile,
}));
const onPreBuild = require('./index').onPreBuild;
describe('onPreBuild', () => {
const OLD_ENV = process.env;
beforeEach(() => {
mockWriteFile.mockReset();
jest.resetModules();
process.env = { ...OLD_ENV };
});
afterEach(() => {
process.env = OLD_ENV;
});
describe('with context prefix ENV overrides', () => {
it('writes an env file with updated values', async () => {
process.env.DATABASE_URL = 'https://dev.com';
process.env.STAGING_DATABASE_URL = 'https://stage.com';
process.env.CONTEXT = 'staging';
await onPreBuild({ inputs: { mode: 'prefix' } });
expect(mockWriteFile).toHaveBeenCalledWith('.env', 'DATABASE_URL=https://stage.com\n');
});
});
describe('with suffix context prefix ENV overrides', () => {
it('writes an env file with updated values', async () => {
process.env.DATABASE_URL = 'https://dev.com';
process.env.DATABASE_URL_STAGING = 'https://stage.com';
process.env.CONTEXT = 'staging';
await onPreBuild({ inputs: { mode: 'suffix' } });
expect(mockWriteFile).toHaveBeenCalledWith('.env', 'DATABASE_URL=https://stage.com\n');
});
});
describe('with branch ENV overrides', () => {
it('writes an env file with updated values', async () => {
process.env.DATABASE_URL = 'https://dev.com';
process.env.HELLO_DATABASE_URL = 'https://stage.com';
process.env.BRANCH = 'hello';
await onPreBuild({ inputs: { mode: 'prefix' } });
expect(mockWriteFile).toHaveBeenCalledWith('.env', 'DATABASE_URL=https://stage.com\n');
});
});
describe('with suffix branch ENV overrides', () => {
it('writes an env file with updated values', async () => {
process.env.DATABASE_URL = 'https://dev.com';
process.env.DATABASE_URL_HELLO = 'https://stage.com';
process.env.BRANCH = 'hello';
await onPreBuild({ inputs: { mode: 'suffix' } });
expect(mockWriteFile).toHaveBeenCalledWith('.env', 'DATABASE_URL=https://stage.com\n');
});
});
describe('without ENV overrides', () => {
it('writes an env file with updated values', async () => {
process.env.DATABASE_URL = 'https://dev.com';
process.env.BRANCH = 'hello';
await onPreBuild({ inputs: { mode: 'prefix' } });
expect(mockWriteFile).not.toHaveBeenCalled();
});
});
});