forked from jestjs/jest
/
args.test.ts
157 lines (132 loc) · 5.13 KB
/
args.test.ts
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
/**
* 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.
*
*/
import type {Config} from '@jest/types';
import {constants} from 'jest-config';
import {buildArgv} from '../../cli';
import {check} from '../../cli/args';
const argv = (input: Partial<Config.Argv>): Config.Argv => input as Config.Argv;
describe('check', () => {
it('returns true if the arguments are valid', () => {
expect(check(argv({}))).toBe(true);
});
it('raises an exception if runInBand and maxWorkers are both specified', () => {
expect(() => check(argv({maxWorkers: 2, runInBand: true}))).toThrow(
'Both --runInBand and --maxWorkers were specified',
);
});
it('raises an exception if onlyChanged and watchAll are both specified', () => {
expect(() => check(argv({onlyChanged: true, watchAll: true}))).toThrow(
'Both --onlyChanged and --watchAll were specified',
);
});
it('raises an exception if onlyFailures and watchAll are both specified', () => {
expect(() => check(argv({onlyFailures: true, watchAll: true}))).toThrow(
'Both --onlyFailures and --watchAll were specified',
);
});
it('raises an exception when lastCommit and watchAll are both specified', () => {
expect(() => check(argv({lastCommit: true, watchAll: true}))).toThrow(
'Both --lastCommit and --watchAll were specified',
);
});
it('raises an exception if findRelatedTests is specified with no file paths', () => {
expect(() =>
check(
argv({
_: [],
findRelatedTests: true,
}),
),
).toThrow(
'The --findRelatedTests option requires file paths to be specified',
);
});
it('raises an exception if maxWorkers is specified with no number', () => {
expect(() => check(argv({maxWorkers: undefined}))).toThrow(
'The --maxWorkers (-w) option requires a number or string to be specified',
);
});
it('allows maxWorkers to be a %', () => {
expect(() => check(argv({maxWorkers: '50%'}))).not.toThrow();
});
test.each(constants.JEST_CONFIG_EXT_ORDER.map(e => e.substring(1)))(
'allows using "%s" file for --config option',
ext => {
expect(() => check(argv({config: `jest.config.${ext}`}))).not.toThrow();
expect(() =>
check(argv({config: `../test/test/my_conf.${ext}`})),
).not.toThrow();
},
);
it('raises an exception if selectProjects is not provided any project names', () => {
expect(() => check(argv({selectProjects: []}))).toThrow(
'The --selectProjects option requires the name of at least one project to be specified.\n',
);
});
it('raises an exception if config is not a valid JSON string', () => {
expect(() => check(argv({config: 'x:1'}))).toThrow(
'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .ts, .mjs, .cjs, .json',
);
});
it('raises an exception if config is not a supported file type', () => {
const message =
'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .ts, .mjs, .cjs, .json';
expect(() => check(argv({config: 'jest.configjs'}))).toThrow(message);
expect(() => check(argv({config: 'jest.config.exe'}))).toThrow(message);
});
it('raises an exception if shard has wrong format', () => {
expect(() => check(argv({shard: 'mumblemuble'}))).toThrow(
/string in the format of <n>\/<m>/,
);
});
it('raises an exception if shard pair has to many items', () => {
expect(() => check(argv({shard: '1/2/2'}))).toThrow(
/string in the format of <n>\/<m>/,
);
});
it('raises an exception if shard has floating points', () => {
expect(() => check(argv({shard: '1.0/1'}))).toThrow(
/string in the format of <n>\/<m>/,
);
});
it('raises an exception if first item in shard pair is no number', () => {
expect(() => check(argv({shard: 'a/1'}))).toThrow(
/string in the format of <n>\/<m>/,
);
});
it('raises an exception if second item in shard pair is no number', () => {
expect(() => check(argv({shard: '1/a'}))).toThrow(
/string in the format of <n>\/<m>/,
);
});
it('raises an exception if shard is zero-indexed', () => {
expect(() => check(argv({shard: '0/1'}))).toThrow(
/requires 1-based values, received 0/,
);
});
it('raises an exception if shard index is larger than shard count', () => {
expect(() => check(argv({shard: '2/1'}))).toThrow(
/requires <n> to be lower or equal than <m>/,
);
});
it('allows valid shard format', () => {
expect(() => check(argv({shard: '1/2'}))).not.toThrow();
});
});
describe('buildArgv', () => {
it('should return only camelcased args ', async () => {
const mockProcessArgv = jest
// @ts-expect-error
.spyOn(process.argv, 'slice')
.mockImplementation(() => ['--clear-mocks']);
const actual = await buildArgv();
expect(actual).not.toHaveProperty('clear-mocks');
expect(actual).toHaveProperty('clearMocks', true);
mockProcessArgv.mockRestore();
});
});