-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathenv.test.ts
More file actions
134 lines (103 loc) · 3.73 KB
/
env.test.ts
File metadata and controls
134 lines (103 loc) · 3.73 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { confirm } from '@inquirer/prompts';
import { fs, vol } from 'memfs';
import { Mock, afterEach, expect, test, vi } from 'vitest';
import exec from '../../util/exec';
import { addEnv } from '../env';
vi.mock('../../util/print', () => ({ default: vi.fn() }));
vi.mock('@inquirer/prompts', () => ({ confirm: vi.fn() }));
vi.mock('../../util/exec');
const baseFiles = {
'package.json': JSON.stringify({
scripts: {},
dependencies: {},
devDependencies: {},
}),
'yarn.lock': '',
};
afterEach(() => {
vol.reset();
vi.clearAllMocks();
});
test('copies all template files to correct destinations', async () => {
(confirm as Mock).mockResolvedValueOnce(true);
vol.fromJSON(baseFiles, './');
await addEnv();
expect(fs.existsSync('.env.example')).toBe(true);
expect(fs.existsSync('.env')).toBe(true);
expect(fs.existsSync('.env.test')).toBe(true);
expect(fs.existsSync('jest.setup.env.js')).toBe(true);
expect(fs.existsSync('src/config/index.ts')).toBe(true);
});
test('creates .env from .env.example when .env does not exist', async () => {
(confirm as Mock).mockResolvedValueOnce(true);
vol.fromJSON(baseFiles, './');
await addEnv();
const envContent = fs.readFileSync('.env', 'utf8');
const envExampleContent = fs.readFileSync('.env.example', 'utf8');
expect(envContent).toBe(envExampleContent);
});
test('does not overwrite .env when it already exists', async () => {
(confirm as Mock).mockResolvedValueOnce(true);
vol.fromJSON({ ...baseFiles, '.env': 'EXISTING_VAR=value' }, './');
await addEnv();
const envContent = fs.readFileSync('.env', 'utf8');
expect(envContent).toBe('EXISTING_VAR=value');
});
test('adds .env to .gitignore', async () => {
(confirm as Mock).mockResolvedValueOnce(true);
vol.fromJSON({ ...baseFiles, '.gitignore': 'node_modules\n' }, './');
await addEnv();
const gitignore = fs.readFileSync('.gitignore', 'utf8');
expect(gitignore).toMatch('.env');
});
test('installs dotenv as a dev dependency', async () => {
(confirm as Mock).mockResolvedValueOnce(true);
vol.fromJSON(baseFiles, './');
await addEnv();
expect(exec).toHaveBeenCalledWith('yarn add --dev dotenv');
});
test('patches API file when it contains hardcoded URL', async () => {
(confirm as Mock).mockResolvedValueOnce(true);
vol.fromJSON(
{
...baseFiles,
'src/util/api/api.ts': `const url = 'https://api.github.com/orgs/thoughtbot/repos';`,
},
'./',
);
await addEnv();
const apiContent = fs.readFileSync('src/util/api/api.ts', 'utf8');
expect(apiContent).toMatch('EXPO_PUBLIC_API_BASE_URL');
expect(apiContent).not.toMatch(
"'https://api.github.com/orgs/thoughtbot/repos'",
);
});
test('does not error when API file does not exist', async () => {
(confirm as Mock).mockResolvedValueOnce(true);
vol.fromJSON(baseFiles, './');
await expect(addEnv()).resolves.toBeUndefined();
});
test('patches Jest config when it contains setupFilesAfterEnv', async () => {
(confirm as Mock).mockResolvedValueOnce(true);
vol.fromJSON(
{
...baseFiles,
'jest.config.js': `module.exports = {\n setupFilesAfterEnv: [\n './jest.setup.js'\n ]\n};`,
},
'./',
);
await addEnv();
const jestConfig = fs.readFileSync('jest.config.js', 'utf8');
expect(jestConfig).toMatch("setupFiles: ['./jest.setup.env.js']");
expect(jestConfig).toMatch('setupFilesAfterEnv');
});
test('does not error when Jest config does not exist', async () => {
(confirm as Mock).mockResolvedValueOnce(true);
vol.fromJSON(baseFiles, './');
await expect(addEnv()).resolves.toBeUndefined();
});
test('skips confirmation prompt in non-interactive mode', async () => {
vol.fromJSON(baseFiles, './');
await addEnv({ interactive: false });
expect(confirm).not.toHaveBeenCalled();
});