@yaks/testing 0.0.0-stage → 0.2.36

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.
Files changed (93) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +427 -2
  3. package/_dist/assert.d.ts +40 -0
  4. package/_dist/assert.d.ts.map +1 -0
  5. package/_dist/choose.d.ts +39 -0
  6. package/_dist/choose.d.ts.map +1 -0
  7. package/_dist/clock.d.ts +8 -0
  8. package/_dist/clock.d.ts.map +1 -0
  9. package/_dist/deps.d.ts +113 -0
  10. package/_dist/deps.d.ts.map +1 -0
  11. package/_dist/examples.d.ts +69 -0
  12. package/_dist/examples.d.ts.map +1 -0
  13. package/_dist/find.d.ts +23 -0
  14. package/_dist/find.d.ts.map +1 -0
  15. package/_dist/kept.d.ts +27 -0
  16. package/_dist/kept.d.ts.map +1 -0
  17. package/_dist/lend.d.ts +15 -0
  18. package/_dist/lend.d.ts.map +1 -0
  19. package/_dist/main.d.ts +2 -0
  20. package/_dist/main.d.ts.map +1 -0
  21. package/_dist/mod.d.ts +45 -0
  22. package/_dist/mod.d.ts.map +1 -0
  23. package/_dist/names.d.ts +31 -0
  24. package/_dist/names.d.ts.map +1 -0
  25. package/_dist/own.d.ts +25 -0
  26. package/_dist/own.d.ts.map +1 -0
  27. package/_dist/reads.d.ts +8 -0
  28. package/_dist/reads.d.ts.map +1 -0
  29. package/_dist/suite.d.ts +82 -0
  30. package/_dist/suite.d.ts.map +1 -0
  31. package/_dist/typist.d.ts +24 -0
  32. package/_dist/typist.d.ts.map +1 -0
  33. package/_dist/wait.d.ts +21 -0
  34. package/_dist/wait.d.ts.map +1 -0
  35. package/assert.js +114 -0
  36. package/assert.js.map +1 -0
  37. package/assert.ts +143 -0
  38. package/choose.js +197 -0
  39. package/choose.js.map +1 -0
  40. package/choose.ts +327 -0
  41. package/clock.js +45 -0
  42. package/clock.js.map +1 -0
  43. package/clock.ts +60 -0
  44. package/deno.json +1 -0
  45. package/deps.js +868 -0
  46. package/deps.js.map +1 -0
  47. package/deps.ts +1004 -0
  48. package/drive.js +43 -0
  49. package/drive.js.map +1 -0
  50. package/drive.ts +41 -0
  51. package/examples.js +279 -0
  52. package/examples.js.map +1 -0
  53. package/examples.ts +380 -0
  54. package/find.js +86 -0
  55. package/find.js.map +1 -0
  56. package/find.ts +102 -0
  57. package/kept.js +128 -0
  58. package/kept.js.map +1 -0
  59. package/kept.ts +146 -0
  60. package/lend.js +30 -0
  61. package/lend.js.map +1 -0
  62. package/lend.ts +31 -0
  63. package/load.js +106 -0
  64. package/load.js.map +1 -0
  65. package/load.ts +139 -0
  66. package/main.js +214 -0
  67. package/main.js.map +1 -0
  68. package/main.ts +217 -0
  69. package/mod.js +45 -0
  70. package/mod.js.map +1 -0
  71. package/mod.ts +55 -0
  72. package/names.js +209 -0
  73. package/names.js.map +1 -0
  74. package/names.ts +204 -0
  75. package/own.js +109 -0
  76. package/own.js.map +1 -0
  77. package/own.ts +136 -0
  78. package/package.json +56 -4
  79. package/reads.js +88 -0
  80. package/reads.js.map +1 -0
  81. package/reads.ts +98 -0
  82. package/run.js +306 -0
  83. package/run.js.map +1 -0
  84. package/run.ts +405 -0
  85. package/suite.js +191 -0
  86. package/suite.js.map +1 -0
  87. package/suite.ts +224 -0
  88. package/typist.js +74 -0
  89. package/typist.js.map +1 -0
  90. package/typist.ts +79 -0
  91. package/wait.js +34 -0
  92. package/wait.js.map +1 -0
  93. package/wait.ts +60 -0
