-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.ts
More file actions
104 lines (83 loc) · 2.35 KB
/
Config.ts
File metadata and controls
104 lines (83 loc) · 2.35 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
import { TestFileLocator } from "./TestFileLocator";
import { TestFileParser } from "./TestFileParser";
import { Reporter } from "./Reporter";
import { Runner } from "./Runner";
import { Settings } from "./Settings";
import { Plugin } from "./Plugin";
export class Config {
public locator?: TestFileLocator;
public parser?: TestFileParser;
public runner?: Runner;
public reporters: Array<Reporter> = [];
public settings: Settings = {
randomizeTests: false,
testTimeout: 5000,
};
public globals: Record<string, unknown> = {};
public plugins: Array<Plugin> = [];
constructor(public readonly path: string) {}
withLocator(locator: TestFileLocator): this {
this.locator = locator;
return this;
}
withParser(parser: TestFileParser): this {
this.parser = parser;
return this;
}
withRunner(runner: Runner): this {
this.runner = runner;
return this;
}
withReporter(reporter: Reporter): this {
this.reporters?.push(reporter);
return this;
}
withReporters(reporters: Array<Reporter>): this {
this.reporters = reporters;
return this;
}
withSetting<K extends keyof Settings>(key: K, value: Settings[K]): this {
this.settings[key] = value;
return this;
}
withSettings(settings: Settings): this {
this.settings = Object.assign({}, this.settings, settings);
return this;
}
withPlugin(pluginClass: new (config: this) => Plugin): this {
this.plugins.push(new pluginClass(this));
return this;
}
static async load(configFilePath: string): Promise<ValidConfig> {
const configFile: Config = await import(configFilePath);
if (!Config.isValidConfig(configFile)) {
const missing = [];
if (!configFile.path) {
missing.push("path");
}
if (!configFile.locator) {
missing.push("locator");
}
if (!configFile.parser) {
missing.push("parser");
}
if (!configFile.runner) {
missing.push("runner");
}
throw new Error(`Invalid config file. Missing: ${missing.join(", ")}`);
}
return configFile;
}
static isValidConfig(configFile: Config): configFile is ValidConfig {
if (
!configFile.path ||
!configFile.locator ||
!configFile.parser ||
!configFile.runner
) {
return false;
}
return true;
}
}
export type ValidConfig = Required<Config>;