Skip to content

Commit 527a53a

Browse files
committed
feat: plugins and timing
1 parent f294c61 commit 527a53a

29 files changed

+1124
-515
lines changed

assembly/__tests__/sleep.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, expect, run, test } from "..";
2+
import { sleep } from "as-sleep/assembly";
3+
describe("Should sleep", () => {
4+
test("1ms", () => {
5+
sleep(1);
6+
expect(1).toBe(1);
7+
});
8+
test("10ms", () => {
9+
sleep(10);
10+
expect(1).toBe(1);
11+
});
12+
test("100ms", () => {
13+
sleep(100);
14+
expect(1).toBe(1);
15+
});
16+
test("1s", () => {
17+
sleep(1000);
18+
expect(1).toBe(1);
19+
});
20+
});
21+
22+
run();

assembly/index.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Expectation } from "./src/expectation";
44
import { stringify } from "as-console/stringify";
55
import { __COVER, __HASHES, __POINTS } from "as-test/assembly/coverage";
66
import { JSON } from "json-as";
7-
import { Report, SuiteReport, TestReport } from "../reporters/report";
7+
import { Report, SuiteReport, TestReport, Time } from "../reporters/report";
88

99
/**
1010
* Enumeration representing the verdict of a test case.
@@ -20,11 +20,7 @@ let entrySuites: Suite[] = [];
2020

2121
// Globals
2222
@global let suites: Suite[] = [];
23-
24-
2523
@global let depth: i32 = -1;
26-
27-
2824
@global let current_suite: Suite | null = null;
2925

3026
let before_all_callback: (() => void) | null = null;
@@ -217,7 +213,7 @@ export function afterEach(callback: () => void): void {
217213
}
218214

219215
/**
220-
* Overrides all references to an existing function to instead point to this
216+
* Overrides all references to an existing function in local scope to instead point to new function
221217
* @param {string} fn - name of function to override
222218
* @param {() => returnType} callback - the function to substitute it with
223219
*/
@@ -226,6 +222,22 @@ export function mockFn<returnType>(
226222
callback: (...args: any[]) => returnType,
227223
): void {}
228224

