-
Notifications
You must be signed in to change notification settings - Fork 766
/
validate-testing.ts
188 lines (161 loc) · 6.47 KB
/
validate-testing.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
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
import { buildError, isOutputTargetDist, isOutputTargetWww, isString } from '@utils';
import { basename, dirname, isAbsolute, join } from 'path';
import type * as d from '../../declarations';
import { isLocalModule } from '../sys/resolve/resolve-utils';
export const validateTesting = (config: d.ValidatedConfig, diagnostics: d.Diagnostic[]) => {
const testing = (config.testing = Object.assign({}, config.testing || {}));
if (!config.flags.e2e && !config.flags.spec) {
return;
}
let configPathDir = config.configPath!;
if (isString(configPathDir)) {
if (basename(configPathDir).includes('.')) {
configPathDir = dirname(configPathDir);
}
} else {
configPathDir = config.rootDir!;
}
if (typeof config.flags.headless === 'boolean' || config.flags.headless === 'new') {
testing.browserHeadless = config.flags.headless;
} else if (typeof testing.browserHeadless !== 'boolean' && testing.browserHeadless !== 'new') {
testing.browserHeadless = true;
}
if (!testing.browserWaitUntil) {
testing.browserWaitUntil = 'load';
}
testing.browserArgs = testing.browserArgs || [];
addTestingConfigOption(testing.browserArgs, '--font-render-hinting=medium');
addTestingConfigOption(testing.browserArgs, '--incognito');
if (config.flags.ci) {
addTestingConfigOption(testing.browserArgs, '--no-sandbox');
addTestingConfigOption(testing.browserArgs, '--disable-setuid-sandbox');
addTestingConfigOption(testing.browserArgs, '--disable-dev-shm-usage');
testing.browserHeadless = testing.browserHeadless === 'new' ? 'new' : true;
}
if (typeof testing.rootDir === 'string') {
if (!isAbsolute(testing.rootDir)) {
testing.rootDir = join(config.rootDir!, testing.rootDir);
}
} else {
testing.rootDir = config.rootDir;
}
if (typeof config.flags.screenshotConnector === 'string') {
testing.screenshotConnector = config.flags.screenshotConnector;
}
if (typeof testing.screenshotConnector === 'string') {
if (!isAbsolute(testing.screenshotConnector)) {
testing.screenshotConnector = join(config.rootDir!, testing.screenshotConnector);
}
} else {
testing.screenshotConnector = join(
config.sys!.getCompilerExecutingPath(),
'..',
'..',
'screenshot',
'local-connector.js'
);
}
if (!Array.isArray(testing.testPathIgnorePatterns)) {
testing.testPathIgnorePatterns = DEFAULT_IGNORE_PATTERNS.map((ignorePattern) => {
return join(testing.rootDir!, ignorePattern);
});
(config.outputTargets ?? [])
.filter(
(o): o is d.OutputTargetWww | d.OutputTargetDist => (isOutputTargetDist(o) || isOutputTargetWww(o)) && !!o.dir
)
.forEach((outputTarget) => {
testing.testPathIgnorePatterns?.push(outputTarget.dir!);
});
}
if (typeof testing.preset !== 'string') {
testing.preset = join(config.sys!.getCompilerExecutingPath(), '..', '..', 'testing');
} else if (!isAbsolute(testing.preset)) {
testing.preset = join(configPathDir, testing.preset);
}
if (!Array.isArray(testing.setupFilesAfterEnv)) {
testing.setupFilesAfterEnv = [];
}
testing.setupFilesAfterEnv.unshift(
join(config.sys!.getCompilerExecutingPath(), '..', '..', 'testing', 'jest-setuptestframework.js')
);
if (isString(testing.testEnvironment)) {
if (!isAbsolute(testing.testEnvironment) && isLocalModule(testing.testEnvironment)) {
testing.testEnvironment = join(configPathDir, testing.testEnvironment);
}
}
if (typeof testing.allowableMismatchedPixels === 'number') {
if (testing.allowableMismatchedPixels < 0) {
const err = buildError(diagnostics);
err.messageText = `allowableMismatchedPixels must be a value that is 0 or greater`;
}
} else {
testing.allowableMismatchedPixels = DEFAULT_ALLOWABLE_MISMATCHED_PIXELS;
}
if (typeof testing.allowableMismatchedRatio === 'number') {
if (testing.allowableMismatchedRatio < 0 || testing.allowableMismatchedRatio > 1) {
const err = buildError(diagnostics);
err.messageText = `allowableMismatchedRatio must be a value ranging from 0 to 1`;
}
}
if (typeof testing.pixelmatchThreshold === 'number') {
if (testing.pixelmatchThreshold < 0 || testing.pixelmatchThreshold > 1) {
const err = buildError(diagnostics);
err.messageText = `pixelmatchThreshold must be a value ranging from 0 to 1`;
}
} else {
testing.pixelmatchThreshold = DEFAULT_PIXEL_MATCH_THRESHOLD;
}
if (testing.testRegex === undefined) {
/**
* The test regex covers cases of:
* - files under a `__tests__` directory
* - the case where a test file has a name such as `test.ts`, `spec.ts` or `e2e.ts`.
* - these files can use any of the following file extensions: .ts, .tsx, .js, .jsx.
* - this regex only handles the entire path of a file, e.g. `/some/path/e2e.ts`
* - the case where a test file ends with `.test.ts`, `.spec.ts`, or `.e2e.ts`.
* - these files can use any of the following file extensions: .ts, .tsx, .js, .jsx.
* - this regex case shall match file names such as `my-cmp.spec.ts`, `test.spec.ts`
* - this regex case shall not match file names such as `attest.ts`, `bespec.ts`
*/
testing.testRegex = '(/__tests__/.*|(\\.|/)(test|spec|e2e))\\.[jt]sx?$';
}
if (Array.isArray(testing.testMatch)) {
delete testing.testRegex;
} else if (typeof testing.testRegex === 'string') {
delete testing.testMatch;
}
if (typeof testing.runner !== 'string') {
testing.runner = join(config.sys!.getCompilerExecutingPath(), '..', '..', 'testing', 'jest-runner.js');
}
if (typeof testing.waitBeforeScreenshot === 'number') {
if (testing.waitBeforeScreenshot < 0) {
const err = buildError(diagnostics);
err.messageText = `waitBeforeScreenshot must be a value that is 0 or greater`;
}
} else {
testing.waitBeforeScreenshot = 10;
}
if (!Array.isArray(testing.emulate) || testing.emulate.length === 0) {
testing.emulate = [
{
userAgent: 'default',
viewport: {
width: 600,
height: 600,
deviceScaleFactor: 1,
isMobile: false,
hasTouch: false,
isLandscape: false,
},
},
];
}
};
const addTestingConfigOption = (setArray: string[], option: string) => {
if (!setArray.includes(option)) {
setArray.push(option);
}
};
const DEFAULT_ALLOWABLE_MISMATCHED_PIXELS = 100;
const DEFAULT_PIXEL_MATCH_THRESHOLD = 0.1;
const DEFAULT_IGNORE_PATTERNS = ['.vscode', '.stencil', 'node_modules'];