/
init.test.js
204 lines (149 loc) · 6.53 KB
/
init.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable no-eval */
import * as path from 'path';
import * as fs from 'graceful-fs';
import prompts from 'prompts';
import {constants} from 'jest-config';
import init from '../';
import {onNodeVersions} from '@jest/test-utils';
const {JEST_CONFIG_EXT_ORDER} = constants;
jest.mock('prompts');
jest.mock('../../../../jest-config/build/getCacheDirectory', () => () =>
'/tmp/jest',
);
jest.mock('path', () => ({...jest.requireActual('path'), sep: '/'}));
jest.mock('fs', () => ({
...jest.requireActual('fs'),
writeFileSync: jest.fn(),
}));
const resolveFromFixture = relativePath =>
path.resolve(__dirname, 'fixtures', relativePath);
const consoleLog = console.log;
describe('init', () => {
beforeEach(() => {
console.log = jest.fn();
});
afterEach(() => {
jest.clearAllMocks();
console.log = consoleLog;
});
describe('project with package.json and no jest config', () => {
describe('all questions answered with answer: "No"', () => {
it('should return the default configuration (an empty config)', async () => {
prompts.mockReturnValueOnce({});
await init(resolveFromFixture('only_package_json'));
const writtenJestConfig = fs.writeFileSync.mock.calls[0][1];
expect(writtenJestConfig).toMatchSnapshot();
const evaluatedConfig = eval(writtenJestConfig);
expect(evaluatedConfig).toEqual({});
});
onNodeVersions('^13.2.0', () => {
it('should generate empty config with mjs extension', async () => {
prompts.mockReturnValueOnce({});
await init(resolveFromFixture('type_module'));
const writtenJestConfigFilename = fs.writeFileSync.mock.calls[0][0];
const writtenJestConfig = fs.writeFileSync.mock.calls[0][1];
expect(writtenJestConfigFilename.endsWith('.mjs')).toBe(true);
expect(typeof writtenJestConfig).toBe('string');
expect(writtenJestConfig.split('\n')[3]).toBe('export default {');
});
});
});
describe('some questions answered with answer: "Yes"', () => {
it('should create configuration for {clearMocks: true}', async () => {
prompts.mockReturnValueOnce({clearMocks: true});
await init(resolveFromFixture('only_package_json'));
const writtenJestConfig = fs.writeFileSync.mock.calls[0][1];
const evaluatedConfig = eval(writtenJestConfig);
expect(evaluatedConfig).toEqual({clearMocks: true});
});
it('should create configuration for {coverage: true}', async () => {
prompts.mockReturnValueOnce({coverage: true});
await init(resolveFromFixture('only_package_json'));
const writtenJestConfig = fs.writeFileSync.mock.calls[0][1];
const evaluatedConfig = eval(writtenJestConfig);
expect(evaluatedConfig).toEqual({coverageDirectory: 'coverage'});
});
it('should create configuration for {environment: "jsdom"}', async () => {
prompts.mockReturnValueOnce({environment: 'jsdom'});
await init(resolveFromFixture('only_package_json'));
const writtenJestConfig = fs.writeFileSync.mock.calls[0][1];
const evaluatedConfig = eval(writtenJestConfig);
// should modify when the default environment will be changed to "node"
expect(evaluatedConfig).toEqual({});
});
it('should create configuration for {environment: "node"}', async () => {
prompts.mockReturnValueOnce({environment: 'node'});
await init(resolveFromFixture('only_package_json'));
const writtenJestConfig = fs.writeFileSync.mock.calls[0][1];
const evaluatedConfig = eval(writtenJestConfig);
// should modify when the default environment will be changed to "node"
expect(evaluatedConfig).toEqual({testEnvironment: 'node'});
});
it('should create package.json with configured test command when {scripts: true}', async () => {
prompts.mockReturnValueOnce({scripts: true});
await init(resolveFromFixture('only_package_json'));
const writtenPackageJson = fs.writeFileSync.mock.calls[0][1];
expect(writtenPackageJson).toMatchSnapshot();
expect(JSON.parse(writtenPackageJson).scripts.test).toEqual('jest');
});
});
});
describe('no package json', () => {
it('should throw an error if there is no package.json file', async () => {
expect.assertions(1);
try {
await init(resolveFromFixture('no_package_json'));
} catch (error) {
expect(error.message).toMatch(
'Could not find a "package.json" file in',
);
}
});
});
describe.each(JEST_CONFIG_EXT_ORDER.map(e => e.substring(1)))(
'has-jest-config-file-%s',
extension => {
describe('ask the user whether to override config or not', () => {
it('user answered with "Yes"', async () => {
prompts.mockReturnValueOnce({continue: true}).mockReturnValueOnce({});
await init(resolveFromFixture(`has_jest_config_file_${extension}`));
expect(prompts.mock.calls[0][0]).toMatchSnapshot();
const writtenJestConfig = fs.writeFileSync.mock.calls[0][1];
expect(writtenJestConfig).toBeDefined();
});
it('user answered with "No"', async () => {
prompts.mockReturnValueOnce({continue: false});
await init(resolveFromFixture(`has_jest_config_file_${extension}`));
// return after first prompt
expect(prompts).toHaveBeenCalledTimes(1);
});
});
},
);
describe('has jest config in package.json', () => {
it('should ask the user whether to override config or not', async () => {
prompts.mockReturnValueOnce({continue: true}).mockReturnValueOnce({});
await init(resolveFromFixture('has_jest_config_in_package_json'));
expect(prompts.mock.calls[0][0]).toMatchSnapshot();
const writtenJestConfig = fs.writeFileSync.mock.calls[0][1];
expect(writtenJestConfig).toBeDefined();
});
});
describe('already has "jest" in packageJson.scripts.test', () => {
it('should not ask "test script question"', async () => {
prompts.mockReturnValueOnce({});
await init(resolveFromFixture('test_script_configured'));
const questionsNames = prompts.mock.calls[0][0].map(
question => question.name,
);
expect(questionsNames).not.toContain('scripts');
});
});
});
/* eslint-enable */