as-test 0.4.4 → 0.5.1

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 (67) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/README.md +196 -82
  3. package/as-test.config.schema.json +137 -0
  4. package/assembly/coverage.ts +19 -0
  5. package/assembly/index.ts +172 -85
  6. package/assembly/src/expectation.ts +263 -199
  7. package/assembly/src/log.ts +1 -9
  8. package/assembly/src/suite.ts +61 -25
  9. package/assembly/src/tests.ts +2 -0
  10. package/assembly/util/wipc.ts +286 -0
  11. package/bin/build.js +86 -41
  12. package/bin/index.js +337 -68
  13. package/bin/init.js +441 -183
  14. package/bin/reporter.js +1 -1
  15. package/bin/reporters/default.js +379 -0
  16. package/bin/reporters/types.js +1 -0
  17. package/bin/run.js +882 -194
  18. package/bin/types.js +14 -7
  19. package/bin/util.js +54 -3
  20. package/package.json +34 -16
  21. package/transform/lib/builder.js +169 -169
  22. package/transform/lib/builder.js.map +1 -1
  23. package/transform/lib/coverage.js +47 -1
  24. package/transform/lib/coverage.js.map +1 -1
  25. package/transform/lib/index.js +70 -0
  26. package/transform/lib/index.js.map +1 -1
  27. package/transform/lib/location.js +20 -0
  28. package/transform/lib/location.js.map +1 -0
  29. package/transform/lib/log.js +118 -0
  30. package/transform/lib/log.js.map +1 -0
  31. package/transform/lib/mock.js +2 -2
  32. package/transform/lib/mock.js.map +1 -1
  33. package/transform/lib/util.js +3 -3
  34. package/transform/lib/util.js.map +1 -1
  35. package/.github/workflows/as-test.yml +0 -26
  36. package/.prettierrc +0 -3
  37. package/as-test.config.json +0 -19
  38. package/assembly/__tests__/array.spec.ts +0 -25
  39. package/assembly/__tests__/math.spec.ts +0 -16
  40. package/assembly/__tests__/mock.spec.ts +0 -22
  41. package/assembly/__tests__/mock.ts +0 -7
  42. package/assembly/__tests__/sleep.spec.ts +0 -28
  43. package/assembly/tsconfig.json +0 -97
  44. package/assets/img/screenshot.png +0 -0
  45. package/cli/build.ts +0 -117
  46. package/cli/index.ts +0 -190
  47. package/cli/init.ts +0 -247
  48. package/cli/reporter.ts +0 -1
  49. package/cli/run.ts +0 -286
  50. package/cli/tsconfig.json +0 -9
  51. package/cli/types.ts +0 -29
  52. package/cli/util.ts +0 -65
  53. package/run/package.json +0 -27
  54. package/tests/array.run.js +0 -7
  55. package/tests/math.run.js +0 -7
  56. package/tests/mock.run.js +0 -14
  57. package/tests/sleep.run.js +0 -7
  58. package/transform/src/builder.ts +0 -1474
  59. package/transform/src/coverage.ts +0 -580
  60. package/transform/src/index.ts +0 -73
  61. package/transform/src/linker.ts +0 -41
  62. package/transform/src/mock.ts +0 -163
  63. package/transform/src/range.ts +0 -12
  64. package/transform/src/types.ts +0 -35
  65. package/transform/src/util.ts +0 -81
  66. package/transform/src/visitor.ts +0 -744
  67. package/transform/tsconfig.json +0 -10
