-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.test.ts
66 lines (52 loc) · 1.45 KB
/
mod.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { test } from "./mod.ts";
import { assertEquals, assertNotEquals } from "@std/assert";
import { delay } from "@std/async";
// Simple test
test("Multiplication", () => {
assertEquals(5 * 4, 20);
});
// Simple test with timeout
test("Multiplication with timeout", () => {
assertEquals(5 * 4, 20);
}, { timeout: 1000 });
// Failing async test with done callback
test("Long async test", (_context, done) => {
setTimeout(() => {
assertNotEquals(5, 4);
done(); // Signal test completion
}, 500);
}, { waitForCallback: true });
// Test with done callback (useful for async operations)
test("Async test", (_context, done) => {
setTimeout(() => {
assertNotEquals(5, 4);
done(); // Signal test completion
}, 4500);
}, { waitForCallback: true, timeout: 5500 });
// Test async
test("async hello world", async () => {
const x = 1 + 2;
// await some async task
await delay(100);
if (x !== 3) {
throw Error("x should be equal to 3");
}
});
/* Test sinon */
import sinon from "sinon";
function bar() {/*...*/}
export const funcs = {
bar,
};
// 'foo' no longer takes a parameter, but calls 'bar' from an object
export function foo() {
funcs.bar();
}
test("calls bar during execution of foo", () => {
// create a test spy that wraps 'bar' on the 'funcs' object
const spy = sinon.spy(funcs, "bar");
// call function 'foo' without an argument
foo();
assertEquals(spy.called, true);
assertEquals(spy.getCalls().length, 1);
});