-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup_tests.test.ts
146 lines (121 loc) · 3.57 KB
/
setup_tests.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
// Copyright 2023-2023 the Nifty li'l' tricks authors. All rights reserved. MIT license.
import { assertEquals, assertInstanceOf } from "std/testing/asserts.ts";
import { beforeEach, describe, it } from "std/testing/bdd.ts";
import { setupTestsFactory } from "./setup_tests.ts";
import { Plugin, SetupTestsFn } from "./type.ts";
describe("setupTestsFactory", () => {
let setupTests: SetupTestsFn<Plugins>;
beforeEach(() => {
teardownCalls = [];
const { setupTests: setupTestsFn } = setupTestsFactory({
plugin1,
plugin2,
});
setupTests = setupTestsFn;
});
describe("setupTests", () => {
it("should run activated plugins and return the results", async () => {
// Arrange
const plugin1Config: Plugin1Config = { data1: ["1"] };
// Act
const { teardownTests, outputs } = await setupTests({
plugin1: plugin1Config,
});
// Assert
assertInstanceOf(teardownTests, Function);
assertEquals(outputs, {
plugin1: {
output: { plugin1Result: plugin1Config },
teardown: teardown1,
},
});
});
it("should run plugins and return the results when all plugins are activated", async () => {
// Arrange
const plugin1Config: Plugin1Config = { data1: ["1"] };
const plugin2Config: Plugin2Config = { data2: [2] };
// Act
const { teardownTests, outputs } = await setupTests({
plugin1: plugin1Config,
plugin2: plugin2Config,
});
// Assert
assertInstanceOf(teardownTests, Function);
assertEquals(outputs, {
plugin1: {
output: { plugin1Result: plugin1Config },
teardown: teardown1,
},
plugin2: {
output: { plugin2Result: plugin2Config },
teardown: teardown2,
},
});
});
});
describe("teardown", () => {
it("should run the teardown function of activated plugins", async () => {
// Arrange
const plugin1Config: Plugin1Config = { data1: ["1"] };
const plugin2Config: Plugin2Config = { data2: [2] };
const { teardownTests } = await setupTests({
plugin1: plugin1Config,
plugin2: plugin2Config,
});
// Act
await teardownTests();
// Assert
assertEquals(teardownCalls, ["plugin2.teardown", "plugin1.teardown"]);
});
it("should run the teardown function of all plugins when all plugins are activated", async () => {
// Arrange
const plugin1Config: Plugin1Config = { data1: ["1"] };
const { teardownTests } = await setupTests({
plugin1: plugin1Config,
});
// Act
await teardownTests();
// Assert
assertEquals(teardownCalls, ["plugin1.teardown"]);
});
});
});
type Plugins = {
plugin1: Plugin<Plugin1Config, Plugin1Result>;
plugin2: Plugin<Plugin2Config, Plugin2Result>;
};
interface Plugin1Config {
data1: string[];
}
interface Plugin1Result {
plugin1Result: Plugin1Config;
}
interface Plugin2Config {
data2: number[];
}
interface Plugin2Result {
plugin2Result: Plugin2Config;
}
let teardownCalls: string[] = [];
function teardown1() {
teardownCalls.push("plugin1.teardown");
}
const plugin1: Plugin<Plugin1Config, Plugin1Result> = {
setup(config: Plugin1Config) {
return {
output: { plugin1Result: config },
teardown: teardown1,
};
},
};
function teardown2() {
teardownCalls.push("plugin2.teardown");
}
const plugin2: Plugin<Plugin2Config, Plugin2Result> = {
setup(config: Plugin2Config) {
return {
output: { plugin2Result: config },
teardown: teardown2,
};
},
};