as-test 0.3.0 → 0.3.2

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.
@@ -19,9 +19,6 @@ jobs:
19
19
  - name: Install dependencies
20
20
  run: bun install
21
21
 
22
- - name: Build reporter
23
- run: bun run build:log
24
-
25
22
  - name: Build tests
26
23
  run: bun run pretest
27
24
 
package/CHANGELOG.md CHANGED
@@ -24,3 +24,5 @@ v0.2.0 - Fix mock -> mockFn artifacts
24
24
  v0.2.1 - Remove accidental logging
25
25
 
26
26
  v0.3.0 - Pass metadata through terminal - Support for multiple files - Better reporting - Timing for suites - Terminal utilities
27
+ v0.3.1 - Add screenshot of completed tests to readme
28
+ v0.3.2 - Add `mockImport` to override imported functions
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  | _ || __| ___|_ _|| __|| __||_ _|
4
4
  | ||__ ||___| | | | __||__ | | |
5
5
  |__|__||_____| |_| |_____||_____| |_|
6
- v0.3.0
6
+ v0.3.2
7
7
  </pre>
8
8
  </h5>
9
9
 
@@ -59,6 +59,8 @@ mockFn<void>("console.log", (data: string): void => {
59
59
  console.log("[MOCKED]: " + data + "\n");
60
60
  });
61
61
 