package/cli/index.ts DELETED
@@ -1,190 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import chalk from "chalk";
4
- import { build } from "./build.js";
5
- import { run } from "./run.js";
6
- import { init } from "./init.js";
7
-
8
- const _args = process.argv.slice(2);
9
- const flags: string[] = [];
10
- const args: string[] = [];
11
-
12
- const COMMANDS: string[] = ["run", "build", "test", "init"];
13
-
14
- const version = "0.3.5";
15
-
16
- for (const arg of _args) {
17
- if (arg.startsWith("-")) flags.push(arg);
18
- else args.push(arg);
19
- }
20
-
21
- if (!args.length) {
22
- if (flags.includes("--version") || flags.includes("-v")) {
23
- console.log("as-test v" + version.toString());
24
- } else {
25
- info();
26
- }
27
- } else if (COMMANDS.includes(args[0]!)) {
28
- const command = args.shift();
29
- if (command === "build") {
30
- build();
31
- } else if (command === "run") {
32
- run();
33
- } else if (command === "test") {
34
- build().then(() => {
35
- run();
36
- });
37
- } else if (command === "init") {
38
- init(args);
39
- }
40
- } else {
41
- console.log(
42
- chalk.bgRed(" ERROR ") +
43
- chalk.dim(":") +
44
- " " +
45
- chalk.bold("Unknown command: ") +
46
- args[0],
47
- );
48
- }
49
-
50
- function info(): void {
51
- console.log(
52
- chalk.bold.blueBright("as-test") +
53
- " is a testing framework for AssemblyScript. " +
54
- chalk.dim("(v" + version + ")") +
55
- "\n",
56
- );
57
-
58
- console.log(
59
- chalk.bold("Usage: as-test") +
60
- " " +
61
- chalk.dim("<command>") +
62
- " " +
63
- chalk.bold.blueBright("[...flags]") +
64
- " " +
65
- chalk.bold("[...args]") +
66
- " " +
67
- chalk.dim("(alias: ast)") +
68
- "\n",
69
- );
70
-
71
- console.log(chalk.bold("Commands:"));
72
- console.log(
73
- " " +
74
- chalk.bold.blueBright("run") +
75
- " " +
76
- chalk.dim("<my-test.spec.ts>") +
77
- " " +
78
- "Run unit tests with selected runtime",
79
- );
80
- console.log(
81
- " " +
82
- chalk.bold.blueBright("build") +
83
- " " +
84
- chalk.dim("<my-test.spec.ts>") +
85
- " " +
86
- "Build unit tests and compile",
87
- );
88
- console.log(
89
- " " +
90
- chalk.bold.blueBright("test") +
91
- " " +
92
- chalk.dim("<my-test.spec.ts>") +
93
- " " +
94
- "Build and run unit tests with selected runtime" +
95
- "\n",
96
- );
97
-
98
- console.log(
99
- " " +
100
- chalk.bold.magentaBright("init") +
101
- " " +
102
- chalk.strikethrough.dim("") +
103
- " " +
104
- "Initialize an empty testing template",
105
- );
106
- console.log(
107
- " " +
108
- chalk.strikethrough.bold.magentaBright("config") +
109
- " " +
110
- chalk.strikethrough.dim("as-test.config.json") +
111
- " " +
112
- "Specify the configuration file",
113
- );
114
- console.log(
115
- " " +
116
- chalk.strikethrough.bold.magentaBright("reporter") +
117
- " " +
118
- chalk.strikethrough.dim("<tap>") +
119
- " " +
120
- "Specify the test reporter to use",
121
- );
122
- console.log(
123
- " " +
124
- chalk.strikethrough.bold.magentaBright("use") +
125
- " " +
126
- chalk.strikethrough.dim("wasmtime") +
127
- " " +
128
- "Specify the runtime to use" +
129
- "\n",
130
- );
131
-
132
- console.log(chalk.bold("Flags:"));
133
-
134
- console.log(
135
- " " +
136
- chalk.strikethrough.dim("run") +
137
- " " +
138
- chalk.strikethrough.bold.blue("--coverage") +
139
- " " +
140
- "Use code coverage",
141
- );
142
- console.log(
143
- " " +
144
- chalk.strikethrough.dim("run") +
145
- " " +
146
- chalk.strikethrough.bold.blue("--snapshot") +
147
- " " +
148
- "Take a snapshot of the tests",
149
- );
150
- console.log(
151
- " " +
152
- chalk.strikethrough.dim("use") +
153
- " " +
154
- chalk.strikethrough.bold.blue("--list") +
155
- " " +
156
- "List supported runtimes",
157
- );
158
- console.log(
159
- " " +
160
- chalk.strikethrough.dim("reporter") +
161
- " " +
162
- chalk.strikethrough.bold.blue("--list") +
163
- " " +
164
- "List supported reporters",
165
- );
166
- console.log(
167
- " " +
168
- chalk.strikethrough.dim("<command>") +
169
- " " +
170
- chalk.strikethrough.bold.blue("--help") +
171
- " " +
172
- "Print info about command" +
173
- "\n",
174
- );
175
-
176
- console.log(
177
- chalk.dim(
178
- "If your using this, consider dropping a star, it would help a lot!",
179
- ) + "\n",
180
- );
181
-
182
- console.log(
183
- "View the repo: " +
184
- chalk.magenta("https://github.com/JairusSW/as-test"),
185
- );
186
- console.log(
187
- "View the docs: " +
188
- chalk.strikethrough.blue("https://docs.jairus.dev/as-test"),
189
- );
190
- }
package/cli/init.ts DELETED
@@ -1,247 +0,0 @@
1
- import chalk from "chalk";
2
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
3
- import * as path from "path";
4
- import { createInterface, Interface } from "readline";
5
- import { loadConfig } from "./util.js";
6
- const TARGETS = ["wasi", "bindings"];
7
- export async function init(args: string[]) {
8
- const rl = createInterface({
9
- input: process.stdin,
10
- output: process.stdout,
11
- });
12
- console.log(chalk.bold("as-test init v0.3.5") + "\n");
13
- console.log(chalk.dim("[1/3]") + " select a target [wasi/bindings]");
14
- const target = await ask(chalk.dim(" -> "), rl);
15
- if (!TARGETS.includes(target)) {
16
- console.log("Invalid target " + target + ". Exiting.");
17
- process.exit(0);
18
- }
19
- process.stdout.write(`\u001B[1A`);
20
- process.stdout.write("\x1B[2K");
21
- process.stdout.write("\x1B[0G");
22
- console.log(
23
- "\n" +
24
- chalk.dim("[2/3]") +
25
- " attempting to create the following files. Continue? [y/n]\n",
26
- );
27
- console.log(
28
- chalk.dim(
29
- ` ├── 📂 assembly/
30
- │ └── 📂 __tests__/
31
- │ └── 🧪 example.spec.ts
32
- ├── 📂 build/
33
- ├── 📂 logs/
34
- ├── 📂 tests/
35
- │ └── 📃 as-test.run.js
36
- ├── ⚙️ as-test.config.json
37
- └── ⚙️ package.json\n`,
38
- ),
39
- );
40
-
41
- const cont = (await ask(chalk.dim(" -> "), rl)).toLowerCase().trim();
42
-
43
- if (cont == "n" || cont == "no") {
44
- console.log("Exiting.");
45
- process.exit(0);
46
- }
47
-
48
- let config = loadConfig(path.join(process.cwd(), "./as-test.config.json"));
49
- if (target == "wasi" && config.buildOptions.target != "wasi") {
50
- config.buildOptions.target = "wasi";
51
- config.runOptions.runtime.name = "wasmtime";
52
- config.runOptions.runtime.run = "wasmtime <file>";
53
- } else if (target == "bindings" && config.buildOptions.target != "bindings") {
54
- config.buildOptions.target = "bindings";
55
- config.runOptions.runtime.name = "node";
56
- config.runOptions.runtime.run = "node ./tests/<name>.run.js";
57
- }
58
-
59
- writeFile("./as-test.config.json", JSON.stringify(config, null, 2));
60
-
61
- writeFile(
62
- "./assembly/__tests__/example.spec.ts",
63
- `import {
64
- describe,
65
- expect,
66
- test,
67
- beforeAll,
68
- afterAll,
69
- mockFn,
70
- log,
71
- run,
72
- it
73
- } from "as-test";
74
-
75
- beforeAll(() => {
76
- log("Setting up test environment...");
77
- });
78
-
79
- afterAll(() => {
80
- log("Tearing down test environment...");
81
- });
82
-
83
- // Mock/override the function console.log
84
- mockFn<void>("console.log", (data: string): void => {
85
- console.log("[MOCKED]: " + data + "\\n");
86
- });
87
-
88
- describe("Should sleep", () => {
89
- test("1ms", () => {
90
- const start = Date.now();
91
- sleep(1);
92
- expect(Date.now() - start).toBeGreaterOrEqualTo(1);
93
- });
94
- test("10ms", () => {
95
- const start = Date.now();
96
- sleep(10);
97
- expect(Date.now() - start).toBeGreaterOrEqualTo(10);
98
- });
99
- test("1s", () => {
100
- const start = Date.now();
101
- sleep(1000);
102
- expect(Date.now() - start).toBeGreaterOrEqualTo(1000);
103
- });
104
- test("5s", () => {
105
- const start = Date.now();
106
- log("Sleeping...");
107
- sleep(5000);
108
- log("Done!");
109
- expect(Date.now() - start).toBeGreaterOrEqualTo(5000);
110
- });
111
- });
112
-
113
- describe("Math operations", () => {
114
- test("Addition", () => {
115
- expect(1 + 2).toBe(3);
116
- });
117
-
118
- test("Subtraction", () => {
119
- expect(1 - 2).toBe(-1);
120
- });
121
-
122
- test("Comparison", () => {
123
- expect(5).toBeGreaterThan(3);
124
- expect(2).toBeLessThan(4);
125
- });
126
-
127
- test("Type checking", () => {
128
- expect("hello").toBeString();
129
- expect(true).toBeBoolean();
130
- expect(10.5).toBeNumber();
131
- });
132
- });
133
-
134
- let myArray: i32[] = [1, 2, 3];
135
-
136
- describe("Array manipulation", () => {
137
- test("Array length", () => {
138
- expect(myArray).toHaveLength(3);
139
- });
140
-
141
- test("Array inclusion", () => {
142
- expect(myArray).toContain(2);
143
- });
144
-
145
- it("should be empty", () => { });
146
- });
147
-
148
- run();
149
-
150
- function sleep(ms: i64): void {
151
- const target = Date.now() + ms;
152
- while (target > Date.now()) { }
153
- }`,
154
- );
155
-
156
- writeDir("./build/");
157
- writeDir("./logs/");
158
-
159
- if (target == "bindings") {
160
- writeFile(
161
- "./tests/example.run.js",
162
- `import { readFileSync } from "fs";
163
- import { instantiate } from "../build/example.spec.js";
164
-
165
- const binary = readFileSync("./build/example.spec.wasm");
166
- const module = new WebAssembly.Module(binary);
167
-
168
- const exports = instantiate(module, {});`,
169
- );
170
- }
171
-
172
- const PKG_PATH = path.join(process.cwd(), "./package.json");
173
- if (!hasDep(PKG_PATH, "assemblyscript")) {
174
- console.log(
175
- chalk.dim(
176
- "AssemblyScript is not included in dependencies.\nInstall it with " +
177
- (process.env.npm_config_user_agent == "yarn"
178
- ? process.env.npm_config_user_agent + " add assemblyscript"
179
- : process.env.npm_config_user_agent + " install assemblyscript"),
180
- ),
181
- );
182
- }
183
- const pkg = JSON.parse(
184
- existsSync(PKG_PATH) ? readFileSync(PKG_PATH).toString() : "{}",
185
- );
186
- if (!pkg["scripts"]) pkg["scripts"] = {};
187
- if (!pkg.scripts["pretest"]) {
188
- pkg.scripts["pretest"] = "as-test build";
189
- pkg.scripts["test"] = "as-test run";
190
- } else {
191
- pkg.scripts["test"] = "as-test test";
192
- }
193
- if (!pkg["devDependencies"]) pkg["devDependencies"] = {};
194
- if (!pkg["devDependencies"]["as-test"])
195
- pkg["devDependencies"]["as-test"] = "^0.3.5";
196
- if (target == "bindings") {
197
- pkg["type"] = "module";
198
- }
199
- writeFileSync(PKG_PATH, JSON.stringify(pkg, null, 2));
200
- process.exit(0);
201
- }
202
-
203
- function ask(question: string, face: Interface): Promise<string> {
204
- return new Promise<string>((res, _) => {
205
- face.question(question, (answer) => {
206
- res(answer);
207
- });
208
- });
209
- }
210
-
211
- function writeFile(pth: string, data: string) {
212
- const fmtPath = path.join(process.cwd(), pth);
213
- if (existsSync(fmtPath)) return;
214
- if (!existsSync(path.dirname(fmtPath)))
215
- mkdirSync(path.dirname(fmtPath), { recursive: true });
216
- writeFileSync(fmtPath, data);
217
- }
218
-
219
- function writeDir(pth: string) {
220
- const fmtPath = path.join(process.cwd(), pth);
221
- if (existsSync(fmtPath)) return;
222
- mkdirSync(fmtPath);
223
- }
224
-
225
- function hasDep(PKG_PATH: string, dep: string): boolean {
226
- const pkg = JSON.parse(
227
- existsSync(PKG_PATH) ? readFileSync(PKG_PATH).toString() : "{}",
228
- ) as {
229
- dependencies: string[] | null;
230
- devDependencies: string[] | null;
231
- peerDependencies: string[] | null;
232
- };
233
-
234
- if (existsSync(path.join(process.cwd(), "./node_modules/", dep))) return true;
235
-
236
- if (
237
- pkg.dependencies &&
238
- !Object.keys(pkg.dependencies).includes(dep) &&
239
- pkg.devDependencies &&
240
- !Object.keys(pkg.devDependencies).includes(dep) &&
241
- pkg.peerDependencies &&
242
- !Object.keys(pkg.peerDependencies).includes(dep)
243
- ) {
244
- return false;
245
- }
246
- return true;
247
- }
package/cli/reporter.ts DELETED
@@ -1 +0,0 @@
1
- export function report(): void {}