225+
/**
226+
* Unmock all references to an existing function to instead point to the original function
227+
* @param {string} fn - name of function to override
228+
*/
229+
export function unmockFn(
230+
fn: string
231+
): void {}
232+
233+
/**
234+
* Re-mock all references to an existing function to instead point to the declared function
235+
* @param {string} fn - name of function to override
236+
*/
237+
export function remockFn(
238+
fn: string
239+
): void {}
240+
229241
/**
230242
* Class defining options that can be passed to the `run` function.
231243
*
@@ -261,18 +273,25 @@ class RunOptions {
261273
export function run(options: RunOptions = new RunOptions()): void {
262274
__test_options = options;
263275
const report = new Report();
276+
report.time.start = performance.now();
264277
for (let i = 0; i < entrySuites.length; i++) {
278+
const suiteReport = new SuiteReport();
265279
// @ts-ignore
266280
const suite = unchecked(entrySuites[i]);
267281
suites = [suite];
282+
268283
current_suite = suite;
269284
depth = -1;
270285
current_suite = null;
286+
287+
suiteReport.time.start = performance.now();
271288
suite.run();
289+
suiteReport.time.end = performance.now();
290+
272291
suites = [];
273292
depth = -1;
274293
current_suite = null;
275-
const suiteReport = new SuiteReport();
294+
276295
suiteReport.kind = suite.kind;
277296
suiteReport.verdict = suite.verdict;
278297
for (let ii = 0; ii < suite.suites.length; ii++) {
@@ -288,6 +307,7 @@ export function run(options: RunOptions = new RunOptions()): void {
288307
suiteReport.description = suite.description;
289308
report.groups.push(suiteReport);
290309
}
310+
report.time.end = performance.now();
291311
if (report.verdict === Verdict.None) {
292312
if (report.groups.length) report.verdict = Verdict.Ok;
293313
}

assembly/src/suite.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import { Verdict } from "..";
2+
import { Time } from "../../reporters/report";
23
import { Expectation } from "./expectation";
34
import { Tests } from "./tests";
5+
6+
export type SuiteKind = string;
7+
export namespace SuiteKind {
8+
export const It = "it";
9+
export const Describe = "describe";
10+
export const Test = "test";
11+
}
12+
413
export class Suite {
14+
public time: Time = new Time();
515
public description: string;
616
public depth: i32 = 0;
717
public suites: Suite[] = [];
@@ -34,7 +44,9 @@ export class Suite {
3444
depth--;
3545
for (let i = 0; i < this.suites.length; i++) {
3646
const suite = unchecked(this.suites[i]);
47+
suite.time.start = performance.now();
3748
suite.run();
49+
suite.time.end = performance.now();
3850
if (suite.verdict === Verdict.Fail) {
3951
this.verdict = Verdict.Fail;
4052
break;
@@ -52,11 +64,4 @@ export class Suite {
5264
if (this.suites.length) this.verdict = Verdict.Ok;
5365
}
5466
}
55-
}
56-
57-
export type SuiteKind = string;
58-
export namespace SuiteKind {
59-
export const It = "it";
60-
export const Describe = "describe";
61-
export const Test = "test";
6267
}

assembly/util/helpers.ts

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,39 +79,6 @@ export function diff(left: string, right: string, not: boolean = false): Diff {
7979
};
8080
}
8181

82-
class Unit {
83-
name: string;
84-
divisor: number;
85-
}
86-
87-
export function formatTime(ms: number): string {
88-
if (ms < 0) {
89-
throw new Error("Time should be a non-negative number.");
90-
}
91-
92-
// Convert milliseconds to microseconds
93-
const us = ms * 1000;
94-
95-
const units: Unit[] = [
96-
{ name: "μs", divisor: 1 },
97-
{ name: "ms", divisor: 1000 },
98-
{ name: "s", divisor: 1000 * 1000 },
99-
{ name: "m", divisor: 60 * 1000 * 1000 },
100-
{ name: "h", divisor: 60 * 60 * 1000 * 1000 },
101-
{ name: "d", divisor: 24 * 60 * 60 * 1000 * 1000 },
102-
];
103-
104-
for (let i = units.length - 1; i >= 0; i--) {
105-
const unit = units[i];
106-
if (us >= unit.divisor) {
107-
const value = Math.round((us / unit.divisor) * 1000) / 1000;
108-
return `${value}${unit.name}`;
109-
}
110-
}
111-
112-
return `${us}us`;
113-
}
114-
11582
// @ts-ignore
11683
@inline
11784
export function colorText(format: i32[], text: string): string {

bin/about.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import chalk from "chalk";
2+
export function about() {
3+
console.log(chalk.bold.blueBright("as-test") +
4+
" is a testing framework for AssemblyScript. " +
5+
chalk.dim("(v" + version + ")") +
6+
"\n");
7+
console.log(chalk.bold("Usage: as-test") +
8+
" " +
9+
chalk.dim("<command>") +
10+
" " +
11+
chalk.bold.blueBright("[...flags]") +
12+
" " +
13+
chalk.bold("[...args]") +
14+
" " +
15+
chalk.dim("(alias: ast)") +
16+
"\n");
17+
console.log(chalk.bold("Commands:"));
18+
console.log(" " +
19+
chalk.bold.blueBright("run") +
20+
" " +
21+
chalk.dim("<my-test.spec.ts>") +
22+
" " +
23+
"Run unit tests with selected runtime");
24+
console.log(" " +
25+
chalk.bold.blueBright("build") +
26+
" " +
27+
chalk.dim("<my-test.spec.ts>") +
28+
" " +
29+
"Build unit tests and compile");
30+
console.log(" " +
31+
chalk.bold.blueBright("test") +
32+
" " +
33+
chalk.dim("<my-test.spec.ts>") +
34+
" " +
35+
"Build and run unit tests with selected runtime" +
36+
"\n");
37+
console.log(" " +
38+
chalk.bold.magentaBright("init") +
39+
" " +
40+
chalk.strikethrough.dim("") +
41+
" " +
42+
"Initialize an empty testing template");
43+
console.log(" " +
44+
chalk.strikethrough.bold.magentaBright("config") +
45+
" " +
46+
chalk.strikethrough.dim("as-test.config.json") +
47+
" " +
48+
"Specify the configuration file");
49+
console.log(" " +
50+
chalk.strikethrough.bold.magentaBright("reporter") +
51+
" " +
52+
chalk.strikethrough.dim("<tap>") +
53+
" " +
54+
"Specify the test reporter to use");
55+
console.log(" " +
56+
chalk.strikethrough.bold.magentaBright("use") +
57+
" " +
58+
chalk.strikethrough.dim("wasmtime") +
59+
" " +
60+
"Specify the runtime to use" +
61+
"\n");
62+
console.log(chalk.bold("Flags:"));
63+
console.log(" " +
64+
chalk.strikethrough.dim("run") +
65+
" " +
66+
chalk.strikethrough.bold.blue("--coverage") +
67+
" " +
68+
"Use code coverage");
69+
console.log(" " +
70+
chalk.strikethrough.dim("run") +
71+
" " +
72+
chalk.strikethrough.bold.blue("--snapshot") +
73+
" " +
74+
"Take a snapshot of the tests");
75+
console.log(" " +
76+
chalk.strikethrough.dim("use") +
77+
" " +
78+
chalk.strikethrough.bold.blue("--list") +
79+
" " +
80+
"List supported runtimes");
81+
console.log(" " +
82+
chalk.strikethrough.dim("reporter") +
83+
" " +
84+
chalk.strikethrough.bold.blue("--list") +
85+
" " +
86+
"List supported reporters");
87+
console.log(" " +
88+
chalk.strikethrough.dim("<command>") +
89+
" " +
90+
chalk.strikethrough.bold.blue("--help") +
91+
" " +
92+
"Print info about command" +
93+
"\n");
94+
console.log(chalk.dim("If your using this, consider dropping a star, it would help a lot!") + "\n");
95+
console.log("View the repo: " +
96+
chalk.magenta("https://github.com/JairusSW/as-test"));
97+
console.log("View the docs: " +
98+
chalk.strikethrough.blue("https://docs.jairus.dev/as-test"));
99+
}

0 commit comments

Comments
 (0)