62
+ // Or override an imported function with mockImport
63
+
62
64
  describe("Should sleep", () => {
63
65
  test("1ms", () => {
64
66
  const start = Date.now();
@@ -133,19 +135,12 @@ Build and run it using as-test
133
135
  npm run test
134
136
  ```
135
137
 
136
- <h6>
137
-
138
138
  ## Running
139
139
 
140
140
  To add `as-test` to your CI/CD workflow, check out [The provided example](https://github.com/JairusSW/as-test/blob/main/.github/workflows/nodejs.yml)
141
141
 
142
142
  If you use this project in your codebase, consider dropping a [⭐ HERE](https://github.com/JairusSW/as-test). I would really appreciate it!
143
143
 
144
- ## Notes
145
-
146
- This library is in the EARLY STAGES OF DEVELOPMENT!
147
- If you want a feature, drop an issue (and again, maybe a star). I'll likely add it in less than 7 days.
148
-
149
144
  ## Issues
150
145
 
151
146
  Please submit an issue to https://github.com/JairusSW/as-test/issues if you find anything wrong with this library
@@ -0,0 +1,22 @@
1
+ import { expect, it, log, mockFn, mockImport, run } from "..";
2
+ import { foo, getFoo } from "./mock";
3
+
4
+ // function foo(): string {
5
+ // return "bar";
6
+ // }
7
+
8
+ mockFn(foo, (): string => {
9
+ return "baz " + foo();
10
+ });
11
+
12
+ mockImport("mock.foo", (): string => {
13
+ return "biz";
14
+ });
15
+
16
+ it("should mock functions", () => {
17
+ log(foo());
18
+ expect(foo()).toBe("baz biz");
19
+ expect(getFoo()).toBe("biz");
20
+ });
21
+
22
+ run();
@@ -0,0 +1,7 @@
1
+
2
+ @external("mock", "foo")
3
+ export declare function foo(): string;
4
+
5
+ export function getFoo(): string {
6
+ return foo();
7
+ }
package/assembly/index.ts CHANGED
@@ -4,7 +4,7 @@ import { Expectation } from "./src/expectation";
4
4
  import { stringify } from "as-console/stringify";
5
5
  import { __COVER, __HASHES, __POINTS } from "as-test/assembly/coverage";
6
6
  import { JSON } from "json-as";
7
- import { term, TermLine } from "./util/term";
7
+ import { term } from "./util/term";
8
8
  import { Log } from "./src/log";
9
9
 
10
10
  let entrySuites: Suite[] = [];
@@ -13,6 +13,10 @@ let entrySuites: Suite[] = [];
13
13
  const FILE = isDefined(ENTRY_FILE) ? ENTRY_FILE : "unknown";
14
14
  // Globals
15
15
  // @ts-ignore
16
+ @global let __mock_global: Map<string, u32> = new Map<string, u32>();
17
+ // @ts-ignore
18
+ @global let __mock_import: Map<string, u32> = new Map<string, u32>();
19
+ // @ts-ignore
16
20
  @global let suites: Suite[] = [];
17
21
  // @ts-ignore
18
22
  @global let depth: i32 = -1;
@@ -215,26 +219,16 @@ export function afterEach(callback: () => void): void {
215
219
  }
216
220
 
217
221
  /**
218
- * Overrides all references to an existing function in local scope to instead point to new function
219
- * @param {string} fn - name of function to override
220
- * @param {() => returnType} callback - the function to substitute it with
221
- */
222
- export function mockFn<returnType>(
223
- fn: string,
224
- callback: (...args: any[]) => returnType,
225
- ): void {}
226
-
227
- /**
228
- * Unmock all references to an existing function to instead point to the original function
229
- * @param {string} fn - name of function to override
222
+ * Replace all references to an existing function to new function
223
+ * @param {Function} oldFn - name of function to mock
224
+ * @param {Function} newFn - the function to substitute it with
230
225
  */
231
- export function unmockFn(fn: string): void {}
226
+ export function mockFn(oldFn: Function, newFn: Function): void {}
232
227
 
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(fn: string): void {}
228
+ export function mockImport(oldFn: string, newFn: () => string): void {
229
+ __mock_import.set(oldFn, newFn.index);
230
+ // mocks.set(oldFn, new MockFn(oldFn, newFn).enable());
231
+ }
238
232
 
239
233
  /**
240
234
  * Class defining options that can be passed to the `run` function.
Binary file
package/bin/build.js CHANGED
@@ -7,80 +7,92 @@ import { loadConfig } from "./util.js";
7
7
  const CONFIG_PATH = path.join(process.cwd(), "./as-test.config.json");
8
8
  const PKG_PATH = path.join(process.cwd(), "./package.json");
9
9
  export async function build() {
10
- let config = loadConfig(CONFIG_PATH, true);
11
- const ASCONFIG_PATH = path.join(process.cwd(), config.config);
12
- if (config.config && config.config !== "none" && !existsSync(ASCONFIG_PATH)) {
13
- console.log(`${chalk.bgMagentaBright(" WARN ")}${chalk.dim(":")} Could not locate asconfig.json file! If you do not want to provide a config, set "config": "none"`);
14
- }
15
- ensureDeps(config);
16
- let pkgRunner = getPkgRunner();
17
- const inputFiles = await glob(config.input);
18
- let buildArgs = getBuildArgs(config);
19
- for (const file of inputFiles) {
20
- let cmd = `${pkgRunner} asc ${file}${buildArgs}`;
21
- const outFile = `${config.outDir}/${file.slice(file.lastIndexOf("/") + 1).replace(".ts", ".wasm")}`;
22
- if (config.outDir) {
23
- cmd += " -o " + outFile;
24
- }
25
- buildFile(cmd);
10
+ let config = loadConfig(CONFIG_PATH, true);
11
+ const ASCONFIG_PATH = path.join(process.cwd(), config.config);
12
+ if (config.config && config.config !== "none" && !existsSync(ASCONFIG_PATH)) {
13
+ console.log(
14
+ `${chalk.bgMagentaBright(" WARN ")}${chalk.dim(":")} Could not locate asconfig.json file! If you do not want to provide a config, set "config": "none"`,
15
+ );
16
+ }
17
+ ensureDeps(config);
18
+ let pkgRunner = getPkgRunner();
19
+ const inputFiles = await glob(config.input);
20
+ let buildArgs = getBuildArgs(config);
21
+ for (const file of inputFiles) {
22
+ let cmd = `${pkgRunner} asc ${file}${buildArgs}`;
23
+ const outFile = `${config.outDir}/${file.slice(file.lastIndexOf("/") + 1).replace(".ts", ".wasm")}`;
24
+ if (config.outDir) {
25
+ cmd += " -o " + outFile;
26
26
  }
27
+ buildFile(cmd);
28
+ }
27
29
  }
28
30
  function ensureDeps(config) {
29
- const pkg = JSON.parse(readFileSync(PKG_PATH).toString());
30
- if (config.buildOptions.target == "wasi") {
31
- if (!existsSync("./node_modules/@assemblyscript/wasi-shim/asconfig.json")) {
32
- console.log(`${chalk.bgRed(" ERROR ")}${chalk.dim(":")} could not find @assemblyscript/wasi-shim! Add it to your dependencies to run with WASI!`);
33
- process.exit(1);
34
- }
35
- if (pkg.dependencies &&
36
- !Object.keys(pkg.dependencies).includes("@assemblyscript/wasi-shim") &&
37
- pkg.devDependencies &&
38
- !Object.keys(pkg.devDependencies).includes("@assemblyscript/wasi-shim") &&
39
- pkg.peerDependencies &&
40
- !Object.keys(pkg.peerDependencies).includes("@assemblyscript/wasi-shim") &&
41
- existsSync("./node_modules/@assemblyscript/wasi-shim/asconfig.json")) {
42
- console.log(`${chalk.bold.bgMagentaBright(" WARN ")}${chalk.dim(":")} @assemblyscript/wasi-shim is not included in project dependencies!"`);
43
- }
31
+ const pkg = JSON.parse(readFileSync(PKG_PATH).toString());
32
+ if (config.buildOptions.target == "wasi") {
33
+ if (!existsSync("./node_modules/@assemblyscript/wasi-shim/asconfig.json")) {
34
+ console.log(
35
+ `${chalk.bgRed(" ERROR ")}${chalk.dim(":")} could not find @assemblyscript/wasi-shim! Add it to your dependencies to run with WASI!`,
36
+ );
37
+ process.exit(1);
38
+ }
39
+ if (
40
+ pkg.dependencies &&
41
+ !Object.keys(pkg.dependencies).includes("@assemblyscript/wasi-shim") &&
42
+ pkg.devDependencies &&
43
+ !Object.keys(pkg.devDependencies).includes("@assemblyscript/wasi-shim") &&
44
+ pkg.peerDependencies &&
45
+ !Object.keys(pkg.peerDependencies).includes(
46
+ "@assemblyscript/wasi-shim",
47
+ ) &&
48
+ existsSync("./node_modules/@assemblyscript/wasi-shim/asconfig.json")
49
+ ) {
50
+ console.log(
51
+ `${chalk.bold.bgMagentaBright(" WARN ")}${chalk.dim(":")} @assemblyscript/wasi-shim is not included in project dependencies!"`,
52
+ );
44
53
  }
54
+ }
45
55
  }
46
56
  function getPkgRunner() {
47
- switch (process.env.npm_config_user_agent) {
48
- case "pnpm": {
49
- return "pnpx";
50
- }
51
- case "yarn": {
52
- return "yarn";
53
- }
54
- case "bun": {
55
- return "bunx";
56
- }
57
+ switch (process.env.npm_config_user_agent) {
58
+ case "pnpm": {
59
+ return "pnpx";
60
+ }
61
+ case "yarn": {
62
+ return "yarn";
57
63
  }
58
- return "npx";
64
+ case "bun": {
65
+ return "bunx";
66
+ }
67
+ }
68
+ return "npx";
59
69
  }
60
70
  function buildFile(command) {
61
- execSync(command, { stdio: "inherit" });
71
+ execSync(command, { stdio: "inherit" });
62
72
  }
63
73
  function getBuildArgs(config) {
64
- let buildArgs = "";
65
- buildArgs += " --transform as-test/transform --transform json-as/transform";
66
- if (config.config && config.config !== "none") {
67
- buildArgs += " --config " + config.config;
68
- }
69
- // Should also strip any bindings-enabling from asconfig
70
- if (config.buildOptions.target == "bindings") {
71
- buildArgs += " --bindings raw --exportRuntime --exportStart _start";
72
- }
73
- else if (config.buildOptions.target == "wasi") {
74
- buildArgs +=
75
- " --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json";
76
- }
77
- else {
78
- console.log(`${chalk.bgRed(" ERROR ")}${chalk.dim(":")} could determine target in config! Set target to 'bindings' or 'wasi'`);
79
- process.exit(0);
80
- }
81
- if (config.buildOptions.args.length &&
82
- config.buildOptions.args.find((v) => v.length > 0)) {
83
- buildArgs += " " + config.buildOptions.args.join(" ");
84
- }
85
- return buildArgs;
74
+ let buildArgs = "";
75
+ buildArgs += " --transform as-test/transform --transform json-as/transform";
76
+ if (config.config && config.config !== "none") {
77
+ buildArgs += " --config " + config.config;
78
+ }
79
+ // Should also strip any bindings-enabling from asconfig
80
+ if (config.buildOptions.target == "bindings") {
81
+ buildArgs += " --bindings raw --exportRuntime --exportStart _start";
82
+ } else if (config.buildOptions.target == "wasi") {
83
+ buildArgs +=
84
+ " --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json";
85
+ } else {
86
+ console.log(
87
+ `${chalk.bgRed(" ERROR ")}${chalk.dim(":")} could determine target in config! Set target to 'bindings' or 'wasi'`,
88
+ );
89
+ process.exit(0);
90
+ }
91
+ if (
92
+ config.buildOptions.args.length &&
93
+ config.buildOptions.args.find((v) => v.length > 0)
94
+ ) {
95
+ buildArgs += " " + config.buildOptions.args.join(" ");
96
+ }
97
+ return buildArgs;
86
98
  }
package/bin/index.js CHANGED
@@ -7,140 +7,170 @@ const _args = process.argv.slice(2);
7
7
  const flags = [];
8
8
  const args = [];
9
9
  const COMMANDS = ["run", "build", "test", "init"];
10
- const version = "0.3.0";
10
+ const version = "0.3.2";
11
11
  for (const arg of _args) {
12
- if (arg.startsWith("-"))
13
- flags.push(arg);
14
- else
15
- args.push(arg);
12
+ if (arg.startsWith("-")) flags.push(arg);
13
+ else args.push(arg);
16
14
  }
17
15
  if (!args.length) {
18
- if (flags.includes("--version") || flags.includes("-v")) {
19
- console.log("as-test v" + version.toString());
20
- }
21
- else {
22
- info();
23
- }
24
- }
25
- else if (COMMANDS.includes(args[0])) {
26
- const command = args.shift();
27
- if (command === "build") {
28
- build();
29
- }
30
- else if (command === "run") {
31
- run();
32
- }
33
- else if (command === "test") {
34
- build().then(() => {
35
- run();
36
- });
37
- }
38
- else if (command === "init") {
39
- init(args);
40
- }
41
- }
42
- else {
43
- console.log(chalk.bgRed(" ERROR ") +
44
- chalk.dim(":") +
45
- " " +
46
- chalk.bold("Unknown command: ") +
47
- args[0]);
16
+ if (flags.includes("--version") || flags.includes("-v")) {
17
+ console.log("as-test v" + version.toString());
18
+ } else {
19
+ info();
20
+ }
21
+ } else if (COMMANDS.includes(args[0])) {
22
+ const command = args.shift();
23
+ if (command === "build") {
24
+ build();
25
+ } else if (command === "run") {
26
+ run();
27
+ } else if (command === "test") {
28
+ build().then(() => {
29
+ run();
30
+ });
31
+ } else if (command === "init") {
32
+ init(args);
33
+ }
34
+ } else {
35
+ console.log(
36
+ chalk.bgRed(" ERROR ") +
37
+ chalk.dim(":") +
38
+ " " +
39
+ chalk.bold("Unknown command: ") +
40
+ args[0],
41
+ );
48
42
  }
49
43
  function info() {
50
- console.log(chalk.bold.blueBright("as-test") +
51
- " is a testing framework for AssemblyScript. " +
52
- chalk.dim("(v" + version + ")") +
53
- "\n");
54
- console.log(chalk.bold("Usage: as-test") +
55
- " " +
56
- chalk.dim("<command>") +
57
- " " +
58
- chalk.bold.blueBright("[...flags]") +
59
- " " +
60
- chalk.bold("[...args]") +
61
- " " +
62
- chalk.dim("(alias: ast)") +
63
- "\n");
64
- console.log(chalk.bold("Commands:"));
65
- console.log(" " +
66
- chalk.bold.blueBright("run") +
67
- " " +
68
- chalk.dim("<my-test.spec.ts>") +
69
- " " +
70
- "Run unit tests with selected runtime");
71
- console.log(" " +
72
- chalk.bold.blueBright("build") +
73
- " " +
74
- chalk.dim("<my-test.spec.ts>") +
75
- " " +
76
- "Build unit tests and compile");
77
- console.log(" " +
78
- chalk.bold.blueBright("test") +
79
- " " +
80
- chalk.dim("<my-test.spec.ts>") +
81
- " " +
82
- "Build and run unit tests with selected runtime" +
83
- "\n");
84
- console.log(" " +
85
- chalk.bold.magentaBright("init") +
86
- " " +
87
- chalk.strikethrough.dim("") +
88
- " " +
89
- "Initialize an empty testing template");
90
- console.log(" " +
91
- chalk.strikethrough.bold.magentaBright("config") +
92
- " " +
93
- chalk.strikethrough.dim("as-test.config.json") +
94
- " " +
95
- "Specify the configuration file");
96
- console.log(" " +
97
- chalk.strikethrough.bold.magentaBright("reporter") +
98
- " " +
99
- chalk.strikethrough.dim("<tap>") +
100
- " " +
101
- "Specify the test reporter to use");
102
- console.log(" " +
103
- chalk.strikethrough.bold.magentaBright("use") +
104
- " " +
105
- chalk.strikethrough.dim("wasmtime") +
106
- " " +
107
- "Specify the runtime to use" +
108
- "\n");
109
- console.log(chalk.bold("Flags:"));
110
- console.log(" " +
111
- chalk.strikethrough.dim("run") +
112
- " " +
113
- chalk.strikethrough.bold.blue("--coverage") +
114
- " " +
115
- "Use code coverage");
116
- console.log(" " +
117
- chalk.strikethrough.dim("run") +
118
- " " +
119
- chalk.strikethrough.bold.blue("--snapshot") +
120
- " " +
121
- "Take a snapshot of the tests");
122
- console.log(" " +
123
- chalk.strikethrough.dim("use") +
124
- " " +
125
- chalk.strikethrough.bold.blue("--list") +
126
- " " +
127
- "List supported runtimes");
128
- console.log(" " +
129
- chalk.strikethrough.dim("reporter") +
130
- " " +
131
- chalk.strikethrough.bold.blue("--list") +
132
- " " +
133
- "List supported reporters");
134
- console.log(" " +
135
- chalk.strikethrough.dim("<command>") +
136
- " " +
137
- chalk.strikethrough.bold.blue("--help") +
138
- " " +
139
- "Print info about command" +
140
- "\n");
141
- console.log(chalk.dim("If your using this, consider dropping a star, it would help a lot!") + "\n");
142
- console.log("View the repo: " +
143
- chalk.magenta("https://github.com/JairusSW/as-test"));
144
- console.log("View the docs: " +
145
- chalk.strikethrough.blue("https://docs.jairus.dev/as-test"));
44
+ console.log(
45
+ chalk.bold.blueBright("as-test") +
46
+ " is a testing framework for AssemblyScript. " +
47
+ chalk.dim("(v" + version + ")") +
48
+ "\n",
49
+ );
50
+ console.log(
51
+ chalk.bold("Usage: as-test") +
52
+ " " +
53
+ chalk.dim("<command>") +
54
+ " " +
55
+ chalk.bold.blueBright("[...flags]") +
56
+ " " +
57
+ chalk.bold("[...args]") +
58
+ " " +
59
+ chalk.dim("(alias: ast)") +
60
+ "\n",
61
+ );
62
+ console.log(chalk.bold("Commands:"));
63
+ console.log(
64
+ " " +
65
+ chalk.bold.blueBright("run") +
66
+ " " +
67
+ chalk.dim("<my-test.spec.ts>") +
68
+ " " +
69
+ "Run unit tests with selected runtime",
70
+ );
71
+ console.log(
72
+ " " +
73
+ chalk.bold.blueBright("build") +
74
+ " " +
75
+ chalk.dim("<my-test.spec.ts>") +
76
+ " " +
77
+ "Build unit tests and compile",
78
+ );
79
+ console.log(
80
+ " " +
81
+ chalk.bold.blueBright("test") +
82
+ " " +
83
+ chalk.dim("<my-test.spec.ts>") +
84
+ " " +
85
+ "Build and run unit tests with selected runtime" +
86
+ "\n",
87
+ );
88
+ console.log(
89
+ " " +
90
+ chalk.bold.magentaBright("init") +
91
+ " " +
92
+ chalk.strikethrough.dim("") +
93
+ " " +
94
+ "Initialize an empty testing template",
95
+ );
96
+ console.log(
97
+ " " +
98
+ chalk.strikethrough.bold.magentaBright("config") +
99
+ " " +
100
+ chalk.strikethrough.dim("as-test.config.json") +
101
+ " " +
102
+ "Specify the configuration file",
103
+ );
104
+ console.log(
105
+ " " +
106
+ chalk.strikethrough.bold.magentaBright("reporter") +
107
+ " " +
108
+ chalk.strikethrough.dim("<tap>") +
109
+ " " +
110
+ "Specify the test reporter to use",
111
+ );
112
+ console.log(
113
+ " " +
114
+ chalk.strikethrough.bold.magentaBright("use") +
115
+ " " +
116
+ chalk.strikethrough.dim("wasmtime") +
117
+ " " +
118
+ "Specify the runtime to use" +
119
+ "\n",
120
+ );
121
+ console.log(chalk.bold("Flags:"));
122
+ console.log(
123
+ " " +
124
+ chalk.strikethrough.dim("run") +
125
+ " " +
126
+ chalk.strikethrough.bold.blue("--coverage") +
127
+ " " +
128
+ "Use code coverage",
129
+ );
130
+ console.log(
131
+ " " +
132
+ chalk.strikethrough.dim("run") +
133
+ " " +
134
+ chalk.strikethrough.bold.blue("--snapshot") +
135
+ " " +
136
+ "Take a snapshot of the tests",
137
+ );
138
+ console.log(
139
+ " " +
140
+ chalk.strikethrough.dim("use") +
141
+ " " +
142
+ chalk.strikethrough.bold.blue("--list") +
143
+ " " +
144
+ "List supported runtimes",
145
+ );
146
+ console.log(
147
+ " " +
148
+ chalk.strikethrough.dim("reporter") +
149
+ " " +
150
+ chalk.strikethrough.bold.blue("--list") +
151
+ " " +
152
+ "List supported reporters",
153
+ );
154
+ console.log(
155
+ " " +
156
+ chalk.strikethrough.dim("<command>") +
157
+ " " +
158
+ chalk.strikethrough.bold.blue("--help") +
159
+ " " +
160
+ "Print info about command" +
161
+ "\n",
162
+ );
163
+ console.log(
164
+ chalk.dim(
165
+ "If your using this, consider dropping a star, it would help a lot!",
166
+ ) + "\n",
167
+ );
168
+ console.log(
169
+ "View the repo: " +
170
+ chalk.magenta("https://github.com/JairusSW/as-test"),
171
+ );
172
+ console.log(
173
+ "View the docs: " +
174
+ chalk.strikethrough.blue("https://docs.jairus.dev/as-test"),
175
+ );
146
176
  }