@effectionx/test-adapter 0.3.0 → 0.5.0
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 +6 -1
- package/esm/mod.d.ts +17 -8
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +52 -15
- package/package.json +1 -1
- package/script/mod.d.ts +17 -8
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +51 -14
package/README.md
CHANGED
|
@@ -25,9 +25,14 @@ describe("something", () => {
|
|
|
25
25
|
|
|
26
26
|
adapter.addSetup(function*() {
|
|
27
27
|
/* do some setup. equivalent of beforeEach() */
|
|
28
|
-
/* contexts set here will be visible in the test
|
|
28
|
+
/* contexts set here will be visible in the test */
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
+
adapter.addOnetimeSetup(function*() {
|
|
32
|
+
/* do some setup that will only happen once. equivalent of beforeAll() */
|
|
33
|
+
/* contexts set here will be visible in the test */
|
|
34
|
+
});
|
|
35
|
+
|
|
31
36
|
it("does a thing", async () => {
|
|
32
37
|
await adapter.runTest(function*() {
|
|
33
38
|
/* ... the body of the test */
|
package/esm/mod.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Future, Operation, Scope } from "effection";
|
|
1
|
+
import type { Future, Operation, Result, Scope } from "effection";
|
|
2
2
|
export interface TestOperation {
|
|
3
3
|
(): Operation<void>;
|
|
4
4
|
}
|
|
@@ -17,11 +17,6 @@ export interface TestAdapter {
|
|
|
17
17
|
* ancestors. E.g. `All Tests > File System > write`
|
|
18
18
|
*/
|
|
19
19
|
readonly fullname: string;
|
|
20
|
-
/**
|
|
21
|
-
* Every test adapter has its own Effection `Scope` which holds the resources necessary
|
|
22
|
-
* to run this test.
|
|
23
|
-
*/
|
|
24
|
-
readonly scope: Scope;
|
|
25
20
|
/**
|
|
26
21
|
* A list of this test adapter and every adapter that it descends from.
|
|
27
22
|
*/
|
|
@@ -30,22 +25,36 @@ export interface TestAdapter {
|
|
|
30
25
|
* The setup operations that will be run by this test adapter. It only includes those
|
|
31
26
|
* setups that are associated with this adapter, not those of its ancestors.
|
|
32
27
|
*/
|
|
33
|
-
readonly
|
|
28
|
+
readonly setup: {
|
|
29
|
+
all: TestOperation[];
|
|
30
|
+
each: TestOperation[];
|
|
31
|
+
};
|
|
34
32
|
/**
|
|
35
33
|
* Add a setup operation to every test that is part of this adapter. In BDD integrations,
|
|
36
34
|
* this is usually called by `beforEach()`
|
|
37
35
|
*/
|
|
38
36
|
addSetup(op: TestOperation): void;
|
|
37
|
+
/**
|
|
38
|
+
* Add a setup operation that will run exactly once before any tests that are run in this
|
|
39
|
+
* adapter. In BDD integrations, this is usually called by beforeAll()
|
|
40
|
+
*/
|
|
41
|
+
addOnetimeSetup(op: TestOperation): void;
|
|
39
42
|
/**
|
|
40
43
|
* Actually run a test. This evaluates all setup operations, and then after those have completed
|
|
41
44
|
* it runs the body of the test itself.
|
|
42
45
|
*/
|
|
43
|
-
runTest(body: TestOperation): Future<void
|
|
46
|
+
runTest(body: TestOperation): Future<Result<void>>;
|
|
44
47
|
/**
|
|
45
48
|
* Teardown this test adapter and all of the task and resources that are running inside it.
|
|
46
49
|
* This basically destroys the Effection `Scope` associated with this adapter.
|
|
47
50
|
*/
|
|
48
51
|
destroy(): Future<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Used internally to prepare adapters to run test
|
|
54
|
+
*
|
|
55
|
+
* @ignore
|
|
56
|
+
*/
|
|
57
|
+
["@@init@@"](): Operation<Scope>;
|
|
49
58
|
}
|
|
50
59
|
export interface TestAdapterOptions {
|
|
51
60
|
/**
|
package/esm/mod.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":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od.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Err, Ok, run, suspend, useScope, withResolvers } from "effection";
|
|
2
2
|
const anonymousNames = (function* () {
|
|
3
3
|
let count = 1;
|
|
4
4
|
while (true) {
|
|
@@ -9,16 +9,18 @@ const anonymousNames = (function* () {
|
|
|
9
9
|
* Create a new test adapter with the given options.
|
|
10
10
|
*/
|
|
11
11
|
export function createTestAdapter(options = {}) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
const setup = {
|
|
13
|
+
all: [],
|
|
14
|
+
each: [],
|
|
15
|
+
};
|
|
16
|
+
const { parent, name = anonymousNames.next().value } = options;
|
|
17
|
+
let scope = undefined;
|
|
18
|
+
const adapter = {
|
|
16
19
|
parent,
|
|
17
20
|
name,
|
|
18
|
-
|
|
19
|
-
setups,
|
|
21
|
+
setup,
|
|
20
22
|
get lineage() {
|
|
21
|
-
|
|
23
|
+
const lineage = [adapter];
|
|
22
24
|
for (let current = parent; current; current = current.parent) {
|
|
23
25
|
lineage.unshift(current);
|
|
24
26
|
}
|
|
@@ -28,18 +30,53 @@ export function createTestAdapter(options = {}) {
|
|
|
28
30
|
return adapter.lineage.map((adapter) => adapter.name).join(" > ");
|
|
29
31
|
},
|
|
30
32
|
addSetup(op) {
|
|
31
|
-
|
|
33
|
+
setup.each.push(op);
|
|
34
|
+
},
|
|
35
|
+
addOnetimeSetup(op) {
|
|
36
|
+
setup.all.push(op);
|
|
32
37
|
},
|
|
33
38
|
runTest(op) {
|
|
34
|
-
return
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
return run(() => box(function* () {
|
|
40
|
+
const setups = adapter.lineage.reduce((all, adapter) => all.concat(adapter.setup.each), []);
|
|
41
|
+
let scope = yield* adapter["@@init@@"]();
|
|
42
|
+
let test = yield* scope.spawn(function* () {
|
|
43
|
+
for (const setup of setups) {
|
|
44
|
+
yield* setup();
|
|
45
|
+
}
|
|
46
|
+
yield* op();
|
|
47
|
+
});
|
|
48
|
+
yield* test;
|
|
49
|
+
}()));
|
|
50
|
+
},
|
|
51
|
+
// no-op that will be replaced once initialze
|
|
52
|
+
destroy: () => run(function* () { }),
|
|
53
|
+
*["@@init@@"]() {
|
|
54
|
+
if (scope) {
|
|
55
|
+
return scope;
|
|
56
|
+
}
|
|
57
|
+
let parentScope = parent
|
|
58
|
+
? yield* parent["@@init@@"]()
|
|
59
|
+
: yield* useScope();
|
|
60
|
+
let initialized = withResolvers();
|
|
61
|
+
let task = yield* parentScope.spawn(function* () {
|
|
62
|
+
scope = yield* useScope();
|
|
63
|
+
for (let op of setup.all) {
|
|
64
|
+
yield* op();
|
|
38
65
|
}
|
|
39
|
-
|
|
66
|
+
initialized.resolve(scope);
|
|
67
|
+
yield* suspend();
|
|
40
68
|
});
|
|
69
|
+
adapter.destroy = () => run(task.halt);
|
|
70
|
+
return yield* initialized.operation;
|
|
41
71
|
},
|
|
42
|
-
destroy,
|
|
43
72
|
};
|
|
44
73
|
return adapter;
|
|
45
74
|
}
|
|
75
|
+
function* box(op) {
|
|
76
|
+
try {
|
|
77
|
+
return Ok(yield* op);
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
return Err(error);
|
|
81
|
+
}
|
|
82
|
+
}
|
package/package.json
CHANGED
package/script/mod.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Future, Operation, Scope } from "effection";
|
|
1
|
+
import type { Future, Operation, Result, Scope } from "effection";
|
|
2
2
|
export interface TestOperation {
|
|
3
3
|
(): Operation<void>;
|
|
4
4
|
}
|
|
@@ -17,11 +17,6 @@ export interface TestAdapter {
|
|
|
17
17
|
* ancestors. E.g. `All Tests > File System > write`
|
|
18
18
|
*/
|
|
19
19
|
readonly fullname: string;
|
|
20
|
-
/**
|
|
21
|
-
* Every test adapter has its own Effection `Scope` which holds the resources necessary
|
|
22
|
-
* to run this test.
|
|
23
|
-
*/
|
|
24
|
-
readonly scope: Scope;
|
|
25
20
|
/**
|
|
26
21
|
* A list of this test adapter and every adapter that it descends from.
|
|
27
22
|
*/
|
|
@@ -30,22 +25,36 @@ export interface TestAdapter {
|
|
|
30
25
|
* The setup operations that will be run by this test adapter. It only includes those
|
|
31
26
|
* setups that are associated with this adapter, not those of its ancestors.
|
|
32
27
|
*/
|
|
33
|
-
readonly
|
|
28
|
+
readonly setup: {
|
|
29
|
+
all: TestOperation[];
|
|
30
|
+
each: TestOperation[];
|
|
31
|
+
};
|
|
34
32
|
/**
|
|
35
33
|
* Add a setup operation to every test that is part of this adapter. In BDD integrations,
|
|
36
34
|
* this is usually called by `beforEach()`
|
|
37
35
|
*/
|
|
38
36
|
addSetup(op: TestOperation): void;
|
|
37
|
+
/**
|
|
38
|
+
* Add a setup operation that will run exactly once before any tests that are run in this
|
|
39
|
+
* adapter. In BDD integrations, this is usually called by beforeAll()
|
|
40
|
+
*/
|
|
41
|
+
addOnetimeSetup(op: TestOperation): void;
|
|
39
42
|
/**
|
|
40
43
|
* Actually run a test. This evaluates all setup operations, and then after those have completed
|
|
41
44
|
* it runs the body of the test itself.
|
|
42
45
|
*/
|
|
43
|
-
runTest(body: TestOperation): Future<void
|
|
46
|
+
runTest(body: TestOperation): Future<Result<void>>;
|
|
44
47
|
/**
|
|
45
48
|
* Teardown this test adapter and all of the task and resources that are running inside it.
|
|
46
49
|
* This basically destroys the Effection `Scope` associated with this adapter.
|
|
47
50
|
*/
|
|
48
51
|
destroy(): Future<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Used internally to prepare adapters to run test
|
|
54
|
+
*
|
|
55
|
+
* @ignore
|
|
56
|
+
*/
|
|
57
|
+
["@@init@@"](): Operation<Scope>;
|
|
49
58
|
}
|
|
50
59
|
export interface TestAdapterOptions {
|
|
51
60
|
/**
|
package/script/mod.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src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,CAAC,EAAE,WAAW,CAAC;CACtB;AASD;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,GAAE,kBAAuB,GAC/B,WAAW,CAiFb"}
|
package/script/mod.js
CHANGED
|
@@ -12,16 +12,18 @@ const anonymousNames = (function* () {
|
|
|
12
12
|
* Create a new test adapter with the given options.
|
|
13
13
|
*/
|
|
14
14
|
function createTestAdapter(options = {}) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
const setup = {
|
|
16
|
+
all: [],
|
|
17
|
+
each: [],
|
|
18
|
+
};
|
|
19
|
+
const { parent, name = anonymousNames.next().value } = options;
|
|
20
|
+
let scope = undefined;
|
|
21
|
+
const adapter = {
|
|
19
22
|
parent,
|
|
20
23
|
name,
|
|
21
|
-
|
|
22
|
-
setups,
|
|
24
|
+
setup,
|
|
23
25
|
get lineage() {
|
|
24
|
-
|
|
26
|
+
const lineage = [adapter];
|
|
25
27
|
for (let current = parent; current; current = current.parent) {
|
|
26
28
|
lineage.unshift(current);
|
|
27
29
|
}
|
|
@@ -31,18 +33,53 @@ function createTestAdapter(options = {}) {
|
|
|
31
33
|
return adapter.lineage.map((adapter) => adapter.name).join(" > ");
|
|
32
34
|
},
|
|
33
35
|
addSetup(op) {
|
|
34
|
-
|
|
36
|
+
setup.each.push(op);
|
|
37
|
+
},
|
|
38
|
+
addOnetimeSetup(op) {
|
|
39
|
+
setup.all.push(op);
|
|
35
40
|
},
|
|
36
41
|
runTest(op) {
|
|
37
|
-
return
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
42
|
+
return (0, effection_1.run)(() => box(function* () {
|
|
43
|
+
const setups = adapter.lineage.reduce((all, adapter) => all.concat(adapter.setup.each), []);
|
|
44
|
+
let scope = yield* adapter["@@init@@"]();
|
|
45
|
+
let test = yield* scope.spawn(function* () {
|
|
46
|
+
for (const setup of setups) {
|
|
47
|
+
yield* setup();
|
|
48
|
+
}
|
|
49
|
+
yield* op();
|
|
50
|
+
});
|
|
51
|
+
yield* test;
|
|
52
|
+
}()));
|
|
53
|
+
},
|
|
54
|
+
// no-op that will be replaced once initialze
|
|
55
|
+
destroy: () => (0, effection_1.run)(function* () { }),
|
|
56
|
+
*["@@init@@"]() {
|
|
57
|
+
if (scope) {
|
|
58
|
+
return scope;
|
|
59
|
+
}
|
|
60
|
+
let parentScope = parent
|
|
61
|
+
? yield* parent["@@init@@"]()
|
|
62
|
+
: yield* (0, effection_1.useScope)();
|
|
63
|
+
let initialized = (0, effection_1.withResolvers)();
|
|
64
|
+
let task = yield* parentScope.spawn(function* () {
|
|
65
|
+
scope = yield* (0, effection_1.useScope)();
|
|
66
|
+
for (let op of setup.all) {
|
|
67
|
+
yield* op();
|
|
41
68
|
}
|
|
42
|
-
|
|
69
|
+
initialized.resolve(scope);
|
|
70
|
+
yield* (0, effection_1.suspend)();
|
|
43
71
|
});
|
|
72
|
+
adapter.destroy = () => (0, effection_1.run)(task.halt);
|
|
73
|
+
return yield* initialized.operation;
|
|
44
74
|
},
|
|
45
|
-
destroy,
|
|
46
75
|
};
|
|
47
76
|
return adapter;
|
|
48
77
|
}
|
|
78
|
+
function* box(op) {
|
|
79
|
+
try {
|
|
80
|
+
return (0, effection_1.Ok)(yield* op);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
return (0, effection_1.Err)(error);
|
|
84
|
+
}
|
|
85
|
+
}
|