@tishlang/tish-format 3.6.0 → 3.7.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/Cargo.toml +1 -0
- package/bin/tish-format +0 -0
- package/crates/tish/Cargo.toml +4 -1
- package/crates/tish/src/cli_help.rs +84 -0
- package/crates/tish/src/main.rs +93 -1
- package/crates/tish/tests/test_runner.rs +673 -0
- package/crates/tish_bytecode/src/compiler.rs +25 -4
- package/crates/tish_compile/src/resolve.rs +45 -12
- package/crates/tish_core/src/lib.rs +103 -11
- package/crates/tish_eval/src/eval.rs +16 -0
- package/crates/tish_eval/src/value_convert.rs +10 -0
- package/crates/tish_fmt/Cargo.toml +1 -1
- package/crates/tish_test/Cargo.toml +47 -0
- package/crates/tish_test/src/assert.rs +435 -0
- package/crates/tish_test/src/config.rs +85 -0
- package/crates/tish_test/src/coverage.rs +237 -0
- package/crates/tish_test/src/deep_equal.rs +261 -0
- package/crates/tish_test/src/discovery.rs +70 -0
- package/crates/tish_test/src/expect.rs +977 -0
- package/crates/tish_test/src/instrument.rs +457 -0
- package/crates/tish_test/src/isolation.rs +14 -0
- package/crates/tish_test/src/lib.rs +22 -0
- package/crates/tish_test/src/load.rs +152 -0
- package/crates/tish_test/src/mocks.rs +270 -0
- package/crates/tish_test/src/module.rs +318 -0
- package/crates/tish_test/src/registry.rs +235 -0
- package/crates/tish_test/src/report.rs +312 -0
- package/crates/tish_test/src/runner.rs +797 -0
- package/crates/tish_test/src/snapshots.rs +197 -0
- package/crates/tish_vm/src/vm.rs +17 -14
- package/package.json +1 -1
- package/platform/darwin-arm64/tish-fmt +0 -0
- package/platform/darwin-x64/tish-fmt +0 -0
- package/platform/linux-arm64/tish-fmt +0 -0
- package/platform/linux-x64/tish-fmt +0 -0
- package/platform/win32-x64/tish-fmt.exe +0 -0
|
@@ -0,0 +1,673 @@
|
|
|
1
|
+
//! Regression suite for `tish test` itself.
|
|
2
|
+
//!
|
|
3
|
+
//! Every case here pins a behavior where the runner previously reported success while tests
|
|
4
|
+
//! were broken, missing, or never run. A test framework that can exit 0 with lost tests is
|
|
5
|
+
//! worse than no framework, so these assert on the **exit code** as much as on the output.
|
|
6
|
+
//!
|
|
7
|
+
//! Each case writes its fixtures to a fresh temp dir under `target/` (never `/tmp`) and runs
|
|
8
|
+
//! the real binary, so the CLI wiring is covered too.
|
|
9
|
+
|
|
10
|
+
use std::path::PathBuf;
|
|
11
|
+
use std::process::{Command, Output};
|
|
12
|
+
|
|
13
|
+
/// A throwaway directory under `target/`, removed on drop.
|
|
14
|
+
struct Sandbox {
|
|
15
|
+
dir: PathBuf,
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
impl Sandbox {
|
|
19
|
+
fn new(name: &str) -> Self {
|
|
20
|
+
let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
21
|
+
.join("../../target/test-runner-fixtures")
|
|
22
|
+
.join(name);
|
|
23
|
+
let _ = std::fs::remove_dir_all(&dir);
|
|
24
|
+
std::fs::create_dir_all(&dir).expect("create sandbox");
|
|
25
|
+
Self { dir }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
fn write(&self, name: &str, body: &str) -> PathBuf {
|
|
29
|
+
let path = self.dir.join(name);
|
|
30
|
+
if let Some(parent) = path.parent() {
|
|
31
|
+
std::fs::create_dir_all(parent).expect("create fixture parent");
|
|
32
|
+
}
|
|
33
|
+
std::fs::write(&path, body).expect("write fixture");
|
|
34
|
+
path
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
fn run(&self, args: &[&str]) -> Output {
|
|
38
|
+
Command::new(PathBuf::from(env!("CARGO_BIN_EXE_tish")))
|
|
39
|
+
.arg("test")
|
|
40
|
+
.args(args)
|
|
41
|
+
.current_dir(&self.dir)
|
|
42
|
+
.env_remove("CI")
|
|
43
|
+
.env_remove("GITHUB_ACTIONS")
|
|
44
|
+
.output()
|
|
45
|
+
.expect("spawn tish test")
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
impl Drop for Sandbox {
|
|
50
|
+
fn drop(&mut self) {
|
|
51
|
+
let _ = std::fs::remove_dir_all(&self.dir);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
impl Sandbox {
|
|
56
|
+
/// Same as `run`, but with `GITHUB_ACTIONS` set so the annotation path is exercised.
|
|
57
|
+
fn run_in_gha(&self, args: &[&str]) -> Output {
|
|
58
|
+
Command::new(PathBuf::from(env!("CARGO_BIN_EXE_tish")))
|
|
59
|
+
.arg("test")
|
|
60
|
+
.args(args)
|
|
61
|
+
.current_dir(&self.dir)
|
|
62
|
+
.env_remove("CI")
|
|
63
|
+
.env("GITHUB_ACTIONS", "true")
|
|
64
|
+
.output()
|
|
65
|
+
.expect("spawn tish test")
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
fn combined(out: &Output) -> String {
|
|
70
|
+
format!(
|
|
71
|
+
"{}{}",
|
|
72
|
+
String::from_utf8_lossy(&out.stdout),
|
|
73
|
+
String::from_utf8_lossy(&out.stderr)
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
fn assert_failed(out: &Output, why: &str) {
|
|
78
|
+
assert!(
|
|
79
|
+
!out.status.success(),
|
|
80
|
+
"expected a non-zero exit ({why}), got success. Output:\n{}",
|
|
81
|
+
combined(out)
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
fn assert_passed(out: &Output, why: &str) {
|
|
86
|
+
assert!(
|
|
87
|
+
out.status.success(),
|
|
88
|
+
"expected exit 0 ({why}). Output:\n{}",
|
|
89
|
+
combined(out)
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/// A throw inside a `describe` body silently dropped every later registration and exited 0.
|
|
94
|
+
#[test]
|
|
95
|
+
fn collection_error_fails_the_run() {
|
|
96
|
+
let sb = Sandbox::new("collect_error");
|
|
97
|
+
sb.write(
|
|
98
|
+
"a.test.tish",
|
|
99
|
+
r#"import { describe, test, expect } from "tish:test"
|
|
100
|
+
describe("outer", () => {
|
|
101
|
+
test("first", () => { expect(1).toBe(1) })
|
|
102
|
+
throw "collection blew up"
|
|
103
|
+
test("never registered", () => { expect(1).toBe(2) })
|
|
104
|
+
})
|
|
105
|
+
"#,
|
|
106
|
+
);
|
|
107
|
+
let out = sb.run(&[]);
|
|
108
|
+
assert_failed(&out, "a describe body threw during collection");
|
|
109
|
+
let text = combined(&out);
|
|
110
|
+
assert!(
|
|
111
|
+
text.contains("collection blew up") && text.contains("collection error"),
|
|
112
|
+
"collection error not reported:\n{text}"
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/// `--rerun-each` reported only the final pass, so a run that failed once and passed twice
|
|
117
|
+
/// exited 0 — defeating the flake detection the flag exists for.
|
|
118
|
+
#[test]
|
|
119
|
+
fn rerun_each_fails_if_any_pass_failed() {
|
|
120
|
+
let sb = Sandbox::new("rerun_each");
|
|
121
|
+
sb.write(
|
|
122
|
+
"flake.test.tish",
|
|
123
|
+
r#"import { test, expect } from "tish:test"
|
|
124
|
+
import { readFileSync, writeFileSync, existsSync } from "tish:fs"
|
|
125
|
+
test("fails on the first pass only", () => {
|
|
126
|
+
let n = existsSync("./counter.txt") ? Number(readFileSync("./counter.txt", "utf8")) : 0
|
|
127
|
+
writeFileSync("./counter.txt", String(n + 1))
|
|
128
|
+
expect(n).toBeGreaterThan(0)
|
|
129
|
+
})
|
|
130
|
+
"#,
|
|
131
|
+
);
|
|
132
|
+
let out = sb.run(&["--rerun-each", "3"]);
|
|
133
|
+
assert_failed(&out, "pass 1 of 3 failed");
|
|
134
|
+
assert!(
|
|
135
|
+
combined(&out).contains("passes failed"),
|
|
136
|
+
"no flake summary:\n{}",
|
|
137
|
+
combined(&out)
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/// `--feature` was accepted and ignored, so a restricted run still had every capability.
|
|
142
|
+
#[test]
|
|
143
|
+
fn feature_flag_restricts_capabilities() {
|
|
144
|
+
let sb = Sandbox::new("features");
|
|
145
|
+
sb.write(
|
|
146
|
+
"fs.test.tish",
|
|
147
|
+
r#"import { test, expect } from "tish:test"
|
|
148
|
+
import { writeFileSync } from "tish:fs"
|
|
149
|
+
test("writes", () => {
|
|
150
|
+
writeFileSync("./written.txt", "x")
|
|
151
|
+
expect(1).toBe(1)
|
|
152
|
+
})
|
|
153
|
+
"#,
|
|
154
|
+
);
|
|
155
|
+
assert_passed(&sb.run(&["--feature", "fs"]), "fs granted");
|
|
156
|
+
assert_failed(
|
|
157
|
+
&sb.run(&["--feature", "timers"]),
|
|
158
|
+
"fs must be denied when only timers is granted",
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/// Two unnamed `toMatchSnapshot()` calls in sibling tests collided on one `.snap` file, so
|
|
163
|
+
/// each test was diffed against the other's value.
|
|
164
|
+
#[test]
|
|
165
|
+
fn unnamed_snapshots_do_not_collide_across_tests() {
|
|
166
|
+
let sb = Sandbox::new("snapshots");
|
|
167
|
+
sb.write(
|
|
168
|
+
"snap.test.tish",
|
|
169
|
+
r#"import { test, expect } from "tish:test"
|
|
170
|
+
test("alpha", () => { expect({ v: "alpha" }).toMatchSnapshot() })
|
|
171
|
+
test("beta", () => { expect({ v: "beta" }).toMatchSnapshot() })
|
|
172
|
+
"#,
|
|
173
|
+
);
|
|
174
|
+
assert_passed(&sb.run(&[]), "first run writes both snapshots");
|
|
175
|
+
assert_passed(&sb.run(&[]), "second run compares against its own snapshot");
|
|
176
|
+
|
|
177
|
+
let snaps: Vec<String> = std::fs::read_dir(sb.dir.join("__snapshots__"))
|
|
178
|
+
.expect("snapshot dir")
|
|
179
|
+
.filter_map(|e| e.ok())
|
|
180
|
+
.map(|e| e.file_name().to_string_lossy().into_owned())
|
|
181
|
+
.collect();
|
|
182
|
+
assert_eq!(
|
|
183
|
+
snaps.len(),
|
|
184
|
+
2,
|
|
185
|
+
"expected one snapshot per test, got {snaps:?}"
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/// In CI a *missing* snapshot was written and passed, so a deleted `.snap` went green on the
|
|
190
|
+
/// very run that should have caught it.
|
|
191
|
+
#[test]
|
|
192
|
+
fn ci_mode_fails_on_a_missing_snapshot() {
|
|
193
|
+
let sb = Sandbox::new("snapshots_ci");
|
|
194
|
+
sb.write(
|
|
195
|
+
"snap.test.tish",
|
|
196
|
+
r#"import { test, expect } from "tish:test"
|
|
197
|
+
test("alpha", () => { expect({ v: "alpha" }).toMatchSnapshot() })
|
|
198
|
+
"#,
|
|
199
|
+
);
|
|
200
|
+
assert_failed(&sb.run(&["--ci"]), "no snapshot exists yet and --ci is set");
|
|
201
|
+
assert_passed(&sb.run(&[]), "without --ci the snapshot is written");
|
|
202
|
+
assert_passed(&sb.run(&["--ci"]), "now that it exists --ci passes");
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/// `describe.only` did not narrow the run at all, and once only-mode was active, tests nested
|
|
206
|
+
/// more than one level under it were silently filtered out.
|
|
207
|
+
#[test]
|
|
208
|
+
fn describe_only_selects_the_whole_subtree() {
|
|
209
|
+
let sb = Sandbox::new("only");
|
|
210
|
+
sb.write(
|
|
211
|
+
"only.test.tish",
|
|
212
|
+
r#"import { describe, test, expect } from "tish:test"
|
|
213
|
+
describe.only("kept", () => {
|
|
214
|
+
test("direct", () => { expect(1).toBe(1) })
|
|
215
|
+
describe("nested", () => {
|
|
216
|
+
test("one level down", () => { expect(1).toBe(1) })
|
|
217
|
+
describe("deeper", () => {
|
|
218
|
+
test("deep", () => { expect(1).toBe(1) })
|
|
219
|
+
})
|
|
220
|
+
})
|
|
221
|
+
})
|
|
222
|
+
describe("dropped", () => {
|
|
223
|
+
test("must not run", () => { expect(1).toBe(2) })
|
|
224
|
+
})
|
|
225
|
+
"#,
|
|
226
|
+
);
|
|
227
|
+
let out = sb.run(&[]);
|
|
228
|
+
assert_passed(&out, "only the `.only` subtree runs, and it passes");
|
|
229
|
+
let text = combined(&out);
|
|
230
|
+
assert!(
|
|
231
|
+
text.contains("deep"),
|
|
232
|
+
"nested-under-only test was dropped:\n{text}"
|
|
233
|
+
);
|
|
234
|
+
assert!(
|
|
235
|
+
text.contains("3 passed"),
|
|
236
|
+
"expected all 3 subtree tests to run:\n{text}"
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/// A runaway loop could not be interrupted: the deadline was only checked after the body
|
|
241
|
+
/// returned, which never happened.
|
|
242
|
+
#[test]
|
|
243
|
+
fn timeout_interrupts_a_runaway_loop() {
|
|
244
|
+
let sb = Sandbox::new("timeout");
|
|
245
|
+
sb.write(
|
|
246
|
+
"spin.test.tish",
|
|
247
|
+
r#"import { test, expect } from "tish:test"
|
|
248
|
+
test("spins", () => { let i = 0; while (true) { i = i + 1 } })
|
|
249
|
+
test("still runs afterwards", () => { expect(1).toBe(1) })
|
|
250
|
+
"#,
|
|
251
|
+
);
|
|
252
|
+
let started = std::time::Instant::now();
|
|
253
|
+
let out = sb.run(&["--timeout", "500"]);
|
|
254
|
+
let elapsed = started.elapsed();
|
|
255
|
+
assert_failed(&out, "the spinning test must time out");
|
|
256
|
+
assert!(
|
|
257
|
+
elapsed < std::time::Duration::from_secs(60),
|
|
258
|
+
"runner did not abort the loop (took {elapsed:?})"
|
|
259
|
+
);
|
|
260
|
+
let text = combined(&out);
|
|
261
|
+
assert!(text.contains("timed out"), "no timeout message:\n{text}");
|
|
262
|
+
assert!(
|
|
263
|
+
text.contains("still runs afterwards"),
|
|
264
|
+
"the run did not continue past the timed-out test:\n{text}"
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/// `assert.throws(fn, matcher)` treated the matcher as a message, so any throw satisfied it;
|
|
269
|
+
/// `assert.rejects` / `doesNotReject` never invoked a function argument.
|
|
270
|
+
#[test]
|
|
271
|
+
fn node_assert_validates_errors_and_rejections() {
|
|
272
|
+
let sb = Sandbox::new("assert");
|
|
273
|
+
sb.write(
|
|
274
|
+
"ok.test.tish",
|
|
275
|
+
r#"import { test } from "tish:test"
|
|
276
|
+
import assert from "node:assert/strict"
|
|
277
|
+
test("matching pattern", () => { assert.throws(() => { throw "Boom" }, /Boom/) })
|
|
278
|
+
test("rejects on an async fn", () => { assert.rejects(async () => { throw "nope" }) })
|
|
279
|
+
test("doesNotReject on a clean fn", () => { assert.doesNotReject(async () => { return 1 }) })
|
|
280
|
+
"#,
|
|
281
|
+
);
|
|
282
|
+
assert_passed(&sb.run(&[]), "the well-behaved assertions pass");
|
|
283
|
+
|
|
284
|
+
let bad = Sandbox::new("assert_bad");
|
|
285
|
+
bad.write(
|
|
286
|
+
"bad.test.tish",
|
|
287
|
+
r#"import { test } from "tish:test"
|
|
288
|
+
import assert from "node:assert/strict"
|
|
289
|
+
test("non-matching pattern must fail", () => {
|
|
290
|
+
assert.throws(() => { throw "Boom" }, /TotallyDifferent/)
|
|
291
|
+
})
|
|
292
|
+
"#,
|
|
293
|
+
);
|
|
294
|
+
assert_failed(
|
|
295
|
+
&bad.run(&[]),
|
|
296
|
+
"the thrown value does not match the expected pattern",
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
let bad2 = Sandbox::new("assert_bad2");
|
|
300
|
+
bad2.write(
|
|
301
|
+
"bad2.test.tish",
|
|
302
|
+
r#"import { test } from "tish:test"
|
|
303
|
+
import assert from "node:assert/strict"
|
|
304
|
+
test("doesNotReject on a rejecting fn must fail", () => {
|
|
305
|
+
assert.doesNotReject(async () => { throw "rejects" })
|
|
306
|
+
})
|
|
307
|
+
"#,
|
|
308
|
+
);
|
|
309
|
+
assert_failed(&bad2.run(&[]), "the function rejects");
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/// `expect(nonFunction).toThrow()` passed because the failed call itself set a pending throw.
|
|
313
|
+
#[test]
|
|
314
|
+
fn to_throw_rejects_non_functions() {
|
|
315
|
+
let sb = Sandbox::new("to_throw");
|
|
316
|
+
sb.write(
|
|
317
|
+
"t.test.tish",
|
|
318
|
+
r#"import { test, expect } from "tish:test"
|
|
319
|
+
test("not a function", () => { expect("a string").toThrow() })
|
|
320
|
+
"#,
|
|
321
|
+
);
|
|
322
|
+
assert_failed(&sb.run(&[]), "a string cannot throw");
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/// Matcher semantics that previously diverged from Jest/Bun or were unusable outright.
|
|
326
|
+
#[test]
|
|
327
|
+
fn matcher_semantics_match_jest() {
|
|
328
|
+
let sb = Sandbox::new("matchers");
|
|
329
|
+
sb.write(
|
|
330
|
+
"m.test.tish",
|
|
331
|
+
r#"import { test, expect } from "tish:test"
|
|
332
|
+
test("identical functions are equal", () => {
|
|
333
|
+
let f = () => 1
|
|
334
|
+
expect({ f: f }).toEqual({ f: f })
|
|
335
|
+
})
|
|
336
|
+
test("null is the absence value", () => {
|
|
337
|
+
expect(null).toBeUndefined()
|
|
338
|
+
expect(1).toBeDefined()
|
|
339
|
+
})
|
|
340
|
+
test("added matchers", () => {
|
|
341
|
+
expect({ a: { b: [1, 2] } }).toHaveProperty("a.b.1", 2)
|
|
342
|
+
expect(0 / 0).toBeNaN()
|
|
343
|
+
expect("x").toBeTypeOf("string")
|
|
344
|
+
expect([{ id: 1 }]).toContainEqual({ id: 1 })
|
|
345
|
+
})
|
|
346
|
+
test("asymmetric matchers", () => {
|
|
347
|
+
expect([1, 2, 3]).toEqual(expect.arrayContaining([1, 2]))
|
|
348
|
+
expect({ a: 1, b: 2 }).toEqual(expect.objectContaining({ a: 1 }))
|
|
349
|
+
})
|
|
350
|
+
"#,
|
|
351
|
+
);
|
|
352
|
+
assert_passed(&sb.run(&[]), "all matcher semantics hold");
|
|
353
|
+
|
|
354
|
+
let bad = Sandbox::new("matchers_bad");
|
|
355
|
+
bad.write(
|
|
356
|
+
"m.test.tish",
|
|
357
|
+
r#"import { test, expect } from "tish:test"
|
|
358
|
+
test("jest fails this: |0.005 - 0| is not < 10^-2 / 2", () => {
|
|
359
|
+
expect(0.005).toBeCloseTo(0, 2)
|
|
360
|
+
})
|
|
361
|
+
"#,
|
|
362
|
+
);
|
|
363
|
+
assert_failed(
|
|
364
|
+
&bad.run(&[]),
|
|
365
|
+
"toBeCloseTo must use Jest's half-precision rule",
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/// `expect.assertions(n)` turns a body that returned early into a failure rather than a pass.
|
|
370
|
+
#[test]
|
|
371
|
+
fn assertion_count_contract_is_enforced() {
|
|
372
|
+
let sb = Sandbox::new("assertion_count");
|
|
373
|
+
sb.write(
|
|
374
|
+
"a.test.tish",
|
|
375
|
+
r#"import { test, expect } from "tish:test"
|
|
376
|
+
test("promises two assertions but makes one", () => {
|
|
377
|
+
expect.assertions(2)
|
|
378
|
+
expect(1).toBe(1)
|
|
379
|
+
})
|
|
380
|
+
"#,
|
|
381
|
+
);
|
|
382
|
+
assert_failed(&sb.run(&[]), "only one assertion was made");
|
|
383
|
+
|
|
384
|
+
let ok = Sandbox::new("assertion_count_ok");
|
|
385
|
+
ok.write(
|
|
386
|
+
"a.test.tish",
|
|
387
|
+
r#"import { test, expect } from "tish:test"
|
|
388
|
+
test("keeps its promise", () => {
|
|
389
|
+
expect.assertions(2)
|
|
390
|
+
expect(1).toBe(1)
|
|
391
|
+
expect(2).toBe(2)
|
|
392
|
+
})
|
|
393
|
+
"#,
|
|
394
|
+
);
|
|
395
|
+
assert_passed(&ok.run(&[]), "the contract is satisfied");
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/// A failing `beforeAll` dropped its tests from the report entirely and skipped `afterAll`,
|
|
399
|
+
/// leaking whatever the hook had already acquired.
|
|
400
|
+
#[test]
|
|
401
|
+
fn before_all_failure_reports_tests_and_runs_after_all() {
|
|
402
|
+
let sb = Sandbox::new("hooks");
|
|
403
|
+
sb.write(
|
|
404
|
+
"h.test.tish",
|
|
405
|
+
r#"import { describe, test, expect, beforeAll, afterAll } from "tish:test"
|
|
406
|
+
describe("setup fails", () => {
|
|
407
|
+
beforeAll(() => { throw "boom" })
|
|
408
|
+
afterAll(() => { console.log("AFTER_ALL_RAN") })
|
|
409
|
+
test("t1", () => { expect(1).toBe(1) })
|
|
410
|
+
test("t2", () => { expect(1).toBe(1) })
|
|
411
|
+
})
|
|
412
|
+
"#,
|
|
413
|
+
);
|
|
414
|
+
let out = sb.run(&[]);
|
|
415
|
+
assert_failed(&out, "beforeAll threw");
|
|
416
|
+
let text = combined(&out);
|
|
417
|
+
assert!(
|
|
418
|
+
text.contains("AFTER_ALL_RAN"),
|
|
419
|
+
"afterAll did not run:\n{text}"
|
|
420
|
+
);
|
|
421
|
+
assert!(
|
|
422
|
+
text.contains("3 failed"),
|
|
423
|
+
"expected the hook plus both tests to be reported:\n{text}"
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/// `--preload` ran in its own backend instance (so nothing it defined was visible) and
|
|
428
|
+
/// discarded load errors, making a typo'd path a silent no-op.
|
|
429
|
+
#[test]
|
|
430
|
+
fn preload_shares_the_backend_and_surfaces_errors() {
|
|
431
|
+
let sb = Sandbox::new("preload");
|
|
432
|
+
sb.write(
|
|
433
|
+
"setup.tish",
|
|
434
|
+
r#"import { beforeEach } from "tish:test"
|
|
435
|
+
beforeEach(() => { console.log("PRELOAD_HOOK") })
|
|
436
|
+
"#,
|
|
437
|
+
);
|
|
438
|
+
sb.write(
|
|
439
|
+
"p.test.tish",
|
|
440
|
+
r#"import { test, expect } from "tish:test"
|
|
441
|
+
test("t", () => { expect(1).toBe(1) })
|
|
442
|
+
"#,
|
|
443
|
+
);
|
|
444
|
+
let out = sb.run(&["--preload", "./setup.tish"]);
|
|
445
|
+
assert_passed(&out, "preload + test both load");
|
|
446
|
+
assert!(
|
|
447
|
+
combined(&out).contains("PRELOAD_HOOK"),
|
|
448
|
+
"the preload's beforeEach did not reach the test file:\n{}",
|
|
449
|
+
combined(&out)
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
assert_failed(
|
|
453
|
+
&sb.run(&["--preload", "./nope.tish"]),
|
|
454
|
+
"a missing preload must not be silently ignored",
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/// `--reporter dots` printed a bare `F` and no diagnostics at all.
|
|
459
|
+
#[test]
|
|
460
|
+
fn dots_reporter_still_reports_failures() {
|
|
461
|
+
let sb = Sandbox::new("dots");
|
|
462
|
+
sb.write(
|
|
463
|
+
"d.test.tish",
|
|
464
|
+
r#"import { test, expect } from "tish:test"
|
|
465
|
+
test("passes", () => { expect(1).toBe(1) })
|
|
466
|
+
test("fails", () => { expect(1).toBe(2) })
|
|
467
|
+
"#,
|
|
468
|
+
);
|
|
469
|
+
let out = sb.run(&["--reporter", "dots"]);
|
|
470
|
+
assert_failed(&out, "one test fails");
|
|
471
|
+
let text = combined(&out);
|
|
472
|
+
assert!(text.contains("Failures:"), "no failure section:\n{text}");
|
|
473
|
+
assert!(
|
|
474
|
+
text.contains("Expected: 2") && text.contains("Received: 1"),
|
|
475
|
+
"failure diagnostics missing from dots output:\n{text}"
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/// A thrown `AssertionError` was printed as a whole object literal, burying the message.
|
|
480
|
+
#[test]
|
|
481
|
+
fn failures_are_printed_readably() {
|
|
482
|
+
let sb = Sandbox::new("format");
|
|
483
|
+
sb.write(
|
|
484
|
+
"f.test.tish",
|
|
485
|
+
r#"import { test, expect } from "tish:test"
|
|
486
|
+
test("fails", () => { expect(1).toBe(2) })
|
|
487
|
+
"#,
|
|
488
|
+
);
|
|
489
|
+
let out = sb.run(&[]);
|
|
490
|
+
assert_failed(&out, "the test fails");
|
|
491
|
+
let text = combined(&out);
|
|
492
|
+
assert!(
|
|
493
|
+
text.contains("AssertionError: expect(received).toBe(expected)"),
|
|
494
|
+
"failure was not reformatted:\n{text}"
|
|
495
|
+
);
|
|
496
|
+
assert!(
|
|
497
|
+
!text.contains("{generatedMessage:") && !text.contains("{code: ERR_ASSERTION"),
|
|
498
|
+
"raw object dump leaked into the report:\n{text}"
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/// GitHub Actions annotations are what a reviewer sees inline on the diff, so they carry the
|
|
503
|
+
/// same readable formatting as the console — and must be percent-escaped, since GitHub ends the
|
|
504
|
+
/// command at the first newline and would otherwise truncate the annotation and leak the rest.
|
|
505
|
+
#[test]
|
|
506
|
+
fn github_annotations_are_formatted_and_escaped() {
|
|
507
|
+
let sb = Sandbox::new("gha");
|
|
508
|
+
sb.write(
|
|
509
|
+
"f.test.tish",
|
|
510
|
+
r#"import { test, expect } from "tish:test"
|
|
511
|
+
test("fails", () => { expect(1).toBe(2) })
|
|
512
|
+
"#,
|
|
513
|
+
);
|
|
514
|
+
let out = sb.run_in_gha(&[]);
|
|
515
|
+
assert_failed(&out, "the test fails");
|
|
516
|
+
let text = combined(&out);
|
|
517
|
+
let annotation = text
|
|
518
|
+
.lines()
|
|
519
|
+
.find(|l| l.starts_with("::error "))
|
|
520
|
+
.unwrap_or_else(|| panic!("no annotation emitted:\n{text}"))
|
|
521
|
+
.to_string();
|
|
522
|
+
assert!(
|
|
523
|
+
annotation.contains("AssertionError: expect(received).toBe(expected)"),
|
|
524
|
+
"annotation was not formatted:\n{annotation}"
|
|
525
|
+
);
|
|
526
|
+
assert!(
|
|
527
|
+
!annotation.contains("{generatedMessage:") && !annotation.contains("{code: ERR_ASSERTION"),
|
|
528
|
+
"raw object dump leaked into the annotation:\n{annotation}"
|
|
529
|
+
);
|
|
530
|
+
assert!(
|
|
531
|
+
annotation.contains("%0A"),
|
|
532
|
+
"multi-line annotation was not percent-escaped (GitHub truncates at the first newline):\n{annotation}"
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/// Coverage skipped arrow bodies nested in most expression forms, so they never entered the
|
|
537
|
+
/// denominator and the reported percentage was inflated.
|
|
538
|
+
#[test]
|
|
539
|
+
fn coverage_counts_arrows_in_nested_expressions() {
|
|
540
|
+
let sb = Sandbox::new("coverage");
|
|
541
|
+
sb.write(
|
|
542
|
+
"src/lib.tish",
|
|
543
|
+
r#"export fn covered(x) {
|
|
544
|
+
let a = x + 1
|
|
545
|
+
return a
|
|
546
|
+
}
|
|
547
|
+
export let pick = true ? (v) => {
|
|
548
|
+
let inCond = v * 2
|
|
549
|
+
return inCond
|
|
550
|
+
} : (v) => v
|
|
551
|
+
export let holder = {}
|
|
552
|
+
holder.cb = (v) => {
|
|
553
|
+
let inAssign = v + 5
|
|
554
|
+
return inAssign
|
|
555
|
+
}
|
|
556
|
+
"#,
|
|
557
|
+
);
|
|
558
|
+
sb.write(
|
|
559
|
+
"c.test.tish",
|
|
560
|
+
r#"import { test, expect } from "tish:test"
|
|
561
|
+
import { covered, pick, holder } from "./src/lib.tish"
|
|
562
|
+
test("covers", () => {
|
|
563
|
+
expect(covered(1)).toBe(2)
|
|
564
|
+
expect(pick(2)).toBe(4)
|
|
565
|
+
expect(holder.cb(1)).toBe(6)
|
|
566
|
+
})
|
|
567
|
+
"#,
|
|
568
|
+
);
|
|
569
|
+
let out = sb.run(&["--coverage"]);
|
|
570
|
+
assert_passed(&out, "the suite passes under coverage");
|
|
571
|
+
let lcov = std::fs::read_to_string(sb.dir.join("coverage/lcov.info")).expect("lcov written");
|
|
572
|
+
// Lines 6-7 (conditional arrow) and 11-12 (member-assign arrow) must be present.
|
|
573
|
+
for line in ["DA:6,", "DA:7,", "DA:11,", "DA:12,"] {
|
|
574
|
+
assert!(
|
|
575
|
+
lcov.contains(line),
|
|
576
|
+
"coverage omitted a nested arrow body ({line}):\n{lcov}"
|
|
577
|
+
);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/// An all-test-file run reported "100%" coverage of nothing, which reads as a passing gate.
|
|
582
|
+
#[test]
|
|
583
|
+
fn empty_coverage_does_not_claim_full_coverage() {
|
|
584
|
+
let sb = Sandbox::new("coverage_empty");
|
|
585
|
+
sb.write(
|
|
586
|
+
"e.test.tish",
|
|
587
|
+
r#"import { test, expect } from "tish:test"
|
|
588
|
+
test("t", () => { expect(1).toBe(1) })
|
|
589
|
+
"#,
|
|
590
|
+
);
|
|
591
|
+
let out = sb.run(&["--coverage"]);
|
|
592
|
+
assert_passed(&out, "the suite passes");
|
|
593
|
+
let text = combined(&out);
|
|
594
|
+
assert!(
|
|
595
|
+
!text.contains("100.0%"),
|
|
596
|
+
"reported full coverage with nothing instrumented:\n{text}"
|
|
597
|
+
);
|
|
598
|
+
assert!(
|
|
599
|
+
text.contains("nothing to report"),
|
|
600
|
+
"empty coverage was not called out:\n{text}"
|
|
601
|
+
);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/// `--backend interp` was advertised but failed on the simplest possible test file, because
|
|
605
|
+
/// interpreter closures cannot be invoked after the evaluator is dropped.
|
|
606
|
+
#[test]
|
|
607
|
+
fn unsupported_backends_are_rejected_up_front() {
|
|
608
|
+
let sb = Sandbox::new("backend");
|
|
609
|
+
sb.write(
|
|
610
|
+
"b.test.tish",
|
|
611
|
+
r#"import { test, expect } from "tish:test"
|
|
612
|
+
test("t", () => { expect(1).toBe(1) })
|
|
613
|
+
"#,
|
|
614
|
+
);
|
|
615
|
+
for backend in ["interp", "native"] {
|
|
616
|
+
let out = sb.run(&["--backend", backend]);
|
|
617
|
+
assert_failed(&out, "unsupported backend");
|
|
618
|
+
let text = combined(&out);
|
|
619
|
+
assert!(
|
|
620
|
+
text.contains("unsupported --backend"),
|
|
621
|
+
"no clear rejection for --backend {backend}:\n{text}"
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/// Sanity: the happy path still works end to end, including discovery and nesting.
|
|
627
|
+
#[test]
|
|
628
|
+
fn a_passing_suite_exits_zero() {
|
|
629
|
+
let sb = Sandbox::new("happy");
|
|
630
|
+
sb.write(
|
|
631
|
+
"nested/ok.test.tish",
|
|
632
|
+
r#"import { describe, test, expect, beforeEach } from "tish:test"
|
|
633
|
+
let seen = 0
|
|
634
|
+
describe("math", () => {
|
|
635
|
+
beforeEach(() => { seen = seen + 1 })
|
|
636
|
+
test("adds", () => { expect(1 + 1).toBe(2) })
|
|
637
|
+
test.each([[1, 1], [2, 4]])("squares %i", (n, sq) => { expect(n * n).toBe(sq) })
|
|
638
|
+
test.skip("skipped", () => { expect(1).toBe(2) })
|
|
639
|
+
test.todo("todo")
|
|
640
|
+
})
|
|
641
|
+
"#,
|
|
642
|
+
);
|
|
643
|
+
let out = sb.run(&[]);
|
|
644
|
+
assert_passed(&out, "the happy path");
|
|
645
|
+
let text = combined(&out);
|
|
646
|
+
assert!(
|
|
647
|
+
text.contains("3 passed") && text.contains("1 skipped") && text.contains("1 todo"),
|
|
648
|
+
"unexpected summary:\n{text}"
|
|
649
|
+
);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/// Discovery only picks up recognized test-file suffixes.
|
|
653
|
+
#[test]
|
|
654
|
+
fn discovery_ignores_non_test_files() {
|
|
655
|
+
let sb = Sandbox::new("discovery");
|
|
656
|
+
sb.write(
|
|
657
|
+
"helper.tish",
|
|
658
|
+
"export fn helper() { return 1 }\nthrow \"helper must not run as a suite\"\n",
|
|
659
|
+
);
|
|
660
|
+
sb.write(
|
|
661
|
+
"real.spec.tish",
|
|
662
|
+
r#"import { test, expect } from "tish:test"
|
|
663
|
+
test("t", () => { expect(1).toBe(1) })
|
|
664
|
+
"#,
|
|
665
|
+
);
|
|
666
|
+
let out = sb.run(&[]);
|
|
667
|
+
assert_passed(&out, "only real.spec.tish is a suite");
|
|
668
|
+
assert!(
|
|
669
|
+
combined(&out).contains("1 passed"),
|
|
670
|
+
"unexpected discovery result:\n{}",
|
|
671
|
+
combined(&out)
|
|
672
|
+
);
|
|
673
|
+
}
|