package/main.js ADDED
@@ -0,0 +1,214 @@
1
+ #!/usr/bin/env -S deno run -A
2
+ import * as io from "@yaks/file/io";
3
+ import * as system from "@yaks/process/system";
4
+ // The runner: every test module and example page it is given, loaded into
5
+ // this one runtime a file at a time, each file's tests run as it loads.
6
+ //
7
+ // main.ts [--tag=t]... [--platform=p] [--all] [--also=path]... [--keys=file]
8
+ // [--timeout=ms] [--times=dir] [--rest=file] [--full=bytes] [--part=n]
9
+ // [--verbose] path...
10
+ //
11
+ // A path is a test module (`*_test.ts`), a page whose examples run (a module
12
+ // or a Markdown page), or a directory holding them. `--tag` keeps the tests
13
+ // carrying every tag named, and `--platform` is a tag every test here
14
+ // carries. A file whose pass is kept, nothing it depends on changed since
15
+ // (./deps.ts), is left out unless `--all` is given; `--also` names what every
16
+ // file depends on beside its own graph. `--keys` names a JSON file holding
17
+ // the key of each file given, from a caller that chose them already
18
+ // (./choose.ts): every file given runs. The runner observes file reads and
19
+ // module-loading boundaries as it runs
20
+ // (./reads.ts), and keeps a pass by that too.
21
+ //
22
+ // A test that has not ended within `--timeout` of its own time fails, and the
23
+ // run goes on (./run.ts `one`).
24
+ // `--times` writes what each test and each file's load took, and the proof
25
+ // of each file the run remembered as passed, to `<dir>/<platform>.json`, or
26
+ // `<dir>/<platform>-<n>.json` for `--part=n`.
27
+ //
28
+ // A runtime keeps every module it loaded, and whatever their tests left
29
+ // reachable, until it exits. With `--rest`, once it holds more than FULL, or
30
+ // the fewer bytes `--full` names (`full` below), it loads no more files and writes those it
31
+ // did not reach to that file as a JSON list, for a fresh runtime to run
32
+ // (bin/test.ts).
33
+ //
34
+ // Each test is said as it runs, a failure's error right after it, and the
35
+ // closing report says every failure again; each error is cut short (./run.ts
36
+ // `brief`) unless `--verbose`. The exit code is 1 when a test failed.
37
+ import { choose } from "./choose.js";
38
+ import { drive } from "./drive.js";
39
+ import { FACTS, note, proof, remember, survey, tally } from "./deps.js";
40
+ import { discover, tested } from "./find.js";
41
+ import { examples } from "./load.js";
42
+ import { observe } from "./reads.js";
43
+ import { release, summary, timed } from "./run.js";
44
+ import { collect, from, test } from "./suite.js";
45
+ import { tick } from "./wait.js";
46
+ // Before anything a test could start inherits it.
47
+ system.env.delete('DENO_AUDIT_PERMISSIONS');
48
+ let observation = observe();
49
+ let FLAGS = /^--(tag|platform|also|keys|timeout|times|rest|part|full)=(.*)$|^--(all|verbose)$/;
50
+ let odd = system.args.find((a)=>a.startsWith('--') && !FLAGS.test(a));
51
+ if (odd) {
52
+ console.error(`main.ts: the runner has no flag ${odd}`);
53
+ system.exit(2);
54
+ }
55
+ let flag = (name)=>system.args.map((a)=>a.match(FLAGS)).filter((m)=>m?.[1] == name).map((m)=>m[2]);
56
+ let platform = flag('platform').at(-1) ?? '';
57
+ // Every test here carries the platform's tag, so naming it picks them all.
58
+ let tags = flag('tag').flatMap((t)=>t.split(',')).filter((t)=>t && t != platform);
59
+ let timeout = Number(flag('timeout').at(-1) ?? 50_000);
60
+ if (!Number.isFinite(timeout) || timeout < 1) {
61
+ throw new Error('invalid test timeout');
62
+ }
63
+ let all = system.args.includes('--all');
64
+ let verbose = system.args.includes('--verbose');
65
+ let paths = system.args.filter((a)=>!a.startsWith('--'));
66
+ let given = flag('keys').at(-1);
67
+ let rest = flag('rest').at(-1);
68
+ let part = flag('part').at(-1);
69
+ let GIB = 1024 ** 3;
70
+ let FULL = Number(flag('full').at(-1) ?? GIB);
71
+ // Collect when the runtime grows, allow finalizers to close native handles,
72
+ // and give back the memory they free (@yaks/process `trim`). Only a runner
73
+ // with an explicit handoff releases shared fixtures before the last file.
74
+ let sweep = globalThis.gc;
75
+ let releases = [];
76
+ let sweepAt = Math.min(GIB, FULL);
77
+ let trim = async ()=>{
78
+ sweep?.();
79
+ await tick();
80
+ system.trim();
81
+ return system.memoryUsage().rss;
82
+ };
83
+ let full = async ()=>{
84
+ if (system.memoryUsage().rss <= sweepAt) return false;
85
+ let kept = await trim();
86
+ if (rest && kept > FULL) {
87
+ releases.push(...await release(timeout, verbose));
88
+ kept = await trim();
89
+ }
90
+ // Native SQLite pages do not count toward JavaScript heap pressure. Give
91
+ // unreachable fixtures a chance to free them, then wait for memory to
92
+ // grow again before collecting.
93
+ sweepAt = rest ? Math.min(FULL, kept + GIB / 4) : Math.max(GIB, kept + GIB / 4);
94
+ return !!rest && kept > FULL;
95
+ };
96
+ let started = performance.now();
97
+ let here = (path)=>new URL(path, `file://${system.cwd()}/`).href;
98
+ let code = (path)=>/\.[mc]?[jt]sx?$/.test(path);
99
+ // The files given, or found under the paths, and the key of each.
100
+ let { tests: found, pages: shown, parsed } = given ? {
101
+ tests: paths.filter(tested),
102
+ pages: paths.filter((p)=>!tested(p)),
103
+ parsed: undefined
104
+ } : await discover(paths);
105
+ let chosen = given ? {
106
+ keys: new Map(Object.entries(JSON.parse(await io.readTextFile(given)))),
107
+ run: [
108
+ ...found,
109
+ ...shown
110
+ ]
111
+ } : await choose([
112
+ ...found,
113
+ ...shown
114
+ ], {
115
+ all,
116
+ observed: true,
117
+ parsed,
118
+ kind: ()=>platform,
119
+ also: ()=>[
120
+ 'deno.json',
121
+ 'deno.jsonc',
122
+ 'deno.lock',
123
+ ...flag('also')
124
+ ],
125
+ runner: [
126
+ new URL('./run.ts', import.meta.url).pathname.replace(`${system.cwd()}/`, '')
127
+ ]
128
+ });
129
+ let key = chosen.keys;
130
+ let running = new Set(chosen.run);
131
+ let modules = found.filter((m)=>running.has(m));
132
+ let pages = shown.filter((p)=>running.has(p));
133
+ let left = found.length + shown.length - running.size;
134
+ let planned = performance.now() - started;
135
+ let collected = collect();
136
+ let picked = (t)=>tags.every((tag)=>t.tags.includes(tag));
137
+ let load = async (path)=>{
138
+ let at = collected.length;
139
+ observation.mark(path);
140
+ from(path);
141
+ try {
142
+ await (tested(path) ? import(here(path)) : examples(path, parsed?.get(path)));
143
+ } catch (error) {
144
+ test(`${path} loads`, ()=>{
145
+ throw error;
146
+ });
147
+ }
148
+ return collected.splice(at).filter(picked);
149
+ };
150
+ // Examples load before test modules, while the runtime's import graph is small.
151
+ let { outcomes, loading, loads, took, rest: unreached } = await drive([
152
+ ...pages,
153
+ ...modules
154
+ ], load, {
155
+ timeout,
156
+ verbose,
157
+ full
158
+ });
159
+ // What a file kept for those after it ends with the runtime's files, as it
160
+ // would have ended with the runtime's memory.
161
+ outcomes.push(...releases, ...await release(timeout, verbose));
162
+ console.log('recording test results');
163
+ // A file passed when every test it declared ran and passed; a run that
164
+ // picked by tag ran only some. Its pass is kept by its proof: its key, and
165
+ // what it read as it ran, as that is now.
166
+ let failed = new Set(outcomes.filter((o)=>!o.ok).map((o)=>o.test.file));
167
+ let passing = tags.length ? [] : [
168
+ ...modules,
169
+ ...pages
170
+ ].filter((p)=>p in took && !failed.has(p) && key.has(p));
171
+ tally(took);
172
+ let seen = observation.reads();
173
+ observation.close();
174
+ let read = (p)=>seen && (seen.get(p) ?? []);
175
+ let s = await survey([
176
+ ...new Set(passing.flatMap((p)=>read(p) ?? []).filter(code))
177
+ ], undefined, FACTS);
178
+ let remembered = s ? Object.fromEntries(await Promise.all(passing.map(async (p)=>[
179
+ p,
180
+ await proof(s, key.get(p), read(p), true)
181
+ ]))) : {};
182
+ remember(Object.values(remembered));
183
+ if (seen && s) {
184
+ note(Object.fromEntries(passing.map((p)=>[
185
+ p,
186
+ read(p)
187
+ ])), undefined, Object.fromEntries(passing.map((p)=>[
188
+ p,
189
+ key.get(p)
190
+ ])));
191
+ }
192
+ let wall = performance.now() - started;
193
+ summary(outcomes, wall, {
194
+ left,
195
+ planned,
196
+ loading
197
+ }, verbose);
198
+ let times = flag('times').at(-1);
199
+ if (times) {
200
+ await io.mkdir(times, {
201
+ recursive: true
202
+ });
203
+ await io.writeTextFile(`${times}/${platform || 'tests'}${part ? `-${part}` : ''}.json`, JSON.stringify(timed(outcomes, {
204
+ wall,
205
+ planned,
206
+ loading,
207
+ loads,
208
+ remembered,
209
+ left
210
+ })));
211
+ }
212
+ if (rest) await io.writeTextFile(rest, JSON.stringify(unreached));
213
+ system.exit(outcomes.some((o)=>!o.ok) ? 1 : 0);
214
+ //# sourceMappingURL=main.js.map
package/main.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.js","sources":["./main.ts"],"names":[],"mappings":";AACA,YAAY,6BAAkC;AAC9C,YAAY,wCAA6C;AACzD,0EAA0E;AAC1E,wEAAwE;AACxE,EAAE;AACF,+EAA+E;AAC/E,2EAA2E;AAC3E,0BAA0B;AAC1B,EAAE;AACF,6EAA6E;AAC7E,4EAA4E;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;KACK,IAAI,IAAI,QAAQ,OAAO,CAAC,CAAC,IAAM,KAAK,MAAM,EAAE,EAAE,MAAM,CAAC;CACzD,EACD,WACA;AAEF,IAAI,aAAqC,IACrC,OAAO,WAAW,CAClB,MAAM,QAAQ,GAAG,CACf,QAAQ,GAAG,CAAC,OAAO,IAAM;IAAC;IAAG,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,IAAK,KAAK,IAAI;GAAM,MAG1E,CAAC;AACL,SAAS,OAAO,MAAM,CAAC;AACvB,IAAI,QAAQ,GAAG;EACb,KACE,OAAO,WAAW,CAAC,QAAQ,GAAG,CAAC,CAAC,IAAM;MAAC;MAAG,KAAK;KAAI,IACnD,WACA,OAAO,WAAW,CAAC,QAAQ,GAAG,CAAC,CAAC,IAAM;MAAC;MAAG,IAAI,GAAG,CAAC;KAAI;AAE1D;AACA,IAAI,OAAO,YAAY,GAAG,KAAK;AAC/B,QAAQ,UAAU,MAAM;EAAE;EAAM;EAAS;AAAQ,GAAG;AACpD,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC,CAAC;AAC9B,IAAI,OAAO;EACT,MAAM,GAAG,KAAK,CAAC,OAAO;IAAE,WAAW;EAAK;EACxC,MAAM,GAAG,aAAa,CACpB,GAAG,MAAM,CAAC,EAAE,YAAY,UAAU,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,GAAG,KAAK,CAAC,EAC/D,KAAK,SAAS,CACZ,MAAM,UAAU;IAAE;IAAM;IAAS;IAAS;IAAO;IAAY;EAAK;AAGxE;AACA,IAAI,MAAM,MAAM,GAAG,aAAa,CAAC,MAAM,KAAK,SAAS,CAAC;AACtD,OAAO,IAAI,CAAC,SAAS,IAAI,CAAC,CAAC,IAAM,CAAC,EAAE,EAAE,IAAI,IAAI"}
package/main.ts ADDED
@@ -0,0 +1,217 @@
1
+ #!/usr/bin/env -S deno run -A
2
+ import * as io from '@yaks/file/io'
3
+ import * as system from '@yaks/process/system'
4
+ // The runner: every test module and example page it is given, loaded into
5
+ // this one runtime a file at a time, each file's tests run as it loads.
6
+ //
7
+ // main.ts [--tag=t]... [--platform=p] [--all] [--also=path]... [--keys=file]
8
+ // [--timeout=ms] [--times=dir] [--rest=file] [--full=bytes] [--part=n]
9
+ // [--verbose] path...
10
+ //
11
+ // A path is a test module (`*_test.ts`), a page whose examples run (a module
12
+ // or a Markdown page), or a directory holding them. `--tag` keeps the tests
13
+ // carrying every tag named, and `--platform` is a tag every test here
14
+ // carries. A file whose pass is kept, nothing it depends on changed since
15
+ // (./deps.ts), is left out unless `--all` is given; `--also` names what every
16
+ // file depends on beside its own graph. `--keys` names a JSON file holding
17
+ // the key of each file given, from a caller that chose them already
18
+ // (./choose.ts): every file given runs. The runner observes file reads and
19
+ // module-loading boundaries as it runs
20
+ // (./reads.ts), and keeps a pass by that too.
21
+ //
22
+ // A test that has not ended within `--timeout` of its own time fails, and the
23
+ // run goes on (./run.ts `one`).
24
+ // `--times` writes what each test and each file's load took, and the proof
25
+ // of each file the run remembered as passed, to `<dir>/<platform>.json`, or
26
+ // `<dir>/<platform>-<n>.json` for `--part=n`.
27
+ //
28
+ // A runtime keeps every module it loaded, and whatever their tests left
29
+ // reachable, until it exits. With `--rest`, once it holds more than FULL, or
30
+ // the fewer bytes `--full` names (`full` below), it loads no more files and writes those it
31
+ // did not reach to that file as a JSON list, for a fresh runtime to run
32
+ // (bin/test.ts).
33
+ //
34
+ // Each test is said as it runs, a failure's error right after it, and the
35
+ // closing report says every failure again; each error is cut short (./run.ts
36
+ // `brief`) unless `--verbose`. The exit code is 1 when a test failed.
37
+ import { choose } from './choose.js'
38
+ import { drive } from './drive.js'
39
+ import { FACTS, note, proof, remember, survey, tally } from './deps.js'
40
+ import { discover, tested } from './find.js'
41
+ import { examples } from './load.js'
42
+ import { observe } from './reads.js'
43
+ import { type Outcome, release, summary, timed } from './run.js'
44
+ import { collect, from, type Test, test } from './suite.js'
45
+ import { tick } from './wait.js'
46
+
47
+ // Before anything a test could start inherits it.
48
+ system.env.delete('DENO_AUDIT_PERMISSIONS')
49
+ let observation = observe()
50
+
51
+ let FLAGS =
52
+ /^--(tag|platform|also|keys|timeout|times|rest|part|full)=(.*)$|^--(all|verbose)$/
53
+ let odd = system.args.find((a) => a.startsWith('--') && !FLAGS.test(a))
54
+ if (odd) {
55
+ console.error(`main.ts: the runner has no flag ${odd}`)
56
+ system.exit(2)
57
+ }
58
+ let flag = (name: string) =>
59
+ system.args.map((a) => a.match(FLAGS)).filter((m) => m?.[1] == name)
60
+ .map((m) => m![2])
61
+ let platform = flag('platform').at(-1) ?? ''
62
+ // Every test here carries the platform's tag, so naming it picks them all.
63
+ let tags = flag('tag').flatMap((t) => t.split(','))
64
+ .filter((t) => t && t != platform)
65
+ let timeout = Number(flag('timeout').at(-1) ?? 50_000)
66
+ if (!Number.isFinite(timeout) || timeout < 1) {
67
+ throw new Error('invalid test timeout')
68
+ }
69
+ let all = system.args.includes('--all')
70
+ let verbose = system.args.includes('--verbose')
71
+ let paths = system.args.filter((a) => !a.startsWith('--'))
72
+ let given = flag('keys').at(-1)
73
+ let rest = flag('rest').at(-1)
74
+ let part = flag('part').at(-1)
75
+ let GIB = 1024 ** 3
76
+ let FULL = Number(flag('full').at(-1) ?? GIB)
77
+ // Collect when the runtime grows, allow finalizers to close native handles,
78
+ // and give back the memory they free (@yaks/process `trim`). Only a runner
79
+ // with an explicit handoff releases shared fixtures before the last file.
80
+ let sweep = (globalThis as { gc?: () => void }).gc
81
+ let releases: Outcome[] = []
82
+ let sweepAt = Math.min(GIB, FULL)
83
+ let trim = async () => {
84
+ sweep?.()
85
+ await tick()
86
+ system.trim()
87
+ return system.memoryUsage().rss
88
+ }
89
+ let full = async () => {
90
+ if (system.memoryUsage().rss <= sweepAt) return false
91
+ let kept = await trim()
92
+ if (rest && kept > FULL) {
93
+ releases.push(...await release(timeout, verbose))
94
+ kept = await trim()
95
+ }
96
+ // Native SQLite pages do not count toward JavaScript heap pressure. Give
97
+ // unreachable fixtures a chance to free them, then wait for memory to
98
+ // grow again before collecting.
99
+ sweepAt = rest
100
+ ? Math.min(FULL, kept + GIB / 4)
101
+ : Math.max(GIB, kept + GIB / 4)
102
+ return !!rest && kept > FULL
103
+ }
104
+ let started = performance.now()
105
+ let here = (path: string) => new URL(path, `file://${system.cwd()}/`).href
106
+ let code = (path: string) => /\.[mc]?[jt]sx?$/.test(path)
107
+
108
+ // The files given, or found under the paths, and the key of each.
109
+ let { tests: found, pages: shown, parsed } = given
110
+ ? {
111
+ tests: paths.filter(tested),
112
+ pages: paths.filter((p) => !tested(p)),
113
+ parsed: undefined,
114
+ }
115
+ : await discover(paths)
116
+ let chosen = given
117
+ ? {
118
+ keys: new Map(Object.entries(JSON.parse(await io.readTextFile(given)))),
119
+ run: [...found, ...shown],
120
+ }
121
+ : await choose([...found, ...shown], {
122
+ all,
123
+ observed: true,
124
+ parsed,
125
+ kind: () => platform,
126
+ also: () => ['deno.json', 'deno.jsonc', 'deno.lock', ...flag('also')],
127
+ runner: [
128
+ new URL('./run.ts', import.meta.url).pathname.replace(
129
+ `${system.cwd()}/`,
130
+ '',
131
+ ),
132
+ ],
133
+ })
134
+ let key = chosen.keys as Map<string, string>
135
+ let running = new Set(chosen.run)
136
+ let modules = found.filter((m) => running.has(m))
137
+ let pages = shown.filter((p) => running.has(p))
138
+ let left = found.length + shown.length - running.size
139
+
140
+ let planned = performance.now() - started
141
+ let collected = collect()
142
+ let picked = (t: Test) => tags.every((tag) => t.tags.includes(tag))
143
+ let load = async (path: string) => {
144
+ let at = collected.length
145
+ observation.mark(path)
146
+ from(path)
147
+ try {
148
+ await (tested(path)
149
+ ? import(here(path))
150
+ : examples(path, parsed?.get(path)))
151
+ } catch (error) {
152
+ test(`${path} loads`, () => {
153
+ throw error
154
+ })
155
+ }
156
+ return collected.splice(at).filter(picked)
157
+ }
158
+ // Examples load before test modules, while the runtime's import graph is small.
159
+ let { outcomes, loading, loads, took, rest: unreached } = await drive(
160
+ [...pages, ...modules],
161
+ load,
162
+ { timeout, verbose, full },
163
+ )
164
+ // What a file kept for those after it ends with the runtime's files, as it
165
+ // would have ended with the runtime's memory.
166
+ outcomes.push(...releases, ...await release(timeout, verbose))
167
+ console.log('recording test results')
168
+
169
+ // A file passed when every test it declared ran and passed; a run that
170
+ // picked by tag ran only some. Its pass is kept by its proof: its key, and
171
+ // what it read as it ran, as that is now.
172
+ let failed = new Set(outcomes.filter((o) => !o.ok).map((o) => o.test.file))
173
+ let passing = tags.length
174
+ ? []
175
+ : [...modules, ...pages].filter((p) =>
176
+ p in took && !failed.has(p) && key.has(p)
177
+ )
178
+ tally(took)
179
+ let seen = observation.reads()
180
+ observation.close()
181
+ let read = (p: string) => seen && (seen.get(p) ?? [])
182
+ let s = await survey(
183
+ [
184
+ ...new Set(passing.flatMap((p) => read(p) ?? []).filter(code)),
185
+ ],
186
+ undefined,
187
+ FACTS,
188
+ )
189
+ let remembered: Record<string, string> = s
190
+ ? Object.fromEntries(
191
+ await Promise.all(
192
+ passing.map(async (p) => [p, await proof(s, key.get(p)!, read(p), true)]),
193
+ ),
194
+ )
195
+ : {}
196
+ remember(Object.values(remembered))
197
+ if (seen && s) {
198
+ note(
199
+ Object.fromEntries(passing.map((p) => [p, read(p)!])),
200
+ undefined,
201
+ Object.fromEntries(passing.map((p) => [p, key.get(p)!])),
202
+ )
203
+ }
204
+ let wall = performance.now() - started
205
+ summary(outcomes, wall, { left, planned, loading }, verbose)
206
+ let times = flag('times').at(-1)
207
+ if (times) {
208
+ await io.mkdir(times, { recursive: true })
209
+ await io.writeTextFile(
210
+ `${times}/${platform || 'tests'}${part ? `-${part}` : ''}.json`,
211
+ JSON.stringify(
212
+ timed(outcomes, { wall, planned, loading, loads, remembered, left }),
213
+ ),
214
+ )
215
+ }
216
+ if (rest) await io.writeTextFile(rest, JSON.stringify(unreached))
217
+ system.exit(outcomes.some((o) => !o.ok) ? 1 : 0)
package/mod.js ADDED
@@ -0,0 +1,45 @@
1
+ /**
2
+ * @yaks/testing — declare tests, check what they expect, and wait on facts.
3
+ *
4
+ * A test module declares its tests with `test` (or a `suite` of clauses),
5
+ * each with tags a run can pick it by, and says what it expects with
6
+ * `equal`, `ok`, `match` and `throws`:
7
+ *
8
+ * ```ts ignore
9
+ * import { equal, suite, test } from '@yaks/testing'
10
+ *
11
+ * test('two and two', () => equal(2 + 2, 4))
12
+ * suite('sum', ({ it, equal }) => {
13
+ * it('of nothing is zero', () => equal([].length, 0))
14
+ * }, { tags: ['math'] })
15
+ * ```
16
+ *
17
+ * `tick` yields one macrotask and `until` polls a fact, so a test waits on
18
+ * what it expects instead of sleeping a guessed span:
19
+ *
20
+ * ```ts
21
+ * import { until } from '@yaks/testing'
22
+ *
23
+ * let ready = false
24
+ * setTimeout(() => ready = true)
25
+ * await until(() => ready, { label: 'ready' })
26
+ * ```
27
+ *
28
+ * `lend` sets a global, such as `document`, until it is put back, over one
29
+ * another module installed read-only.
30
+ *
31
+ * `typist` keeps a text field's caret as a browser does, on a DOM that keeps
32
+ * none, and types at it as a person does.
33
+ *
34
+ * The runner, `@yaks/testing/main`, loads test modules and the examples
35
+ * beside the code into one runtime and runs them.
36
+ *
37
+ * @module
38
+ */ export { equal, match, ok, throws } from "./assert.js";
39
+ export { clock } from "./clock.js";
40
+ export { afterAll, cleanup, kept, suite, test } from "./suite.js";
41
+ export { lend } from "./lend.js";
42
+ export { own, waits } from "./own.js";
43
+ export { typist } from "./typist.js";
44
+ export { tick, until } from "./wait.js";
45
+ //# sourceMappingURL=mod.js.map
package/mod.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAqCC,GACD,SAAS,KAAK,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,sBAAqB;AACtD,SAAqB,KAAK,qBAAoB;AAC9C,SACE,QAAQ,EACR,OAAO,EAEP,IAAI,EAGJ,KAAK,EAEL,IAAI,qBACa;AACnB,SAAS,IAAI,oBAAmB;AAChC,SAAqB,GAAG,EAAE,KAAK,mBAAkB;AACjD,SAAsB,MAAM,sBAAqB;AACjD,SAAS,IAAI,EAAE,KAAK,oBAA8B"}
package/mod.ts ADDED
@@ -0,0 +1,55 @@
1
+ /**
2
+ * @yaks/testing — declare tests, check what they expect, and wait on facts.
3
+ *
4
+ * A test module declares its tests with `test` (or a `suite` of clauses),
5
+ * each with tags a run can pick it by, and says what it expects with
6
+ * `equal`, `ok`, `match` and `throws`:
7
+ *
8
+ * ```ts ignore
9
+ * import { equal, suite, test } from '@yaks/testing'
10
+ *
11
+ * test('two and two', () => equal(2 + 2, 4))
12
+ * suite('sum', ({ it, equal }) => {
13
+ * it('of nothing is zero', () => equal([].length, 0))
14
+ * }, { tags: ['math'] })
15
+ * ```
16
+ *
17
+ * `tick` yields one macrotask and `until` polls a fact, so a test waits on
18
+ * what it expects instead of sleeping a guessed span:
19
+ *
20
+ * ```ts
21
+ * import { until } from '@yaks/testing'
22
+ *
23
+ * let ready = false
24
+ * setTimeout(() => ready = true)
25
+ * await until(() => ready, { label: 'ready' })
26
+ * ```
27
+ *
28
+ * `lend` sets a global, such as `document`, until it is put back, over one
29
+ * another module installed read-only.
30
+ *
31
+ * `typist` keeps a text field's caret as a browser does, on a DOM that keeps
32
+ * none, and types at it as a person does.
33
+ *
34
+ * The runner, `@yaks/testing/main`, loads test modules and the examples
35
+ * beside the code into one runtime and runs them.
36
+ *
37
+ * @module
38
+ */
39
+ export { equal, match, ok, throws } from './assert.js'
40
+ export { type Clock, clock } from './clock.js'
41
+ export {
42
+ afterAll,
43
+ cleanup,
44
+ type It,
45
+ kept,
46
+ type Options,
47
+ type Spec,
48
+ suite,
49
+ type Test,
50
+ test,
51
+ } from './suite.js'
52
+ export { lend } from './lend.js'
53
+ export { type Looks, own, waits } from './own.js'
54
+ export { type Typist, typist } from './typist.js'
55
+ export { tick, until, type Wait } from './wait.js'