-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup_tests.ts
More file actions
130 lines (120 loc) · 3.57 KB
/
Copy pathsetup_tests.ts
File metadata and controls
130 lines (120 loc) · 3.57 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
122
123
124
125
126
127
128
129
130
// Copyright 2023-2023 the Nifty li'l' tricks authors. All rights reserved. MIT license.
import {
SetupTestsConfig,
SetupTestsFactoryPlugins,
SetupTestsFactoryResult,
SetupTestsPlugins,
SetupTestsPluginTeardown,
SetupTestsResult,
SetupTestsTeardown,
} from "./type.ts";
/**
* Set up tests with defined plugins.
*
* Each plugin must be given a name that is not one of the
* reserved plugin names. This returns a function that when run,
* sets up the tests to use the loaded plugins according to the provided
* config.
*
* Each plugin must contain the following functions in order to be correctly loaded:
* - `setup`
* - `teardown`
*
* ```typescript
* import { setupTestsFactory } from "https://deno.land/x/nifty_lil_tricks_testing/mod.ts";
*
* const helloWorldPlugin = {
* setup: (config: { message: string }) => {
* // Setup plugin according to config
* return {
* output: config,
* teardown: () => {}
* }
* },
* }
*
* export const { setupTests } = setupTestsFactory({
* helloWorld: helloWorldPlugin,
* });
* ```
*/
export function setupTestsFactory<Plugins extends SetupTestsPlugins>(
plugins: SetupTestsFactoryPlugins<Plugins>,
): SetupTestsFactoryResult<Plugins> {
const service = new SetupTestsService(plugins);
return {
setupTests: service.setupTests.bind(service),
};
}
class SetupTestsService<Plugins extends SetupTestsPlugins>
implements SetupTestsFactoryResult<Plugins> {
#plugins: SetupTestsFactoryPlugins<Plugins>;
constructor(plugins: SetupTestsFactoryPlugins<Plugins>) {
this.#plugins = plugins;
}
#buildTeardown(teardowns: SetupTestsPluginTeardown[]): SetupTestsTeardown {
return async () => {
for (const teardown of teardowns) {
await teardown();
}
};
}
async setupTests<Config extends SetupTestsConfig<Plugins>>(
config: Config,
): Promise<SetupTestsResult<Plugins, Config>> {
let outputs = {} as SetupTestsResult<Plugins, Config>["outputs"];
const teardowns: SetupTestsPluginTeardown[] = [];
const { ...pluginsConfig } = config;
for (const [pluginName, pluginConfig] of Object.entries(pluginsConfig)) {
// TODO: add in when generic config is added
// assertAllowedPluginName(pluginName);
const plugin = this.#plugins[pluginName];
const { teardown, output } = await plugin.setup(pluginConfig);
const pluginOutput = {
output,
teardown,
} as Awaited<ReturnType<Plugins[keyof Plugins]["setup"]>>;
outputs = {
...outputs,
[pluginName]: pluginOutput,
};
teardowns.push(
teardown,
);
}
return {
outputs,
teardownTests: this.#buildTeardown(teardowns.reverse()).bind(this),
};
}
}
/**
* Assert that the input is never and not unexpected.
*/
export function assertNever(_input: never, message: string): never {
throw new Error(message);
}
// TODO: add in when generic config is added
// function assertAllowedPluginName(pluginName: string): void {
// const bannedPluginNames: Record<keyof SetupTestsBaseConfig, string> = {};
// if (
// Object.keys(bannedPluginNames).includes(
// pluginName as keyof SetupTestsBaseConfig,
// )
// ) {
// throw new SetupTestsError(
// `'${pluginName}' is a reserved plugin name, please choose another name that is not one of ${
// Object.keys(bannedPluginNames).join("|")
// }`,
// );
// }
// }
// /**
// * SetupTestsError
// */
// export class SetupTestsError extends Error {
// /**
// * name of the error.
// */
// override name = "SetupTestsError";
// }