as-test 0.1.9 → 0.2.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/CHANGELOG.md CHANGED
@@ -18,5 +18,6 @@ v0.1.6 - Fix: args should be prefixed with a space
18
18
  v0.1.7 - Fix: remove warning about wasi-shim not being included when it is included
19
19
  v0.1.8 - Feat: function mocking
20
20
  v0.1.9 - Fix: mocks were not being applied to declared functions, only property accesses
21
-
22
- v0.1.10 [UNRELEASED] - Feat: snapshotting
21
+ v0.1.10 - Feat: support node, deno, and bun
22
+ v0.2.0 - Fix mock -> mockFn artifacts
23
+ [UNRELEASED] - Feat: snapshotting
package/README.md CHANGED
@@ -3,17 +3,28 @@
3
3
  | _ || __| ___|_ _|| __|| __||_ _|
4
4
  | ||__ ||___| | | | __||__ | | |
5
5
  |__|__||_____| |_| |_____||_____| |_|
6
- v0.1.9
6
+ v0.2.0
7
7
  </pre>
8
8
  </h5>
9
9
 
10
10
  ## Installation
11
11
 
12
- ```bash
13
- npm install as-test
14
- ```
12
+ To get started, install the package from NPM or GitHub
13
+
14
+ `npm i as-test --save-dev`
15
+
16
+ You'll also need to install `visitor-as`
17
+
18
+ `npm i visitor-as --save-dev`
19
+
20
+ ## Templates
21
+
22
+ I provide two templates for reference
15
23
 
