@sprucelabs/test-utils 5.4.1 → 5.4.2
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/README.md +1 -5
- package/build/decorators.d.ts +2 -2
- package/build/decorators.js +33 -31
- package/build/esm/decorators.d.ts +2 -2
- package/build/esm/decorators.js +32 -29
- package/package.json +1 -1
package/README.md
CHANGED
@@ -17,8 +17,4 @@ Spruce XP Documentation
|
|
17
17
|
<br />
|
18
18
|
<p align="center">
|
19
19
|
<a href="https://developer.spruce.ai/#/"><img width="250" src="https://raw.githubusercontent.com/sprucelabsai/spruce-test-utils/master/docs/images/read-full-docs.png" /></a>
|
20
|
-
</p>
|
21
|
-
|
22
|
-
### Dependencies
|
23
|
-
|
24
|
-
[Arkit diagram here](docs/dependencies.md).
|
20
|
+
</p>
|
package/build/decorators.d.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
export declare class
|
1
|
+
export declare class SpruceTestResolver {
|
2
2
|
static ActiveTestClass?: any;
|
3
3
|
private static __activeTest;
|
4
|
-
static
|
4
|
+
static resolveTestClass(target: any): any;
|
5
5
|
static getActiveTest(): any;
|
6
6
|
}
|
7
7
|
/** Test decorator */
|
package/build/decorators.js
CHANGED
@@ -1,50 +1,55 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.SpruceTestResolver = void 0;
|
4
4
|
exports.default = test;
|
5
5
|
exports.suite = suite;
|
6
6
|
if (typeof it === 'undefined') {
|
7
7
|
//@ts-ignore
|
8
8
|
global.it = () => { };
|
9
9
|
}
|
10
|
-
class
|
11
|
-
static
|
12
|
-
this.__activeTest
|
13
|
-
|
14
|
-
|
10
|
+
class SpruceTestResolver {
|
11
|
+
static resolveTestClass(target) {
|
12
|
+
if (!this.__activeTest) {
|
13
|
+
this.__activeTest = this.ActiveTestClass
|
14
|
+
? new this.ActiveTestClass()
|
15
|
+
: target;
|
16
|
+
}
|
15
17
|
return this.__activeTest;
|
16
18
|
}
|
17
19
|
static getActiveTest() {
|
18
20
|
return this.__activeTest;
|
19
21
|
}
|
20
22
|
}
|
21
|
-
exports.
|
23
|
+
exports.SpruceTestResolver = SpruceTestResolver;
|
22
24
|
/** Hooks up before, after, etc. */
|
23
|
-
function
|
24
|
-
if (
|
25
|
+
function hookupTestClassToJestLifecycle(Target, h) {
|
26
|
+
if (Target.__isTestingHookedUp) {
|
25
27
|
return;
|
26
28
|
}
|
27
|
-
|
29
|
+
Target.__isTestingHookedUp = !h;
|
28
30
|
const hooks = h ?? ['beforeAll', 'beforeEach', 'afterAll', 'afterEach'];
|
29
31
|
hooks.forEach((hook) => {
|
32
|
+
const cb = Target[hook] ?? Target?.constructor?.[hook];
|
30
33
|
// Have they defined a hook
|
31
|
-
if (!
|
34
|
+
if (!cb) {
|
35
|
+
debugger;
|
32
36
|
return;
|
33
37
|
}
|
34
|
-
if (SpruceTestDecoratorResolver.ActiveTestClass &&
|
35
|
-
!h &&
|
36
|
-
hook === 'beforeAll') {
|
37
|
-
throw new Error(`beforeAll() and afterAll() must be static`);
|
38
|
-
}
|
39
38
|
// @ts-ignore
|
40
39
|
if (global[hook]) {
|
41
40
|
// @ts-ignore
|
42
41
|
global[hook](async () => {
|
43
|
-
if (hook === '
|
44
|
-
|
45
|
-
|
42
|
+
if (hook === 'beforeEach') {
|
43
|
+
await SpruceTestResolver.resolveTestClass(Target).beforeEach();
|
44
|
+
}
|
45
|
+
else if (hook === 'afterEach') {
|
46
|
+
await SpruceTestResolver.resolveTestClass(Target).afterEach();
|
47
|
+
// @ts-ignore
|
48
|
+
delete SpruceTestResolver.__activeTest;
|
49
|
+
}
|
50
|
+
else {
|
51
|
+
await cb.apply(Target);
|
46
52
|
}
|
47
|
-
return target[hook]();
|
48
53
|
});
|
49
54
|
}
|
50
55
|
});
|
@@ -52,11 +57,10 @@ function hookupTestClass(target, h) {
|
|
52
57
|
/** Test decorator */
|
53
58
|
function test(description, ...args) {
|
54
59
|
return function (target, propertyKey, descriptor) {
|
55
|
-
|
56
|
-
hookupTestClass(target);
|
60
|
+
hookupTestClassToJestLifecycle(target);
|
57
61
|
// Make sure each test gets the spruce
|
58
62
|
it(description ?? propertyKey, async () => {
|
59
|
-
const testClass =
|
63
|
+
const testClass = SpruceTestResolver.resolveTestClass(target);
|
60
64
|
const bound = descriptor.value.bind(testClass);
|
61
65
|
//@ts-ignore
|
62
66
|
global.activeTest = {
|
@@ -69,19 +73,17 @@ function test(description, ...args) {
|
|
69
73
|
}
|
70
74
|
function suite() {
|
71
75
|
return function (Target) {
|
72
|
-
|
73
|
-
// Test.activeTest.__isTestingHookedUp = false
|
74
|
-
hookupTestClass(Target, ['beforeAll', 'afterAll']);
|
76
|
+
SpruceTestResolver.ActiveTestClass = Target;
|
75
77
|
};
|
76
78
|
}
|
77
79
|
/** Only decorator */
|
78
80
|
test.only = (description, ...args) => {
|
79
81
|
return function (target, propertyKey, descriptor) {
|
80
82
|
// Lets attach before/after
|
81
|
-
|
83
|
+
hookupTestClassToJestLifecycle(target);
|
82
84
|
// Make sure each test gets the spruce
|
83
85
|
it.only(description ?? propertyKey, async () => {
|
84
|
-
const bound = descriptor.value.bind(
|
86
|
+
const bound = descriptor.value.bind(SpruceTestResolver.resolveTestClass(target));
|
85
87
|
return bound(...args);
|
86
88
|
});
|
87
89
|
};
|
@@ -90,7 +92,7 @@ test.only = (description, ...args) => {
|
|
90
92
|
test.todo = (description, ..._args) => {
|
91
93
|
return function (target, propertyKey) {
|
92
94
|
// Lets attach before/after
|
93
|
-
|
95
|
+
hookupTestClassToJestLifecycle(target);
|
94
96
|
// Make sure each test gets the spruce
|
95
97
|
it.todo(description ?? propertyKey);
|
96
98
|
};
|
@@ -99,10 +101,10 @@ test.todo = (description, ..._args) => {
|
|
99
101
|
test.skip = (description, ...args) => {
|
100
102
|
return function (target, propertyKey, descriptor) {
|
101
103
|
// Lets attach before/after
|
102
|
-
|
104
|
+
hookupTestClassToJestLifecycle(target);
|
103
105
|
// Make sure each test gets the spruce
|
104
106
|
it.skip(description ?? propertyKey, async () => {
|
105
|
-
const bound = descriptor.value.bind(
|
107
|
+
const bound = descriptor.value.bind(SpruceTestResolver.resolveTestClass(target));
|
106
108
|
return bound(...args);
|
107
109
|
});
|
108
110
|
};
|
@@ -1,7 +1,7 @@
|
|
1
|
-
export declare class
|
1
|
+
export declare class SpruceTestResolver {
|
2
2
|
static ActiveTestClass?: any;
|
3
3
|
private static __activeTest;
|
4
|
-
static
|
4
|
+
static resolveTestClass(target: any): any;
|
5
5
|
static getActiveTest(): any;
|
6
6
|
}
|
7
7
|
/** Test decorator */
|
package/build/esm/decorators.js
CHANGED
@@ -2,11 +2,13 @@ if (typeof it === 'undefined') {
|
|
2
2
|
//@ts-ignore
|
3
3
|
global.it = () => { };
|
4
4
|
}
|
5
|
-
export class
|
6
|
-
static
|
7
|
-
this.__activeTest
|
8
|
-
|
9
|
-
|
5
|
+
export class SpruceTestResolver {
|
6
|
+
static resolveTestClass(target) {
|
7
|
+
if (!this.__activeTest) {
|
8
|
+
this.__activeTest = this.ActiveTestClass
|
9
|
+
? new this.ActiveTestClass()
|
10
|
+
: target;
|
11
|
+
}
|
10
12
|
return this.__activeTest;
|
11
13
|
}
|
12
14
|
static getActiveTest() {
|
@@ -14,31 +16,35 @@ export class SpruceTestDecoratorResolver {
|
|
14
16
|
}
|
15
17
|
}
|
16
18
|
/** Hooks up before, after, etc. */
|
17
|
-
function
|
18
|
-
if (
|
19
|
+
function hookupTestClassToJestLifecycle(Target, h) {
|
20
|
+
if (Target.__isTestingHookedUp) {
|
19
21
|
return;
|
20
22
|
}
|
21
|
-
|
23
|
+
Target.__isTestingHookedUp = !h;
|
22
24
|
const hooks = h !== null && h !== void 0 ? h : ['beforeAll', 'beforeEach', 'afterAll', 'afterEach'];
|
23
25
|
hooks.forEach((hook) => {
|
26
|
+
var _a, _b;
|
27
|
+
const cb = (_a = Target[hook]) !== null && _a !== void 0 ? _a : (_b = Target === null || Target === void 0 ? void 0 : Target.constructor) === null || _b === void 0 ? void 0 : _b[hook];
|
24
28
|
// Have they defined a hook
|
25
|
-
if (!
|
29
|
+
if (!cb) {
|
30
|
+
debugger;
|
26
31
|
return;
|
27
32
|
}
|
28
|
-
if (SpruceTestDecoratorResolver.ActiveTestClass &&
|
29
|
-
!h &&
|
30
|
-
hook === 'beforeAll') {
|
31
|
-
throw new Error(`beforeAll() and afterAll() must be static`);
|
32
|
-
}
|
33
33
|
// @ts-ignore
|
34
34
|
if (global[hook]) {
|
35
35
|
// @ts-ignore
|
36
36
|
global[hook](async () => {
|
37
|
-
if (hook === '
|
38
|
-
|
39
|
-
|
37
|
+
if (hook === 'beforeEach') {
|
38
|
+
await SpruceTestResolver.resolveTestClass(Target).beforeEach();
|
39
|
+
}
|
40
|
+
else if (hook === 'afterEach') {
|
41
|
+
await SpruceTestResolver.resolveTestClass(Target).afterEach();
|
42
|
+
// @ts-ignore
|
43
|
+
delete SpruceTestResolver.__activeTest;
|
44
|
+
}
|
45
|
+
else {
|
46
|
+
await cb.apply(Target);
|
40
47
|
}
|
41
|
-
return target[hook]();
|
42
48
|
});
|
43
49
|
}
|
44
50
|
});
|
@@ -46,11 +52,10 @@ function hookupTestClass(target, h) {
|
|
46
52
|
/** Test decorator */
|
47
53
|
export default function test(description, ...args) {
|
48
54
|
return function (target, propertyKey, descriptor) {
|
49
|
-
|
50
|
-
hookupTestClass(target);
|
55
|
+
hookupTestClassToJestLifecycle(target);
|
51
56
|
// Make sure each test gets the spruce
|
52
57
|
it(description !== null && description !== void 0 ? description : propertyKey, async () => {
|
53
|
-
const testClass =
|
58
|
+
const testClass = SpruceTestResolver.resolveTestClass(target);
|
54
59
|
const bound = descriptor.value.bind(testClass);
|
55
60
|
//@ts-ignore
|
56
61
|
global.activeTest = {
|
@@ -63,19 +68,17 @@ export default function test(description, ...args) {
|
|
63
68
|
}
|
64
69
|
export function suite() {
|
65
70
|
return function (Target) {
|
66
|
-
|
67
|
-
// Test.activeTest.__isTestingHookedUp = false
|
68
|
-
hookupTestClass(Target, ['beforeAll', 'afterAll']);
|
71
|
+
SpruceTestResolver.ActiveTestClass = Target;
|
69
72
|
};
|
70
73
|
}
|
71
74
|
/** Only decorator */
|
72
75
|
test.only = (description, ...args) => {
|
73
76
|
return function (target, propertyKey, descriptor) {
|
74
77
|
// Lets attach before/after
|
75
|
-
|
78
|
+
hookupTestClassToJestLifecycle(target);
|
76
79
|
// Make sure each test gets the spruce
|
77
80
|
it.only(description !== null && description !== void 0 ? description : propertyKey, async () => {
|
78
|
-
const bound = descriptor.value.bind(
|
81
|
+
const bound = descriptor.value.bind(SpruceTestResolver.resolveTestClass(target));
|
79
82
|
return bound(...args);
|
80
83
|
});
|
81
84
|
};
|
@@ -84,7 +87,7 @@ test.only = (description, ...args) => {
|
|
84
87
|
test.todo = (description, ..._args) => {
|
85
88
|
return function (target, propertyKey) {
|
86
89
|
// Lets attach before/after
|
87
|
-
|
90
|
+
hookupTestClassToJestLifecycle(target);
|
88
91
|
// Make sure each test gets the spruce
|
89
92
|
it.todo(description !== null && description !== void 0 ? description : propertyKey);
|
90
93
|
};
|
@@ -93,10 +96,10 @@ test.todo = (description, ..._args) => {
|
|
93
96
|
test.skip = (description, ...args) => {
|
94
97
|
return function (target, propertyKey, descriptor) {
|
95
98
|
// Lets attach before/after
|
96
|
-
|
99
|
+
hookupTestClassToJestLifecycle(target);
|
97
100
|
// Make sure each test gets the spruce
|
98
101
|
it.skip(description !== null && description !== void 0 ? description : propertyKey, async () => {
|
99
|
-
const bound = descriptor.value.bind(
|
102
|
+
const bound = descriptor.value.bind(SpruceTestResolver.resolveTestClass(target));
|
100
103
|
return bound(...args);
|
101
104
|
});
|
102
105
|
};
|