-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestBuilder.ts
More file actions
121 lines (99 loc) · 4.01 KB
/
testBuilder.ts
File metadata and controls
121 lines (99 loc) · 4.01 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
import type { IPlatform } from "./platform";
import type { ProjectManager } from "./projectManager";
import * as ServerUtil from "./serverUtil";
import * as TestConfig from "./testConfig";
export type DoneCallback = (error?: unknown) => void;
export interface ITestBuilderContextDefintion {
(description: string, spec: () => void, scenarioPath?: string): void;
only(description: string, spec: () => void, scenarioPath?: string): void;
skip(description: string, spec: () => void, scenarioPath?: string): void;
}
export interface ITestBuilderTestDefinition {
(expectation: string, isCoreTest: boolean, assertion: (done: DoneCallback) => void): void;
only(expectation: string, isCoreTest: boolean, assertion: (done: DoneCallback) => void): void;
skip(expectation: string, isCoreTest: boolean, assertion: (done: DoneCallback) => void): void;
}
type DescribeLike = (description: string, spec: () => void) => unknown;
type ItLike = (expectation: string, assertion: (this: Mocha.Context, done: DoneCallback) => void) => unknown;
export class TestBuilder {
public static readonly describe = getDescribe();
public static readonly it = getIt();
}
export class TestContext {
public static projectManager: ProjectManager | undefined;
public static targetPlatform: IPlatform | undefined;
}
function describeInternal(
describeFunction: DescribeLike,
description: string,
spec: () => void,
scenarioPath?: string,
): unknown {
if (!TestContext.projectManager || !TestContext.targetPlatform) {
throw new Error(
"TestContext.projectManager or TestContext.targetPlatform are not defined. Call TestBuilder.describe only from initializeTests.",
);
}
return describeFunction(description, () => {
afterEach(() => {
console.log("Cleaning up!");
ServerUtil.resetServerState();
});
beforeEach(async () => {
await TestContext.targetPlatform?.getEmulatorManager().prepareEmulatorForTest(TestConfig.TestNamespace).catch(
() => undefined,
);
});
if (scenarioPath) {
before(async () => {
await TestContext.projectManager?.setupScenario(
TestConfig.testRunDirectory,
TestConfig.TestNamespace,
TestConfig.templatePath,
scenarioPath,
TestContext.targetPlatform as IPlatform,
);
});
}
spec();
});
}
function getDescribe(): ITestBuilderContextDefintion {
const describer = ((description: string, spec: () => void, scenarioPath?: string) => {
describeInternal(describe as DescribeLike, description, spec, scenarioPath);
}) as ITestBuilderContextDefintion;
describer.only = (description: string, spec: () => void, scenarioPath?: string) => {
describeInternal(describe.only as DescribeLike, description, spec, scenarioPath);
};
describer.skip = (description: string, spec: () => void, scenarioPath?: string) => {
describeInternal(describe.skip as DescribeLike, description, spec, scenarioPath);
};
return describer;
}
function itInternal(
itFunction: ItLike,
expectation: string,
isCoreTest: boolean,
assertion: (done: DoneCallback) => void,
): unknown {
if (TestConfig.onlyRunCoreTests && !isCoreTest) {
return undefined;
}
const assertionWithTimeout = function (this: Mocha.Context, done: DoneCallback): void {
this.timeout(10 * 2 * 60 * 1000);
assertion(done);
};
return itFunction(expectation, assertionWithTimeout);
}
function getIt(): ITestBuilderTestDefinition {
const testFunction = ((expectation: string, isCoreTest: boolean, assertion: (done: DoneCallback) => void) => {
itInternal(it as ItLike, expectation, isCoreTest, assertion);
}) as ITestBuilderTestDefinition;
testFunction.only = (expectation: string, isCoreTest: boolean, assertion: (done: DoneCallback) => void) => {
itInternal(it.only as ItLike, expectation, isCoreTest, assertion);
};
testFunction.skip = (expectation: string, isCoreTest: boolean, assertion: (done: DoneCallback) => void) => {
itInternal(it.skip as ItLike, expectation, isCoreTest, assertion);
};
return testFunction;
}