@effectionx/test-adapter 0.4.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 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
@@ -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,12 +25,20 @@ 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 setups: TestOperation[];
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.
@@ -46,6 +49,12 @@ export interface TestAdapter {
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,MAAM,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAGlE,MAAM,WAAW,aAAa;IAC5B,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC;CACrB;AAED,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;IAE9B;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAE1B;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IAEtB;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC;IAErC;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,aAAa,EAAE,CAAC;IAEjC;;;OAGG;IACH,QAAQ,CAAC,EAAE,EAAE,aAAa,GAAG,IAAI,CAAC;IAElC;;;OAGG;IACH,OAAO,CAAC,IAAI,EAAE,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;IAEnD;;;OAGG;IACH,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC;CACzB;AAED,MAAM,WAAW,kBAAkB;IACjC;;;;;OAKG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AASD;;GAEG;AACH,wBAAgB,iBAAiB,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BAAiB,CAC/B,OAAO,GAAE,kBAAuB,GAC/B,WAAW,CAiFb"}
package/esm/mod.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createScope, Err, Ok } from "effection";
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,14 +9,16 @@ const anonymousNames = (function* () {
9
9
  * Create a new test adapter with the given options.
10
10
  */
11
11
  export function createTestAdapter(options = {}) {
12
- const setups = [];
12
+ const setup = {
13
+ all: [],
14
+ each: [],
15
+ };
13
16
  const { parent, name = anonymousNames.next().value } = options;
14
- const [scope, destroy] = createScope(parent?.scope);
17
+ let scope = undefined;
15
18
  const adapter = {
16
19
  parent,
17
20
  name,
18
- scope,
19
- setups,
21
+ setup,
20
22
  get lineage() {
21
23
  const lineage = [adapter];
22
24
  for (let current = parent; current; current = current.parent) {
@@ -28,24 +30,53 @@ export function createTestAdapter(options = {}) {
28
30
  return adapter.lineage.map((adapter) => adapter.name).join(" > ");
29
31
  },
30
32
  addSetup(op) {
31
- setups.push(op);
33
+ setup.each.push(op);
34
+ },
35
+ addOnetimeSetup(op) {
36
+ setup.all.push(op);
32
37
  },
33
38
  runTest(op) {
34
- return scope.run(function* () {
35
- const allSetups = adapter.lineage.reduce((all, adapter) => all.concat(adapter.setups), []);
36
- try {
37
- for (const setup of allSetups) {
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) {
38
44
  yield* setup();
39
45
  }
40
46
  yield* op();
41
- return Ok(void 0);
42
- }
43
- catch (error) {
44
- return Err(error);
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();
45
65
  }
66
+ initialized.resolve(scope);
67
+ yield* suspend();
46
68
  });
69
+ adapter.destroy = () => run(task.halt);
70
+ return yield* initialized.operation;
47
71
  },
48
- destroy,
49
72
  };
50
73
  return adapter;
51
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effectionx/test-adapter",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "author": "engineering@frontside.com",
5
5
  "repository": {
6
6
  "type": "git",
package/script/mod.d.ts CHANGED
@@ -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,12 +25,20 @@ 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 setups: TestOperation[];
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.
@@ -46,6 +49,12 @@ export interface TestAdapter {
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
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAGlE,MAAM,WAAW,aAAa;IAC5B,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC;CACrB;AAED,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;IAE9B;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAE1B;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IAEtB;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC;IAErC;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,aAAa,EAAE,CAAC;IAEjC;;;OAGG;IACH,QAAQ,CAAC,EAAE,EAAE,aAAa,GAAG,IAAI,CAAC;IAElC;;;OAGG;IACH,OAAO,CAAC,IAAI,EAAE,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;IAEnD;;;OAGG;IACH,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC;CACzB;AAED,MAAM,WAAW,kBAAkB;IACjC;;;;;OAKG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,MAAM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H,MAAM,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,14 +12,16 @@ const anonymousNames = (function* () {
12
12
  * Create a new test adapter with the given options.
13
13
  */
14
14
  function createTestAdapter(options = {}) {
15
- const setups = [];
15
+ const setup = {
16
+ all: [],
17
+ each: [],
18
+ };
16
19
  const { parent, name = anonymousNames.next().value } = options;
17
- const [scope, destroy] = (0, effection_1.createScope)(parent?.scope);
20
+ let scope = undefined;
18
21
  const adapter = {
19
22
  parent,
20
23
  name,
21
- scope,
22
- setups,
24
+ setup,
23
25
  get lineage() {
24
26
  const lineage = [adapter];
25
27
  for (let current = parent; current; current = current.parent) {
@@ -31,24 +33,53 @@ function createTestAdapter(options = {}) {
31
33
  return adapter.lineage.map((adapter) => adapter.name).join(" > ");
32
34
  },
33
35
  addSetup(op) {
34
- setups.push(op);
36
+ setup.each.push(op);
37
+ },
38
+ addOnetimeSetup(op) {
39
+ setup.all.push(op);
35
40
  },
36
41
  runTest(op) {
37
- return scope.run(function* () {
38
- const allSetups = adapter.lineage.reduce((all, adapter) => all.concat(adapter.setups), []);
39
- try {
40
- for (const setup of allSetups) {
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) {
41
47
  yield* setup();
42
48
  }
43
49
  yield* op();
44
- return (0, effection_1.Ok)(void 0);
45
- }
46
- catch (error) {
47
- return (0, effection_1.Err)(error);
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();
48
68
  }
69
+ initialized.resolve(scope);
70
+ yield* (0, effection_1.suspend)();
49
71
  });
72
+ adapter.destroy = () => (0, effection_1.run)(task.halt);
73
+ return yield* initialized.operation;
50
74
  },
51
- destroy,
52
75
  };
53
76
  return adapter;
54
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
+ }