16
- Note: The transform _is_ OPTIONAL, though it is required to enable code coverage.
24
+ [WASI](https://github.com/JairusSW/as-test/tree/template/wasi)
25
+ [Node/Bun/Deno](https://github.com/JairusSW/as-test/tree/template/node-bun-deno)
26
+
27
+ View the docs: https://docs.jairus.dev/as-test
17
28
 
18
29
  ## Usage
19
30
 
@@ -38,7 +49,7 @@ import {
38
49
  afterAll,
39
50
  beforeEach,
40
51
  afterEach,
41
- mock,
52
+ mockFn,
42
53
  log,
43
54
  run
44
55
  } from "as-test";
@@ -52,7 +63,7 @@ afterAll(() => {
52
63
  });
53
64
 
54
65
  // Mock/override the function console.log
55
- mock("console.log", (data: string): void => {
66
+ mockFn<void>("console.log", (data: string): void => {
56
67
  console.log("[MOCKED]: " + data + "\n");
57
68
  });
58
69
 
@@ -129,16 +140,6 @@ If you use this project in your codebase, consider dropping a [⭐ HERE](https:/
129
140
  This library is in the EARLY STAGES OF DEVELOPMENT!
130
141
  If you want a feature, drop an issue (and again, maybe a star). I'll likely add it in less than 7 days.
131
142
 
132
- ## Contact
133
-
134
- Contact me at:
135
-
136
- Email: `me@jairus.dev`
137
-
138
- GitHub: `JairusSW`
139
-
140
- Discord: `jairussw`
141
-
142
143
  ## Issues
143
144
 
144
145
  Please submit an issue to https://github.com/JairusSW/as-test/issues if you find anything wrong with this library
package/asconfig.json CHANGED
@@ -26,8 +26,6 @@
26
26
  }
27
27
  },
28
28
  "options": {
29
- "transform": [],
30
- "disableWarning": [226]
31
- },
32
- "extends": "./node_modules/@assemblyscript/wasi-shim/asconfig.json"
29
+ "bindings": "esm"
30
+ }
33
31
  }
@@ -8,20 +8,10 @@ import {
8
8
  afterEach,
9
9
  log,
10
10
  run,
11
- mock,
11
+ mockFn,
12
12
  } from "..";
13
13
 
14
- function hello(a: i32, b: i32, c: i32): void {
15
- console.log("a: " + a.toString());
16
- console.log("b: " + b.toString());
17
- console.log("c: " + c.toString());
18
- }
19
-
20
- mock("hello", (a: i32, b: i32, c: i32): void => {
21
- hello(a + 10, b + 10, c + 10);
22
- });
23
-
24
- mock("console.log", (data: string): void => {
14
+ mockFn<void>("console.log", (data: string): void => {
25
15
  console.log("[MOCKED]: " + data);
26
16
  });
27
17
 
@@ -47,7 +37,6 @@ describe("Math operations", () => {
47
37
  });
48
38
 
49
39
  test("Mock", () => {
50
- hello(1, 2, 3);
51
40
  console.log("hello");
52
41
  });
53
42
 
package/assembly/index.ts CHANGED
@@ -175,7 +175,12 @@ export function afterEach(callback: () => void): void {
175
175
  after_each_callback = callback;
176
176
  }
177
177
 
178
- export function mock<returnType>(
178
+ /**
179
+ * Overrides all references to an existing function to instead point to this
180
+ * @param {string} fn - name of function to override
181
+ * @param {() => returnType} callback - the function to substitute it with
182
+ */
183
+ export function mockFn<returnType>(
179
184
  fn: string,
180
185
  callback: (...args: any[]) => returnType,
181
186
  ): void {}
@@ -235,7 +240,7 @@ export function run(options: RunOptions = new RunOptions()): void {
235
240
  ),
236
241
  );
237
242
  console.log(
238
- rainbow.dimMk("\n------------------- v0.1.9 -------------------\n"),
243
+ rainbow.dimMk("\n------------------- v0.2.0 -------------------\n"),
239
244
  );
240
245
  // @ts-ignore
241
246
  if (isDefined(COVERAGE_USE)) {
package/bin/build.js CHANGED
@@ -6,113 +6,140 @@ import { exec } from "child_process";
6
6
  import { formatTime } from "./util.js";
7
7
  import * as path from "path";
8
8
  export async function build(args) {
9
- const CONFIG_PATH = path.join(process.cwd(), "./as-test.config.json");
10
- let config;
11
- if (!existsSync(CONFIG_PATH)) {
12
- console.log(chalk.bgMagentaBright(" WARN ") +
13
- chalk.dim(":") +
14
- " Could not locate config file in the current directory! Continuing with default config." +
15
- "\n");
16
- config = new Config();
9
+ const CONFIG_PATH = path.join(process.cwd(), "./as-test.config.json");
10
+ let config;
11
+ if (!existsSync(CONFIG_PATH)) {
12
+ console.log(
13
+ chalk.bgMagentaBright(" WARN ") +
14
+ chalk.dim(":") +
15
+ " Could not locate config file in the current directory! Continuing with default config." +
16
+ "\n",
17
+ );
18
+ config = new Config();
19
+ } else {
20
+ config = Object.assign(
21
+ new Config(),
22
+ JSON.parse(readFileSync(CONFIG_PATH).toString()),
23
+ );
24
+ console.log(chalk.dim("Loading config from: " + CONFIG_PATH) + "\n");
25
+ }
26
+ const ASCONFIG_PATH = path.join(process.cwd(), config.config);
27
+ if (!existsSync(ASCONFIG_PATH)) {
28
+ console.log(
29
+ chalk.bgMagentaBright(" WARN ") +
30
+ chalk.dim(":") +
31
+ ' Could not locate asconfig.json file! If you do not want to provide a config, set "config": "none". Continuing with default config.' +
32
+ "\n",
33
+ );
34
+ }
35
+ const pkg = JSON.parse(readFileSync("./package.json").toString());
36
+ let buildCommands = [];
37
+ if (config.buildOptions.wasi) {
38
+ if (!existsSync("./node_modules/@assemblyscript/wasi-shim/asconfig.json")) {
39
+ console.log(
40
+ chalk.bgRed(" ERROR ") +
41
+ chalk.dim(":") +
42
+ " " +
43
+ "could not find @assemblyscript/wasi-shim! Add it to your dependencies to run with WASI!",
44
+ );
45
+ process.exit(1);
17
46
  }
18
- else {
19
- config = Object.assign(new Config(), JSON.parse(readFileSync(CONFIG_PATH).toString()));
20
- console.log(chalk.dim("Loading config from: " + CONFIG_PATH) + "\n");
47
+ if (
48
+ pkg.dependencies &&
49
+ !Object.keys(pkg.dependencies).includes("@assemblyscript/wasi-shim") &&
50
+ pkg.devDependencies &&
51
+ !Object.keys(pkg.devDependencies).includes("@assemblyscript/wasi-shim") &&
52
+ pkg.peerDependencies &&
53
+ !Object.keys(pkg.peerDependencies).includes(
54
+ "@assemblyscript/wasi-shim",
55
+ ) &&
56
+ existsSync("./node_modules/@assemblyscript/wasi-shim/asconfig.json")
57
+ ) {
58
+ console.log(
59
+ chalk.bold.bgMagentaBright(" WARN ") +
60
+ chalk.dim(": @assemblyscript/wasi-shim") +
61
+ " is not included in project dependencies!",
62
+ );
21
63
  }
22
- const ASCONFIG_PATH = path.join(process.cwd(), config.config);
23
- if (!existsSync(ASCONFIG_PATH)) {
24
- console.log(chalk.bgMagentaBright(" WARN ") +
25
- chalk.dim(":") +
26
- ' Could not locate asconfig.json file! If you do not want to provide a config, set "config": "none". Continuing with default config.' +
27
- "\n");
64
+ }
65
+ let packageManagerCommand = "npx";
66
+ if (
67
+ process.env.npm_config_user_agent &&
68
+ process.env.npm_config_user_agent.includes("pnpm")
69
+ ) {
70
+ packageManagerCommand = "pnpx";
71
+ } else if (
72
+ process.env.npm_config_user_agent &&
73
+ process.env.npm_config_user_agent.includes("yarn")
74
+ ) {
75
+ packageManagerCommand = "yarn run";
76
+ } else if (
77
+ process.env.npm_config_user_agent &&
78
+ process.env.npm_config_user_agent.includes("bun")
79
+ ) {
80
+ packageManagerCommand = "bunx";
81
+ }
82
+ console.log("");
83
+ const inputFiles = await glob(config.input);
84
+ for (const file of inputFiles) {
85
+ console.log(chalk.dim("Including " + file));
86
+ let command = `${packageManagerCommand} asc ${file}${args.length ? " " + args.join(" ") : ""}`;
87
+ if (config.config !== "none") {
88
+ command += " --config " + config.config;
28
89
  }
29
- const pkg = JSON.parse(readFileSync("./package.json").toString());
30
- let buildCommands = [];
31
90
  if (config.buildOptions.wasi) {
32
- if (!existsSync("./node_modules/@assemblyscript/wasi-shim/asconfig.json")) {
33
- console.log(chalk.bgRed(" ERROR ") +
34
- chalk.dim(":") +
35
- " " +
36
- "could not find @assemblyscript/wasi-shim! Add it to your dependencies to run with WASI!");
37
- process.exit(1);
38
- }
39
- if (pkg.dependencies &&
40
- !Object.keys(pkg.dependencies).includes("@assemblyscript/wasi-shim") &&
41
- pkg.devDependencies &&
42
- !Object.keys(pkg.devDependencies).includes("@assemblyscript/wasi-shim") &&
43
- pkg.peerDependencies &&
44
- !Object.keys(pkg.peerDependencies).includes("@assemblyscript/wasi-shim") &&
45
- existsSync("./node_modules/@assemblyscript/wasi-shim/asconfig.json")) {
46
- console.log(chalk.bold.bgMagentaBright(" WARN ") +
47
- chalk.dim(": @assemblyscript/wasi-shim") +
48
- " is not included in project dependencies!");
49
- }
91
+ command +=
92
+ " --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json";
50
93
  }
51
- let packageManagerCommand = "npx";
52
- if (process.env.npm_config_user_agent &&
53
- process.env.npm_config_user_agent.includes("pnpm")) {
54
- packageManagerCommand = "pnpx";
94
+ const outFile =
95
+ config.outDir +
96
+ "/" +
97
+ file.slice(file.lastIndexOf("/") + 1).replace(".ts", ".wasm");
98
+ if (config.outDir) {
99
+ command += " -o " + outFile;
55
100
  }
56
- else if (process.env.npm_config_user_agent &&
57
- process.env.npm_config_user_agent.includes("yarn")) {
58
- packageManagerCommand = "yarn run";
101
+ if (config.coverage.enabled) {
102
+ console.log(chalk.dim("Enabling coverage"));
103
+ command += " --use COVERAGE_USE=1 --transform as-test/transform";
104
+ if (config.coverage.show) command += " --use COVERAGE_SHOW=1";
59
105
  }
60
- else if (process.env.npm_config_user_agent &&
61
- process.env.npm_config_user_agent.includes("bun")) {
62
- packageManagerCommand = "bunx";
106
+ if (config.buildOptions.args) {
107
+ command += " " + config.buildOptions.args.join(" ");
63
108
  }
64
- console.log("");
65
- const inputFiles = await glob(config.input);
66
- for (const file of inputFiles) {
67
- console.log(chalk.dim("Including " + file));
68
- let command = `${packageManagerCommand} asc ${file}${args.length ? " " + args.join(" ") : ""}`;
69
- if (config.buildOptions.wasi) {
70
- command +=
71
- " --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json";
72
- }
73
- if (config.config !== "none") {
74
- command += " --config " + config.config;
75
- }
76
- const outFile = config.outDir +
77
- "/" +
78
- file.slice(file.lastIndexOf("/") + 1).replace(".ts", ".wasm");
79
- if (config.outDir) {
80
- command += " -o " + outFile;
81
- }
82
- if (config.coverage.enabled) {
83
- console.log(chalk.dim("Enabling coverage"));
84
- command += " --use COVERAGE_USE=1 --transform as-test/transform";
85
- if (config.coverage.show)
86
- command += " --use COVERAGE_SHOW=1";
87
- }
88
- if (config.buildOptions.args) {
89
- command += " " + config.buildOptions.args.join(" ");
90
- }
91
- buildCommands.push(command);
109
+ if (
110
+ ["node", "deno", "bun"].includes(
111
+ config.runOptions.runtime.run.split(" ")[0],
112
+ )
113
+ ) {
114
+ command += " --exportStart";
92
115
  }
93
- const build = (command) => {
94
- return new Promise((resolve, _) => {
95
- console.log(chalk.dim("Building: " + command));
96
- exec(command, (err, stdout, stderr) => {
97
- if (config.buildOptions.verbose) {
98
- process.stdout.write(stdout);
99
- }
100
- if (err) {
101
- process.stderr.write(stderr + "\n");
102
- process.exit(1);
103
- }
104
- resolve();
105
- });
106
- });
107
- };
108
- if (config.buildOptions.parallel) {
109
- console.log(chalk.dim("Building sources in parallel..."));
110
- const start = performance.now();
111
- let builders = [];
112
- for (const command of buildCommands) {
113
- builders.push(build(command));
116
+ buildCommands.push(command);
117
+ }
118
+ const build = (command) => {
119
+ return new Promise((resolve, _) => {
120
+ console.log(chalk.dim("Building: " + command));
121
+ exec(command, (err, stdout, stderr) => {
122
+ if (config.buildOptions.verbose) {
123
+ process.stdout.write(stdout);
124
+ }
125
+ if (err) {
126
+ process.stderr.write(stderr + "\n");
127
+ process.exit(1);
114
128
  }
115
- await Promise.all(builders);
116
- console.log(chalk.dim("Compiled in " + formatTime(performance.now() - start)) + "\n");
129
+ resolve();
130
+ });
131
+ });
132
+ };
133
+ if (config.buildOptions.parallel) {
134
+ console.log(chalk.dim("Building sources in parallel..."));
135
+ const start = performance.now();
136
+ let builders = [];
137
+ for (const command of buildCommands) {
138
+ builders.push(build(command));
117
139
  }
140
+ await Promise.all(builders);
141
+ console.log(
142
+ chalk.dim("Compiled in " + formatTime(performance.now() - start)) + "\n",
143
+ );
144
+ }
118
145
  }