1 | import * as core from "@testdeck/core";
|
2 |
|
3 | function applyTimings(fn: any, settings: any): any {
|
4 | if (settings) {
|
5 | if (fn.length === 1) {
|
6 | return core.wrap(function(done) {
|
7 | if (settings.retries !== undefined) { this.retries(settings.retries); }
|
8 | if (settings.slow !== undefined) { this.slow(settings.slow); }
|
9 | if (settings.timeout !== undefined) { this.timeout(settings.timeout); }
|
10 | return fn.call(this, done);
|
11 | }, fn);
|
12 | } else {
|
13 | return core.wrap(function() {
|
14 | if (settings.retries !== undefined) { this.retries(settings.retries); }
|
15 | if (settings.slow !== undefined) { this.slow(settings.slow); }
|
16 | if (settings.timeout !== undefined) { this.timeout(settings.timeout); }
|
17 | return fn.call(this);
|
18 | }, fn);
|
19 | }
|
20 | } else {
|
21 | return fn;
|
22 | }
|
23 | }
|
24 |
|
25 | const mochaRunner: core.TestRunner = {
|
26 | suite(name: string, callback: () => void, settings?: core.SuiteSettings): void {
|
27 | switch (settings && settings.execution) {
|
28 | case "only":
|
29 | describe.only(name, applyTimings(callback, settings));
|
30 | break;
|
31 | case "skip":
|
32 | describe.skip(name, applyTimings(callback, settings));
|
33 | break;
|
34 | case "pending":
|
35 |
|
36 | describe.skip(name, applyTimings(callback, settings));
|
37 | break;
|
38 | default:
|
39 | describe(name, applyTimings(callback, settings));
|
40 | }
|
41 | },
|
42 | test(name: string, callback: core.CallbackOptionallyAsync, settings?: core.TestSettings): void {
|
43 | switch (settings && settings.execution) {
|
44 | case "only":
|
45 | it.only(name, applyTimings(callback, settings));
|
46 | break;
|
47 | case "skip":
|
48 | it.skip(name, applyTimings(callback, settings));
|
49 | break;
|
50 | case "pending":
|
51 | it(name);
|
52 | break;
|
53 | default:
|
54 | it(name, applyTimings(callback, settings));
|
55 | }
|
56 | },
|
57 |
|
58 | beforeAll(name: string, callback: core.CallbackOptionallyAsync, settings?: core.LifecycleSettings): void {
|
59 | before(applyTimings(callback, settings));
|
60 | },
|
61 | beforeEach(name: string, callback: core.CallbackOptionallyAsync, settings?: core.LifecycleSettings): void {
|
62 | beforeEach(applyTimings(callback, settings));
|
63 | },
|
64 | afterEach(name: string, callback: core.CallbackOptionallyAsync, settings?: core.LifecycleSettings): void {
|
65 | afterEach(applyTimings(callback, settings));
|
66 | },
|
67 | afterAll(name: string, callback: core.CallbackOptionallyAsync, settings?: core.LifecycleSettings): void {
|
68 | after(applyTimings(callback, settings));
|
69 | }
|
70 | };
|
71 |
|
72 | class MochaClassTestUI extends core.ClassTestUI {
|
73 |
|
74 | public constructor(runner: core.TestRunner = mochaRunner) {
|
75 | super(runner);
|
76 | }
|
77 | }
|
78 |
|
79 | const mochaDecorators = new MochaClassTestUI();
|
80 |
|
81 | interface MochaClassTestUI {
|
82 | readonly context: unique symbol;
|
83 | }
|
84 |
|
85 | declare global {
|
86 | interface Function {
|
87 | [mochaDecorators.context]: Mocha.Suite;
|
88 | }
|
89 | interface Object {
|
90 | [mochaDecorators.context]: Mocha.Context
|
91 | }
|
92 | }
|
93 |
|
94 | export const {
|
95 |
|
96 | context,
|
97 |
|
98 | suite,
|
99 | test,
|
100 | slow,
|
101 | timeout,
|
102 | retries,
|
103 | pending,
|
104 | only,
|
105 | skip,
|
106 | params
|
107 | } = mochaDecorators;
|