@sprucelabs/test-utils 5.5.6 → 5.5.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/SpruceTestResolver.d.ts +35 -0
- package/build/SpruceTestResolver.js +95 -0
- package/build/decorators.d.ts +0 -34
- package/build/decorators.js +86 -131
- package/build/esm/SpruceTestResolver.d.ts +35 -0
- package/build/esm/SpruceTestResolver.js +90 -0
- package/build/esm/decorators.d.ts +0 -34
- package/build/esm/decorators.js +49 -120
- package/package.json +1 -1
@@ -0,0 +1,35 @@
|
|
1
|
+
type TestLifecycleListener = (Test: any) => any | Promise<any>;
|
2
|
+
export declare class TestLifecycleListeners {
|
3
|
+
static willBeforeAllListeners: TestLifecycleListener[];
|
4
|
+
static didBeforeAllListeners: TestLifecycleListener[];
|
5
|
+
static willBeforeEachListeners: TestLifecycleListener[];
|
6
|
+
static didBeforeEachListeners: TestLifecycleListener[];
|
7
|
+
static willAfterEachListeners: TestLifecycleListener[];
|
8
|
+
static didAfterEachListeners: TestLifecycleListener[];
|
9
|
+
static willAfterAllListeners: TestLifecycleListener[];
|
10
|
+
static didAfterAllListeners: TestLifecycleListener[];
|
11
|
+
static emitWillRunBeforeAll(): Promise<void>;
|
12
|
+
static emitDidRunBeforeAll(): Promise<void>;
|
13
|
+
static emitWillRunBeforeEach(): Promise<void>;
|
14
|
+
static emitDidRunBeforeEach(): Promise<void>;
|
15
|
+
static emitWillRunAfterEach(): Promise<void>;
|
16
|
+
static emitDidRunAfterEach(): Promise<void>;
|
17
|
+
static emitWillRunAfterAll(): Promise<void>;
|
18
|
+
static emitDidRunAfterAll(): Promise<void>;
|
19
|
+
}
|
20
|
+
export default class SpruceTestResolver {
|
21
|
+
static ActiveTestClass?: any;
|
22
|
+
private static __activeTest;
|
23
|
+
static resolveTestClass(target: any): any;
|
24
|
+
static getActiveTest(): any;
|
25
|
+
static reset(): void;
|
26
|
+
static onWillCallBeforeAll(cb: TestLifecycleListener): void;
|
27
|
+
static onDidBeforeAll(cb: TestLifecycleListener): void;
|
28
|
+
static onWillCallBeforeEach(cb: TestLifecycleListener): void;
|
29
|
+
static onDidCallBeforeEach(cb: TestLifecycleListener): void;
|
30
|
+
static onWillCallAfterEach(cb: TestLifecycleListener): void;
|
31
|
+
static onDidCallAfterEach(cb: TestLifecycleListener): void;
|
32
|
+
static onWillCallAfterAll(cb: TestLifecycleListener): void;
|
33
|
+
static onDidCallAfterAll(cb: TestLifecycleListener): void;
|
34
|
+
}
|
35
|
+
export {};
|
@@ -0,0 +1,95 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.TestLifecycleListeners = void 0;
|
4
|
+
class TestLifecycleListeners {
|
5
|
+
static async emitWillRunBeforeAll() {
|
6
|
+
for (const cb of this.willBeforeAllListeners) {
|
7
|
+
await cb(SpruceTestResolver.getActiveTest().constructor);
|
8
|
+
}
|
9
|
+
}
|
10
|
+
static async emitDidRunBeforeAll() {
|
11
|
+
for (const cb of this.didBeforeAllListeners) {
|
12
|
+
await cb(SpruceTestResolver.getActiveTest().constructor);
|
13
|
+
}
|
14
|
+
}
|
15
|
+
static async emitWillRunBeforeEach() {
|
16
|
+
for (const cb of this.willBeforeEachListeners) {
|
17
|
+
await cb(SpruceTestResolver.getActiveTest());
|
18
|
+
}
|
19
|
+
}
|
20
|
+
static async emitDidRunBeforeEach() {
|
21
|
+
for (const cb of this.didBeforeEachListeners) {
|
22
|
+
await cb(SpruceTestResolver.getActiveTest());
|
23
|
+
}
|
24
|
+
}
|
25
|
+
static async emitWillRunAfterEach() {
|
26
|
+
for (const cb of this.willAfterEachListeners) {
|
27
|
+
await cb(SpruceTestResolver.getActiveTest());
|
28
|
+
}
|
29
|
+
}
|
30
|
+
static async emitDidRunAfterEach() {
|
31
|
+
for (const cb of this.didAfterEachListeners) {
|
32
|
+
await cb(SpruceTestResolver.getActiveTest());
|
33
|
+
}
|
34
|
+
}
|
35
|
+
static async emitWillRunAfterAll() {
|
36
|
+
for (const cb of this.willAfterAllListeners) {
|
37
|
+
await cb(SpruceTestResolver.getActiveTest().constructor);
|
38
|
+
}
|
39
|
+
}
|
40
|
+
static async emitDidRunAfterAll() {
|
41
|
+
for (const cb of this.didAfterAllListeners) {
|
42
|
+
await cb(SpruceTestResolver.getActiveTest().constructor);
|
43
|
+
}
|
44
|
+
}
|
45
|
+
}
|
46
|
+
exports.TestLifecycleListeners = TestLifecycleListeners;
|
47
|
+
TestLifecycleListeners.willBeforeAllListeners = [];
|
48
|
+
TestLifecycleListeners.didBeforeAllListeners = [];
|
49
|
+
TestLifecycleListeners.willBeforeEachListeners = [];
|
50
|
+
TestLifecycleListeners.didBeforeEachListeners = [];
|
51
|
+
TestLifecycleListeners.willAfterEachListeners = [];
|
52
|
+
TestLifecycleListeners.didAfterEachListeners = [];
|
53
|
+
TestLifecycleListeners.willAfterAllListeners = [];
|
54
|
+
TestLifecycleListeners.didAfterAllListeners = [];
|
55
|
+
class SpruceTestResolver {
|
56
|
+
static resolveTestClass(target) {
|
57
|
+
if (!this.__activeTest) {
|
58
|
+
this.__activeTest = this.ActiveTestClass
|
59
|
+
? new this.ActiveTestClass()
|
60
|
+
: target;
|
61
|
+
}
|
62
|
+
return this.__activeTest;
|
63
|
+
}
|
64
|
+
static getActiveTest() {
|
65
|
+
return this.__activeTest;
|
66
|
+
}
|
67
|
+
static reset() {
|
68
|
+
delete this.__activeTest;
|
69
|
+
}
|
70
|
+
static onWillCallBeforeAll(cb) {
|
71
|
+
TestLifecycleListeners.willBeforeAllListeners.push(cb);
|
72
|
+
}
|
73
|
+
static onDidBeforeAll(cb) {
|
74
|
+
TestLifecycleListeners.didBeforeAllListeners.push(cb);
|
75
|
+
}
|
76
|
+
static onWillCallBeforeEach(cb) {
|
77
|
+
TestLifecycleListeners.willBeforeEachListeners.push(cb);
|
78
|
+
}
|
79
|
+
static onDidCallBeforeEach(cb) {
|
80
|
+
TestLifecycleListeners.didBeforeEachListeners.push(cb);
|
81
|
+
}
|
82
|
+
static onWillCallAfterEach(cb) {
|
83
|
+
TestLifecycleListeners.willAfterEachListeners.push(cb);
|
84
|
+
}
|
85
|
+
static onDidCallAfterEach(cb) {
|
86
|
+
TestLifecycleListeners.didAfterEachListeners.push(cb);
|
87
|
+
}
|
88
|
+
static onWillCallAfterAll(cb) {
|
89
|
+
TestLifecycleListeners.willAfterAllListeners.push(cb);
|
90
|
+
}
|
91
|
+
static onDidCallAfterAll(cb) {
|
92
|
+
TestLifecycleListeners.didAfterAllListeners.push(cb);
|
93
|
+
}
|
94
|
+
}
|
95
|
+
exports.default = SpruceTestResolver;
|
package/build/decorators.d.ts
CHANGED
@@ -1,37 +1,3 @@
|
|
1
|
-
type TestLifecycleListener = () => any | Promise<any>;
|
2
|
-
type TestLifecycleListenerWithTest = (Test: any) => any | Promise<any>;
|
3
|
-
export declare class TestLifecycleListeners {
|
4
|
-
static willBeforeAllListeners: TestLifecycleListener[];
|
5
|
-
static didBeforeAllListeners: TestLifecycleListener[];
|
6
|
-
static willBeforeEachListeners: TestLifecycleListenerWithTest[];
|
7
|
-
static didBeforeEachListeners: TestLifecycleListenerWithTest[];
|
8
|
-
static willAfterEachListeners: TestLifecycleListenerWithTest[];
|
9
|
-
static didAfterEachListeners: TestLifecycleListenerWithTest[];
|
10
|
-
static willAfterAllListeners: TestLifecycleListener[];
|
11
|
-
static didAfterAllListeners: TestLifecycleListener[];
|
12
|
-
static emitWillRunBeforeAll(): Promise<void>;
|
13
|
-
static emitDidRunBeforeAll(): Promise<void>;
|
14
|
-
static emitWillRunBeforeEach(): Promise<void>;
|
15
|
-
static emitDidRunBeforeEach(): Promise<void>;
|
16
|
-
static emitWillRunAfterEach(): Promise<void>;
|
17
|
-
static emitDidRunAfterEach(): Promise<void>;
|
18
|
-
static emitWillRunAfterAll(): Promise<void>;
|
19
|
-
static emitDidRunAfterAll(): Promise<void>;
|
20
|
-
}
|
21
|
-
export declare class SpruceTestResolver {
|
22
|
-
static ActiveTestClass?: any;
|
23
|
-
private static __activeTest;
|
24
|
-
static resolveTestClass(target: any): any;
|
25
|
-
static getActiveTest(): any;
|
26
|
-
static onWillCallBeforeAll(cb: TestLifecycleListener): void;
|
27
|
-
static onDidBeforeAll(cb: TestLifecycleListener): void;
|
28
|
-
static onWillCallBeforeEach(cb: TestLifecycleListenerWithTest): void;
|
29
|
-
static onDidCallBeforeEach(cb: TestLifecycleListenerWithTest): void;
|
30
|
-
static onWillCallAfterEach(cb: TestLifecycleListenerWithTest): void;
|
31
|
-
static onDidCallAfterEach(cb: TestLifecycleListenerWithTest): void;
|
32
|
-
static onWillCallAfterAll(cb: TestLifecycleListener): void;
|
33
|
-
static onDidCallAfterAll(cb: TestLifecycleListener): void;
|
34
|
-
}
|
35
1
|
/** Test decorator */
|
36
2
|
declare function test(description?: string, ...args: any[]): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
37
3
|
declare namespace test {
|
package/build/decorators.js
CHANGED
@@ -1,101 +1,45 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
19
|
+
var ownKeys = function(o) {
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
21
|
+
var ar = [];
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
23
|
+
return ar;
|
24
|
+
};
|
25
|
+
return ownKeys(o);
|
26
|
+
};
|
27
|
+
return function (mod) {
|
28
|
+
if (mod && mod.__esModule) return mod;
|
29
|
+
var result = {};
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
31
|
+
__setModuleDefault(result, mod);
|
32
|
+
return result;
|
33
|
+
};
|
34
|
+
})();
|
2
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.SpruceTestResolver = exports.TestLifecycleListeners = void 0;
|
4
36
|
exports.default = test;
|
5
37
|
exports.suite = suite;
|
38
|
+
const SpruceTestResolver_1 = __importStar(require("./SpruceTestResolver"));
|
6
39
|
if (typeof it === 'undefined') {
|
7
40
|
//@ts-ignore
|
8
41
|
global.it = () => { };
|
9
42
|
}
|
10
|
-
class TestLifecycleListeners {
|
11
|
-
static async emitWillRunBeforeAll() {
|
12
|
-
for (const cb of this.willBeforeAllListeners) {
|
13
|
-
await cb();
|
14
|
-
}
|
15
|
-
}
|
16
|
-
static async emitDidRunBeforeAll() {
|
17
|
-
for (const cb of this.didBeforeAllListeners) {
|
18
|
-
await cb();
|
19
|
-
}
|
20
|
-
}
|
21
|
-
static async emitWillRunBeforeEach() {
|
22
|
-
for (const cb of this.willBeforeEachListeners) {
|
23
|
-
await cb(SpruceTestResolver.getActiveTest());
|
24
|
-
}
|
25
|
-
}
|
26
|
-
static async emitDidRunBeforeEach() {
|
27
|
-
for (const cb of this.didBeforeEachListeners) {
|
28
|
-
await cb(SpruceTestResolver.getActiveTest());
|
29
|
-
}
|
30
|
-
}
|
31
|
-
static async emitWillRunAfterEach() {
|
32
|
-
for (const cb of this.willAfterEachListeners) {
|
33
|
-
await cb(SpruceTestResolver.getActiveTest());
|
34
|
-
}
|
35
|
-
}
|
36
|
-
static async emitDidRunAfterEach() {
|
37
|
-
for (const cb of this.didAfterEachListeners) {
|
38
|
-
await cb(SpruceTestResolver.getActiveTest());
|
39
|
-
}
|
40
|
-
}
|
41
|
-
static async emitWillRunAfterAll() {
|
42
|
-
for (const cb of this.willAfterAllListeners) {
|
43
|
-
await cb();
|
44
|
-
}
|
45
|
-
}
|
46
|
-
static async emitDidRunAfterAll() {
|
47
|
-
for (const cb of this.didAfterAllListeners) {
|
48
|
-
await cb();
|
49
|
-
}
|
50
|
-
}
|
51
|
-
}
|
52
|
-
exports.TestLifecycleListeners = TestLifecycleListeners;
|
53
|
-
TestLifecycleListeners.willBeforeAllListeners = [];
|
54
|
-
TestLifecycleListeners.didBeforeAllListeners = [];
|
55
|
-
TestLifecycleListeners.willBeforeEachListeners = [];
|
56
|
-
TestLifecycleListeners.didBeforeEachListeners = [];
|
57
|
-
TestLifecycleListeners.willAfterEachListeners = [];
|
58
|
-
TestLifecycleListeners.didAfterEachListeners = [];
|
59
|
-
TestLifecycleListeners.willAfterAllListeners = [];
|
60
|
-
TestLifecycleListeners.didAfterAllListeners = [];
|
61
|
-
class SpruceTestResolver {
|
62
|
-
static resolveTestClass(target) {
|
63
|
-
if (!this.__activeTest) {
|
64
|
-
this.__activeTest = this.ActiveTestClass
|
65
|
-
? new this.ActiveTestClass()
|
66
|
-
: target;
|
67
|
-
}
|
68
|
-
return this.__activeTest;
|
69
|
-
}
|
70
|
-
static getActiveTest() {
|
71
|
-
return this.__activeTest;
|
72
|
-
}
|
73
|
-
static onWillCallBeforeAll(cb) {
|
74
|
-
TestLifecycleListeners.willBeforeAllListeners.push(cb);
|
75
|
-
}
|
76
|
-
static onDidBeforeAll(cb) {
|
77
|
-
TestLifecycleListeners.didBeforeAllListeners.push(cb);
|
78
|
-
}
|
79
|
-
static onWillCallBeforeEach(cb) {
|
80
|
-
TestLifecycleListeners.willBeforeEachListeners.push(cb);
|
81
|
-
}
|
82
|
-
static onDidCallBeforeEach(cb) {
|
83
|
-
TestLifecycleListeners.didBeforeEachListeners.push(cb);
|
84
|
-
}
|
85
|
-
static onWillCallAfterEach(cb) {
|
86
|
-
TestLifecycleListeners.willAfterEachListeners.push(cb);
|
87
|
-
}
|
88
|
-
static onDidCallAfterEach(cb) {
|
89
|
-
TestLifecycleListeners.didAfterEachListeners.push(cb);
|
90
|
-
}
|
91
|
-
static onWillCallAfterAll(cb) {
|
92
|
-
TestLifecycleListeners.willAfterAllListeners.push(cb);
|
93
|
-
}
|
94
|
-
static onDidCallAfterAll(cb) {
|
95
|
-
TestLifecycleListeners.didAfterAllListeners.push(cb);
|
96
|
-
}
|
97
|
-
}
|
98
|
-
exports.SpruceTestResolver = SpruceTestResolver;
|
99
43
|
//recursive function to get static method by name looping up through constructor chain
|
100
44
|
function resolveMethod(Target, name) {
|
101
45
|
if (Target[name]) {
|
@@ -107,64 +51,75 @@ function resolveMethod(Target, name) {
|
|
107
51
|
return null;
|
108
52
|
}
|
109
53
|
/** Hooks up before, after, etc. */
|
110
|
-
function hookupTestClassToJestLifecycle(Target
|
111
|
-
if (Target.
|
54
|
+
function hookupTestClassToJestLifecycle(Target) {
|
55
|
+
if (Target.__areLifecycleHooksInPlace) {
|
112
56
|
return;
|
113
57
|
}
|
114
|
-
Target.
|
115
|
-
const hooks =
|
58
|
+
Target.__areLifecycleHooksInPlace = true;
|
59
|
+
const hooks = ['beforeAll', 'beforeEach', 'afterAll', 'afterEach'];
|
116
60
|
hooks.forEach((hook) => {
|
117
|
-
const cb = resolveMethod(Target, hook);
|
118
|
-
// Have they defined a hook
|
119
|
-
if (!cb) {
|
120
|
-
return;
|
121
|
-
}
|
122
61
|
// @ts-ignore
|
123
62
|
if (global[hook]) {
|
124
63
|
// @ts-ignore
|
125
64
|
global[hook](async () => {
|
65
|
+
SpruceTestResolver_1.default.resolveTestClass(Target);
|
126
66
|
if (hook === 'beforeEach') {
|
127
|
-
|
128
|
-
await TestLifecycleListeners.emitWillRunBeforeEach();
|
67
|
+
await SpruceTestResolver_1.TestLifecycleListeners.emitWillRunBeforeEach();
|
129
68
|
await runBeforeEach(Target);
|
130
|
-
await TestLifecycleListeners.emitDidRunBeforeEach();
|
69
|
+
await SpruceTestResolver_1.TestLifecycleListeners.emitDidRunBeforeEach();
|
131
70
|
}
|
132
71
|
else if (hook === 'afterEach') {
|
133
|
-
await TestLifecycleListeners.emitWillRunAfterEach();
|
72
|
+
await SpruceTestResolver_1.TestLifecycleListeners.emitWillRunAfterEach();
|
134
73
|
await runAfterEach(Target);
|
135
|
-
await TestLifecycleListeners.emitDidRunAfterEach();
|
136
|
-
|
137
|
-
|
74
|
+
await SpruceTestResolver_1.TestLifecycleListeners.emitDidRunAfterEach();
|
75
|
+
SpruceTestResolver_1.default.reset();
|
76
|
+
}
|
77
|
+
else if (hook === 'beforeAll') {
|
78
|
+
await SpruceTestResolver_1.TestLifecycleListeners.emitWillRunBeforeAll();
|
79
|
+
await runBeforeAll(Target);
|
80
|
+
await SpruceTestResolver_1.TestLifecycleListeners.emitDidRunBeforeAll();
|
81
|
+
SpruceTestResolver_1.default.reset();
|
138
82
|
}
|
139
|
-
else {
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
else if (hook === 'afterAll') {
|
144
|
-
await TestLifecycleListeners.emitWillRunAfterAll();
|
145
|
-
}
|
146
|
-
if (SpruceTestResolver.ActiveTestClass) {
|
147
|
-
await cb.apply(Target.constructor);
|
148
|
-
}
|
149
|
-
else {
|
150
|
-
await cb.apply(Target);
|
151
|
-
}
|
152
|
-
if (hook === 'beforeAll') {
|
153
|
-
await TestLifecycleListeners.emitDidRunBeforeAll();
|
154
|
-
}
|
155
|
-
else if (hook === 'afterAll') {
|
156
|
-
await TestLifecycleListeners.emitDidRunAfterAll();
|
157
|
-
}
|
83
|
+
else if (hook === 'afterAll') {
|
84
|
+
await SpruceTestResolver_1.TestLifecycleListeners.emitWillRunAfterAll();
|
85
|
+
await runAfterAll(Target);
|
86
|
+
await SpruceTestResolver_1.TestLifecycleListeners.emitDidRunAfterAll();
|
158
87
|
}
|
159
88
|
});
|
160
89
|
}
|
161
90
|
});
|
162
91
|
}
|
92
|
+
async function runBeforeAll(Target) {
|
93
|
+
const cb = resolveMethod(Target, 'beforeAll');
|
94
|
+
await cb?.apply?.(Target.constructor);
|
95
|
+
}
|
96
|
+
async function runAfterAll(Target) {
|
97
|
+
const cb = resolveMethod(Target, 'afterAll');
|
98
|
+
await cb?.apply?.(Target.constructor);
|
99
|
+
}
|
163
100
|
async function runAfterEach(Target) {
|
164
|
-
|
101
|
+
if (SpruceTestResolver_1.default.ActiveTestClass) {
|
102
|
+
const Resolved = SpruceTestResolver_1.default.resolveTestClass(Target);
|
103
|
+
await Resolved.afterEach?.apply(Resolved);
|
104
|
+
}
|
105
|
+
else if (Target.afterEach) {
|
106
|
+
await Target.afterEach?.apply?.(Target);
|
107
|
+
}
|
108
|
+
else {
|
109
|
+
await Target?.constructor.afterEach?.apply?.(Target.constructor);
|
110
|
+
}
|
165
111
|
}
|
166
112
|
async function runBeforeEach(Target) {
|
167
|
-
|
113
|
+
if (SpruceTestResolver_1.default.ActiveTestClass) {
|
114
|
+
const Resolved = SpruceTestResolver_1.default.resolveTestClass(Target);
|
115
|
+
await Resolved.beforeEach?.apply(Resolved);
|
116
|
+
}
|
117
|
+
else if (Target.beforeEach) {
|
118
|
+
await Target.beforeEach?.apply?.(Target);
|
119
|
+
}
|
120
|
+
else {
|
121
|
+
await Target?.constructor.beforeEach?.apply?.(Target.constructor);
|
122
|
+
}
|
168
123
|
}
|
169
124
|
/** Test decorator */
|
170
125
|
function test(description, ...args) {
|
@@ -172,7 +127,7 @@ function test(description, ...args) {
|
|
172
127
|
hookupTestClassToJestLifecycle(target);
|
173
128
|
// Make sure each test gets the spruce
|
174
129
|
it(description ?? propertyKey, async () => {
|
175
|
-
const testClass =
|
130
|
+
const testClass = SpruceTestResolver_1.default.resolveTestClass(target);
|
176
131
|
const bound = descriptor.value.bind(testClass);
|
177
132
|
//@ts-ignore
|
178
133
|
global.activeTest = {
|
@@ -185,7 +140,7 @@ function test(description, ...args) {
|
|
185
140
|
}
|
186
141
|
function suite() {
|
187
142
|
return function (Target) {
|
188
|
-
|
143
|
+
SpruceTestResolver_1.default.ActiveTestClass = Target;
|
189
144
|
};
|
190
145
|
}
|
191
146
|
/** Only decorator */
|
@@ -195,7 +150,7 @@ test.only = (description, ...args) => {
|
|
195
150
|
hookupTestClassToJestLifecycle(target);
|
196
151
|
// Make sure each test gets the spruce
|
197
152
|
it.only(description ?? propertyKey, async () => {
|
198
|
-
const bound = descriptor.value.bind(
|
153
|
+
const bound = descriptor.value.bind(SpruceTestResolver_1.default.resolveTestClass(target));
|
199
154
|
return bound(...args);
|
200
155
|
});
|
201
156
|
};
|
@@ -216,7 +171,7 @@ test.skip = (description, ...args) => {
|
|
216
171
|
hookupTestClassToJestLifecycle(target);
|
217
172
|
// Make sure each test gets the spruce
|
218
173
|
it.skip(description ?? propertyKey, async () => {
|
219
|
-
const bound = descriptor.value.bind(
|
174
|
+
const bound = descriptor.value.bind(SpruceTestResolver_1.default.resolveTestClass(target));
|
220
175
|
return bound(...args);
|
221
176
|
});
|
222
177
|
};
|
@@ -0,0 +1,35 @@
|
|
1
|
+
type TestLifecycleListener = (Test: any) => any | Promise<any>;
|
2
|
+
export declare class TestLifecycleListeners {
|
3
|
+
static willBeforeAllListeners: TestLifecycleListener[];
|
4
|
+
static didBeforeAllListeners: TestLifecycleListener[];
|
5
|
+
static willBeforeEachListeners: TestLifecycleListener[];
|
6
|
+
static didBeforeEachListeners: TestLifecycleListener[];
|
7
|
+
static willAfterEachListeners: TestLifecycleListener[];
|
8
|
+
static didAfterEachListeners: TestLifecycleListener[];
|
9
|
+
static willAfterAllListeners: TestLifecycleListener[];
|
10
|
+
static didAfterAllListeners: TestLifecycleListener[];
|
11
|
+
static emitWillRunBeforeAll(): Promise<void>;
|
12
|
+
static emitDidRunBeforeAll(): Promise<void>;
|
13
|
+
static emitWillRunBeforeEach(): Promise<void>;
|
14
|
+
static emitDidRunBeforeEach(): Promise<void>;
|
15
|
+
static emitWillRunAfterEach(): Promise<void>;
|
16
|
+
static emitDidRunAfterEach(): Promise<void>;
|
17
|
+
static emitWillRunAfterAll(): Promise<void>;
|
18
|
+
static emitDidRunAfterAll(): Promise<void>;
|
19
|
+
}
|
20
|
+
export default class SpruceTestResolver {
|
21
|
+
static ActiveTestClass?: any;
|
22
|
+
private static __activeTest;
|
23
|
+
static resolveTestClass(target: any): any;
|
24
|
+
static getActiveTest(): any;
|
25
|
+
static reset(): void;
|
26
|
+
static onWillCallBeforeAll(cb: TestLifecycleListener): void;
|
27
|
+
static onDidBeforeAll(cb: TestLifecycleListener): void;
|
28
|
+
static onWillCallBeforeEach(cb: TestLifecycleListener): void;
|
29
|
+
static onDidCallBeforeEach(cb: TestLifecycleListener): void;
|
30
|
+
static onWillCallAfterEach(cb: TestLifecycleListener): void;
|
31
|
+
static onDidCallAfterEach(cb: TestLifecycleListener): void;
|
32
|
+
static onWillCallAfterAll(cb: TestLifecycleListener): void;
|
33
|
+
static onDidCallAfterAll(cb: TestLifecycleListener): void;
|
34
|
+
}
|
35
|
+
export {};
|
@@ -0,0 +1,90 @@
|
|
1
|
+
export class TestLifecycleListeners {
|
2
|
+
static async emitWillRunBeforeAll() {
|
3
|
+
for (const cb of this.willBeforeAllListeners) {
|
4
|
+
await cb(SpruceTestResolver.getActiveTest().constructor);
|
5
|
+
}
|
6
|
+
}
|
7
|
+
static async emitDidRunBeforeAll() {
|
8
|
+
for (const cb of this.didBeforeAllListeners) {
|
9
|
+
await cb(SpruceTestResolver.getActiveTest().constructor);
|
10
|
+
}
|
11
|
+
}
|
12
|
+
static async emitWillRunBeforeEach() {
|
13
|
+
for (const cb of this.willBeforeEachListeners) {
|
14
|
+
await cb(SpruceTestResolver.getActiveTest());
|
15
|
+
}
|
16
|
+
}
|
17
|
+
static async emitDidRunBeforeEach() {
|
18
|
+
for (const cb of this.didBeforeEachListeners) {
|
19
|
+
await cb(SpruceTestResolver.getActiveTest());
|
20
|
+
}
|
21
|
+
}
|
22
|
+
static async emitWillRunAfterEach() {
|
23
|
+
for (const cb of this.willAfterEachListeners) {
|
24
|
+
await cb(SpruceTestResolver.getActiveTest());
|
25
|
+
}
|
26
|
+
}
|
27
|
+
static async emitDidRunAfterEach() {
|
28
|
+
for (const cb of this.didAfterEachListeners) {
|
29
|
+
await cb(SpruceTestResolver.getActiveTest());
|
30
|
+
}
|
31
|
+
}
|
32
|
+
static async emitWillRunAfterAll() {
|
33
|
+
for (const cb of this.willAfterAllListeners) {
|
34
|
+
await cb(SpruceTestResolver.getActiveTest().constructor);
|
35
|
+
}
|
36
|
+
}
|
37
|
+
static async emitDidRunAfterAll() {
|
38
|
+
for (const cb of this.didAfterAllListeners) {
|
39
|
+
await cb(SpruceTestResolver.getActiveTest().constructor);
|
40
|
+
}
|
41
|
+
}
|
42
|
+
}
|
43
|
+
TestLifecycleListeners.willBeforeAllListeners = [];
|
44
|
+
TestLifecycleListeners.didBeforeAllListeners = [];
|
45
|
+
TestLifecycleListeners.willBeforeEachListeners = [];
|
46
|
+
TestLifecycleListeners.didBeforeEachListeners = [];
|
47
|
+
TestLifecycleListeners.willAfterEachListeners = [];
|
48
|
+
TestLifecycleListeners.didAfterEachListeners = [];
|
49
|
+
TestLifecycleListeners.willAfterAllListeners = [];
|
50
|
+
TestLifecycleListeners.didAfterAllListeners = [];
|
51
|
+
export default class SpruceTestResolver {
|
52
|
+
static resolveTestClass(target) {
|
53
|
+
if (!this.__activeTest) {
|
54
|
+
this.__activeTest = this.ActiveTestClass
|
55
|
+
? new this.ActiveTestClass()
|
56
|
+
: target;
|
57
|
+
}
|
58
|
+
return this.__activeTest;
|
59
|
+
}
|
60
|
+
static getActiveTest() {
|
61
|
+
return this.__activeTest;
|
62
|
+
}
|
63
|
+
static reset() {
|
64
|
+
delete this.__activeTest;
|
65
|
+
}
|
66
|
+
static onWillCallBeforeAll(cb) {
|
67
|
+
TestLifecycleListeners.willBeforeAllListeners.push(cb);
|
68
|
+
}
|
69
|
+
static onDidBeforeAll(cb) {
|
70
|
+
TestLifecycleListeners.didBeforeAllListeners.push(cb);
|
71
|
+
}
|
72
|
+
static onWillCallBeforeEach(cb) {
|
73
|
+
TestLifecycleListeners.willBeforeEachListeners.push(cb);
|
74
|
+
}
|
75
|
+
static onDidCallBeforeEach(cb) {
|
76
|
+
TestLifecycleListeners.didBeforeEachListeners.push(cb);
|
77
|
+
}
|
78
|
+
static onWillCallAfterEach(cb) {
|
79
|
+
TestLifecycleListeners.willAfterEachListeners.push(cb);
|
80
|
+
}
|
81
|
+
static onDidCallAfterEach(cb) {
|
82
|
+
TestLifecycleListeners.didAfterEachListeners.push(cb);
|
83
|
+
}
|
84
|
+
static onWillCallAfterAll(cb) {
|
85
|
+
TestLifecycleListeners.willAfterAllListeners.push(cb);
|
86
|
+
}
|
87
|
+
static onDidCallAfterAll(cb) {
|
88
|
+
TestLifecycleListeners.didAfterAllListeners.push(cb);
|
89
|
+
}
|
90
|
+
}
|
@@ -1,37 +1,3 @@
|
|
1
|
-
type TestLifecycleListener = () => any | Promise<any>;
|
2
|
-
type TestLifecycleListenerWithTest = (Test: any) => any | Promise<any>;
|
3
|
-
export declare class TestLifecycleListeners {
|
4
|
-
static willBeforeAllListeners: TestLifecycleListener[];
|
5
|
-
static didBeforeAllListeners: TestLifecycleListener[];
|
6
|
-
static willBeforeEachListeners: TestLifecycleListenerWithTest[];
|
7
|
-
static didBeforeEachListeners: TestLifecycleListenerWithTest[];
|
8
|
-
static willAfterEachListeners: TestLifecycleListenerWithTest[];
|
9
|
-
static didAfterEachListeners: TestLifecycleListenerWithTest[];
|
10
|
-
static willAfterAllListeners: TestLifecycleListener[];
|
11
|
-
static didAfterAllListeners: TestLifecycleListener[];
|
12
|
-
static emitWillRunBeforeAll(): Promise<void>;
|
13
|
-
static emitDidRunBeforeAll(): Promise<void>;
|
14
|
-
static emitWillRunBeforeEach(): Promise<void>;
|
15
|
-
static emitDidRunBeforeEach(): Promise<void>;
|
16
|
-
static emitWillRunAfterEach(): Promise<void>;
|
17
|
-
static emitDidRunAfterEach(): Promise<void>;
|
18
|
-
static emitWillRunAfterAll(): Promise<void>;
|
19
|
-
static emitDidRunAfterAll(): Promise<void>;
|
20
|
-
}
|
21
|
-
export declare class SpruceTestResolver {
|
22
|
-
static ActiveTestClass?: any;
|
23
|
-
private static __activeTest;
|
24
|
-
static resolveTestClass(target: any): any;
|
25
|
-
static getActiveTest(): any;
|
26
|
-
static onWillCallBeforeAll(cb: TestLifecycleListener): void;
|
27
|
-
static onDidBeforeAll(cb: TestLifecycleListener): void;
|
28
|
-
static onWillCallBeforeEach(cb: TestLifecycleListenerWithTest): void;
|
29
|
-
static onDidCallBeforeEach(cb: TestLifecycleListenerWithTest): void;
|
30
|
-
static onWillCallAfterEach(cb: TestLifecycleListenerWithTest): void;
|
31
|
-
static onDidCallAfterEach(cb: TestLifecycleListenerWithTest): void;
|
32
|
-
static onWillCallAfterAll(cb: TestLifecycleListener): void;
|
33
|
-
static onDidCallAfterAll(cb: TestLifecycleListener): void;
|
34
|
-
}
|
35
1
|
/** Test decorator */
|
36
2
|
declare function test(description?: string, ...args: any[]): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
37
3
|
declare namespace test {
|
package/build/esm/decorators.js
CHANGED
@@ -1,94 +1,8 @@
|
|
1
|
+
import SpruceTestResolver, { TestLifecycleListeners, } from './SpruceTestResolver.js';
|
1
2
|
if (typeof it === 'undefined') {
|
2
3
|
//@ts-ignore
|
3
4
|
global.it = () => { };
|
4
5
|
}
|
5
|
-
export class TestLifecycleListeners {
|
6
|
-
static async emitWillRunBeforeAll() {
|
7
|
-
for (const cb of this.willBeforeAllListeners) {
|
8
|
-
await cb();
|
9
|
-
}
|
10
|
-
}
|
11
|
-
static async emitDidRunBeforeAll() {
|
12
|
-
for (const cb of this.didBeforeAllListeners) {
|
13
|
-
await cb();
|
14
|
-
}
|
15
|
-
}
|
16
|
-
static async emitWillRunBeforeEach() {
|
17
|
-
for (const cb of this.willBeforeEachListeners) {
|
18
|
-
await cb(SpruceTestResolver.getActiveTest());
|
19
|
-
}
|
20
|
-
}
|
21
|
-
static async emitDidRunBeforeEach() {
|
22
|
-
for (const cb of this.didBeforeEachListeners) {
|
23
|
-
await cb(SpruceTestResolver.getActiveTest());
|
24
|
-
}
|
25
|
-
}
|
26
|
-
static async emitWillRunAfterEach() {
|
27
|
-
for (const cb of this.willAfterEachListeners) {
|
28
|
-
await cb(SpruceTestResolver.getActiveTest());
|
29
|
-
}
|
30
|
-
}
|
31
|
-
static async emitDidRunAfterEach() {
|
32
|
-
for (const cb of this.didAfterEachListeners) {
|
33
|
-
await cb(SpruceTestResolver.getActiveTest());
|
34
|
-
}
|
35
|
-
}
|
36
|
-
static async emitWillRunAfterAll() {
|
37
|
-
for (const cb of this.willAfterAllListeners) {
|
38
|
-
await cb();
|
39
|
-
}
|
40
|
-
}
|
41
|
-
static async emitDidRunAfterAll() {
|
42
|
-
for (const cb of this.didAfterAllListeners) {
|
43
|
-
await cb();
|
44
|
-
}
|
45
|
-
}
|
46
|
-
}
|
47
|
-
TestLifecycleListeners.willBeforeAllListeners = [];
|
48
|
-
TestLifecycleListeners.didBeforeAllListeners = [];
|
49
|
-
TestLifecycleListeners.willBeforeEachListeners = [];
|
50
|
-
TestLifecycleListeners.didBeforeEachListeners = [];
|
51
|
-
TestLifecycleListeners.willAfterEachListeners = [];
|
52
|
-
TestLifecycleListeners.didAfterEachListeners = [];
|
53
|
-
TestLifecycleListeners.willAfterAllListeners = [];
|
54
|
-
TestLifecycleListeners.didAfterAllListeners = [];
|
55
|
-
export class SpruceTestResolver {
|
56
|
-
static resolveTestClass(target) {
|
57
|
-
if (!this.__activeTest) {
|
58
|
-
this.__activeTest = this.ActiveTestClass
|
59
|
-
? new this.ActiveTestClass()
|
60
|
-
: target;
|
61
|
-
}
|
62
|
-
return this.__activeTest;
|
63
|
-
}
|
64
|
-
static getActiveTest() {
|
65
|
-
return this.__activeTest;
|
66
|
-
}
|
67
|
-
static onWillCallBeforeAll(cb) {
|
68
|
-
TestLifecycleListeners.willBeforeAllListeners.push(cb);
|
69
|
-
}
|
70
|
-
static onDidBeforeAll(cb) {
|
71
|
-
TestLifecycleListeners.didBeforeAllListeners.push(cb);
|
72
|
-
}
|
73
|
-
static onWillCallBeforeEach(cb) {
|
74
|
-
TestLifecycleListeners.willBeforeEachListeners.push(cb);
|
75
|
-
}
|
76
|
-
static onDidCallBeforeEach(cb) {
|
77
|
-
TestLifecycleListeners.didBeforeEachListeners.push(cb);
|
78
|
-
}
|
79
|
-
static onWillCallAfterEach(cb) {
|
80
|
-
TestLifecycleListeners.willAfterEachListeners.push(cb);
|
81
|
-
}
|
82
|
-
static onDidCallAfterEach(cb) {
|
83
|
-
TestLifecycleListeners.didAfterEachListeners.push(cb);
|
84
|
-
}
|
85
|
-
static onWillCallAfterAll(cb) {
|
86
|
-
TestLifecycleListeners.willAfterAllListeners.push(cb);
|
87
|
-
}
|
88
|
-
static onDidCallAfterAll(cb) {
|
89
|
-
TestLifecycleListeners.didAfterAllListeners.push(cb);
|
90
|
-
}
|
91
|
-
}
|
92
6
|
//recursive function to get static method by name looping up through constructor chain
|
93
7
|
function resolveMethod(Target, name) {
|
94
8
|
if (Target[name]) {
|
@@ -100,24 +14,19 @@ function resolveMethod(Target, name) {
|
|
100
14
|
return null;
|
101
15
|
}
|
102
16
|
/** Hooks up before, after, etc. */
|
103
|
-
function hookupTestClassToJestLifecycle(Target
|
104
|
-
if (Target.
|
17
|
+
function hookupTestClassToJestLifecycle(Target) {
|
18
|
+
if (Target.__areLifecycleHooksInPlace) {
|
105
19
|
return;
|
106
20
|
}
|
107
|
-
Target.
|
108
|
-
const hooks =
|
21
|
+
Target.__areLifecycleHooksInPlace = true;
|
22
|
+
const hooks = ['beforeAll', 'beforeEach', 'afterAll', 'afterEach'];
|
109
23
|
hooks.forEach((hook) => {
|
110
|
-
const cb = resolveMethod(Target, hook);
|
111
|
-
// Have they defined a hook
|
112
|
-
if (!cb) {
|
113
|
-
return;
|
114
|
-
}
|
115
24
|
// @ts-ignore
|
116
25
|
if (global[hook]) {
|
117
26
|
// @ts-ignore
|
118
27
|
global[hook](async () => {
|
28
|
+
SpruceTestResolver.resolveTestClass(Target);
|
119
29
|
if (hook === 'beforeEach') {
|
120
|
-
SpruceTestResolver.resolveTestClass(Target);
|
121
30
|
await TestLifecycleListeners.emitWillRunBeforeEach();
|
122
31
|
await runBeforeEach(Target);
|
123
32
|
await TestLifecycleListeners.emitDidRunBeforeEach();
|
@@ -126,38 +35,58 @@ function hookupTestClassToJestLifecycle(Target, h) {
|
|
126
35
|
await TestLifecycleListeners.emitWillRunAfterEach();
|
127
36
|
await runAfterEach(Target);
|
128
37
|
await TestLifecycleListeners.emitDidRunAfterEach();
|
129
|
-
|
130
|
-
delete SpruceTestResolver.__activeTest;
|
38
|
+
SpruceTestResolver.reset();
|
131
39
|
}
|
132
|
-
else {
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
else {
|
143
|
-
await cb.apply(Target);
|
144
|
-
}
|
145
|
-
if (hook === 'beforeAll') {
|
146
|
-
await TestLifecycleListeners.emitDidRunBeforeAll();
|
147
|
-
}
|
148
|
-
else if (hook === 'afterAll') {
|
149
|
-
await TestLifecycleListeners.emitDidRunAfterAll();
|
150
|
-
}
|
40
|
+
else if (hook === 'beforeAll') {
|
41
|
+
await TestLifecycleListeners.emitWillRunBeforeAll();
|
42
|
+
await runBeforeAll(Target);
|
43
|
+
await TestLifecycleListeners.emitDidRunBeforeAll();
|
44
|
+
SpruceTestResolver.reset();
|
45
|
+
}
|
46
|
+
else if (hook === 'afterAll') {
|
47
|
+
await TestLifecycleListeners.emitWillRunAfterAll();
|
48
|
+
await runAfterAll(Target);
|
49
|
+
await TestLifecycleListeners.emitDidRunAfterAll();
|
151
50
|
}
|
152
51
|
});
|
153
52
|
}
|
154
53
|
});
|
155
54
|
}
|
55
|
+
async function runBeforeAll(Target) {
|
56
|
+
var _a;
|
57
|
+
const cb = resolveMethod(Target, 'beforeAll');
|
58
|
+
await ((_a = cb === null || cb === void 0 ? void 0 : cb.apply) === null || _a === void 0 ? void 0 : _a.call(cb, Target.constructor));
|
59
|
+
}
|
60
|
+
async function runAfterAll(Target) {
|
61
|
+
var _a;
|
62
|
+
const cb = resolveMethod(Target, 'afterAll');
|
63
|
+
await ((_a = cb === null || cb === void 0 ? void 0 : cb.apply) === null || _a === void 0 ? void 0 : _a.call(cb, Target.constructor));
|
64
|
+
}
|
156
65
|
async function runAfterEach(Target) {
|
157
|
-
|
66
|
+
var _a, _b, _c, _d, _e;
|
67
|
+
if (SpruceTestResolver.ActiveTestClass) {
|
68
|
+
const Resolved = SpruceTestResolver.resolveTestClass(Target);
|
69
|
+
await ((_a = Resolved.afterEach) === null || _a === void 0 ? void 0 : _a.apply(Resolved));
|
70
|
+
}
|
71
|
+
else if (Target.afterEach) {
|
72
|
+
await ((_c = (_b = Target.afterEach) === null || _b === void 0 ? void 0 : _b.apply) === null || _c === void 0 ? void 0 : _c.call(_b, Target));
|
73
|
+
}
|
74
|
+
else {
|
75
|
+
await ((_e = (_d = Target === null || Target === void 0 ? void 0 : Target.constructor.afterEach) === null || _d === void 0 ? void 0 : _d.apply) === null || _e === void 0 ? void 0 : _e.call(_d, Target.constructor));
|
76
|
+
}
|
158
77
|
}
|
159
78
|
async function runBeforeEach(Target) {
|
160
|
-
|
79
|
+
var _a, _b, _c, _d, _e;
|
80
|
+
if (SpruceTestResolver.ActiveTestClass) {
|
81
|
+
const Resolved = SpruceTestResolver.resolveTestClass(Target);
|
82
|
+
await ((_a = Resolved.beforeEach) === null || _a === void 0 ? void 0 : _a.apply(Resolved));
|
83
|
+
}
|
84
|
+
else if (Target.beforeEach) {
|
85
|
+
await ((_c = (_b = Target.beforeEach) === null || _b === void 0 ? void 0 : _b.apply) === null || _c === void 0 ? void 0 : _c.call(_b, Target));
|
86
|
+
}
|
87
|
+
else {
|
88
|
+
await ((_e = (_d = Target === null || Target === void 0 ? void 0 : Target.constructor.beforeEach) === null || _d === void 0 ? void 0 : _d.apply) === null || _e === void 0 ? void 0 : _e.call(_d, Target.constructor));
|
89
|
+
}
|
161
90
|
}
|
162
91
|
/** Test decorator */
|
163
92
|
export default function test(description, ...args) {
|