as-test 0.1.7 → 0.1.8

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
@@ -15,4 +15,5 @@ v0.1.3 - Revamp `log<T>(data: T)` with new as-console version
15
15
  v0.1.4 - Fix a few bugs with the cli
16
16
  v0.1.5 - Add skeleton cli and custom config
17
17
  v0.1.6 - Fix: args should be prefixed with a space
18
- v0.1.7 - Fix: remove warning about wasi-shim not being included when it is included
18
+ v0.1.7 - Fix: remove warning about wasi-shim not being included when it is included
19
+ v0.1.8 - Feat: function mocking
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  | _ || __| ___|_ _|| __|| __||_ _|
4
4
  | ||__ ||___| | | | __||__ | | |
5
5
  |__|__||_____| |_| |_____||_____| |_|
6
- v0.1.7
6
+ v0.1.8
7
7
  </pre>
8
8
  </h5>
9
9
 
@@ -38,27 +38,29 @@ import {
38
38
  afterAll,
39
39
  beforeEach,
40
40
  afterEach,
41
+ mock,
41
42
  log,
42
43
  run
43
44
  } from "as-test";
44
45
 
45
- // Shared setup for all tests (executed once before all tests)
46
46
  beforeAll(() => {
47
47
  log("Setting up test environment...");
48
48
  });
49
49
 
50
- // Shared teardown for all tests (executed once after all tests)
51
50
  afterAll(() => {
52
51
  log("Tearing down test environment...");
53
52
  });
54
53
 
54
+ // Mock/override the function console.log
55
+ mock("console.log", (data: string): void => {
56
+ console.log("[MOCKED]: " + data + "\n");
57
+ });
58
+
55
59
  describe("Math operations", () => {
56
- // Setup before each test in this group (optional)
57
60
  beforeEach(() => {
58
61
  log("Initializing test...");
59
62
  });
60
63
 
61
- // Teardown after each test in this group (optional)
62
64
  afterEach(() => {
63
65
  log("Cleaning up after test...");
64
66
  });
package/asconfig.json CHANGED
@@ -26,7 +26,7 @@
26
26
  }
27
27
  },
28
28
  "options": {
29
- "transform": ["./transform"],
29
+ "transform": [],
30
30
  "disableWarning": [226]
31
31
  },
32
32
  "extends": "./node_modules/@assemblyscript/wasi-shim/asconfig.json"
@@ -8,8 +8,23 @@ import {
8
8
  afterEach,
9
9
  log,
10
10
  run,
11
+ mock,
11
12
  } from "..";
12
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 => {
25
+ console.log("[MOCKED]: " + data);
26
+ });
27
+
13
28
  // Shared setup for all tests (executed once before all tests)
14
29
  beforeAll(() => {
15
30
  log("Setting up test environment...");
@@ -31,6 +46,11 @@ describe("Math operations", () => {
31
46
  log("Cleaning up after test...");
32
47
  });
33
48
 
49
+ test("Mock", () => {
50
+ hello(1, 2, 3);
51
+ console.log("hello");
52
+ });
53
+
34
54
  test("Addition", () => {
35
55
  expect(1 + 2).toBe(3);
36
56
  });
package/assembly/index.ts CHANGED
@@ -175,6 +175,11 @@ export function afterEach(callback: () => void): void {
175
175
  after_each_callback = callback;
176
176
  }
177
177
 
178
+ export function mock<returnType>(
179
+ fn: string,
180
+ callback: (...args: any[]) => returnType,
181
+ ): void {}
182
+
178
183
  /**
179
184
  * Class defining options that can be passed to the `run` function.
180
185
  *
@@ -230,7 +235,7 @@ export function run(options: RunOptions = new RunOptions()): void {
230
235
  ),
231
236
  );
232
237
  console.log(
233
- rainbow.dimMk("\n------------------- v0.1.7 -------------------\n"),
238
+ rainbow.dimMk("\n------------------- v0.1.8 -------------------\n"),
234
239
  );
235
240
  // @ts-ignore
236
241
  if (isDefined(COVERAGE_USE)) {
package/assembly/test.ts CHANGED
@@ -1,3 +1,4 @@
1
+ declare function hello(): void;
1
2
  import {
2
3
  describe,
3
4
  expect,
package/bin/build.js CHANGED
@@ -6,125 +6,113 @@ 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(
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 (
39
- (pkg.dependencies &&
40
- !Object.keys(pkg.dependencies).includes("@assemblyscript/wasi-shim")) ||
41
- (pkg.devDependencies &&
42
- !Object.keys(pkg.devDependencies).includes(
43
- "@assemblyscript/wasi-shim",
44
- )) ||
45
- (pkg.peerDependencies &&
46
- !Object.keys(pkg.peerDependencies).includes(
47
- "@assemblyscript/wasi-shim",
48
- ))
49
- ) {
50
- console.log(
51
- chalk.bold.bgMagentaBright(" WARN ") +
52
- chalk.dim(": @assemblyscript/wasi-shim") +
53
- " is not included in project dependencies!",
54
- );
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();
55
17
  }
56
- }
57
- let packageManagerCommand = "npx";
58
- if (
59
- process.env.npm_config_user_agent &&
60
- process.env.npm_config_user_agent.includes("pnpm")
61
- ) {
62
- packageManagerCommand = "pnpx";
63
- } else if (
64
- process.env.npm_config_user_agent &&
65
- process.env.npm_config_user_agent.includes("yarn")
66
- ) {
67
- packageManagerCommand = "yarn run";
68
- } else if (
69
- process.env.npm_config_user_agent &&
70
- process.env.npm_config_user_agent.includes("bun")
71
- ) {
72
- packageManagerCommand = "bunx";
73
- }
74
- console.log("");
75
- const inputFiles = await glob(config.input);
76
- for (const file of inputFiles) {
77
- console.log(chalk.dim("Including " + file));
78
- let command = `${packageManagerCommand} asc ${file}${args.length ? " " + args.join(" ") : ""}`;
79
- if (config.buildOptions.wasi) {
80
- command +=
81
- " --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json";
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");
21
+ }
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");
82
28
  }
83
- if (config.config !== "none") {
84
- command += " --config " + config.config;
29
+ const pkg = JSON.parse(readFileSync("./package.json").toString());
30
+ let buildCommands = [];
31
+ 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
+ }
85
50
  }
86
- const outFile =
87
- config.outDir +
88
- "/" +
89
- file.slice(file.lastIndexOf("/") + 1).replace(".ts", ".wasm");
90
- if (config.outDir) {
91
- command += " -o " + outFile;
51
+ let packageManagerCommand = "npx";
52
+ if (process.env.npm_config_user_agent &&
53
+ process.env.npm_config_user_agent.includes("pnpm")) {
54
+ packageManagerCommand = "pnpx";
92
55
  }
93
- if (config.coverage.enabled) {
94
- console.log(chalk.dim("Enabling coverage"));
95
- command += " --use COVERAGE_USE=1 --transform as-test/transform";
96
- if (config.coverage.show) command += " --use COVERAGE_SHOW=1";
56
+ else if (process.env.npm_config_user_agent &&
57
+ process.env.npm_config_user_agent.includes("yarn")) {
58
+ packageManagerCommand = "yarn run";
97
59
  }
98
- if (config.buildOptions.args) {
99
- command += " " + config.buildOptions.args.join(" ");
60
+ else if (process.env.npm_config_user_agent &&
61
+ process.env.npm_config_user_agent.includes("bun")) {
62
+ packageManagerCommand = "bunx";
100
63
  }
101
- buildCommands.push(command);
102
- }
103
- const build = (command) => {
104
- return new Promise((resolve, _) => {
105
- console.log(chalk.dim("Building: " + command));
106
- exec(command, (err, stdout, stderr) => {
107
- if (config.buildOptions.verbose) {
108
- process.stdout.write(stdout);
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";
109
72
  }
110
- if (err) {
111
- process.stderr.write(stderr + "\n");
112
- process.exit(1);
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);
92
+ }
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));
113
114
  }
114
- resolve();
115
- });
116
- });
117
- };
118
- if (config.buildOptions.parallel) {
119
- console.log(chalk.dim("Building sources in parallel..."));
120
- const start = performance.now();
121
- let builders = [];
122
- for (const command of buildCommands) {
123
- builders.push(build(command));
115
+ await Promise.all(builders);
116
+ console.log(chalk.dim("Compiled in " + formatTime(performance.now() - start)) + "\n");
124
117
  }
125
- await Promise.all(builders);
126
- console.log(
127
- chalk.dim("Compiled in " + formatTime(performance.now() - start)) + "\n",
128
- );
129
- }
130
118
  }
package/bin/index.js CHANGED
@@ -7,14 +7,16 @@ 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.1.7";
10
+ const version = "0.1.8";
11
11
  for (const arg of _args) {
12
- if (arg.startsWith("-")) flags.push(arg);
13
- else args.push(arg);
12
+ if (arg.startsWith("-"))
13
+ flags.push(arg);
14
+ else
15
+ args.push(arg);
14
16
  }
15
17
  if (!args.length) {
16
- if (flags.includes("--tap")) {
17
- console.log(`TAP version 13
18
+ if (flags.includes("--tap")) {
19
+ console.log(`TAP version 13
18
20
  # timing test
19
21
  ok 1 should be strictly equal
20
22
  not ok 2 should be strictly equal
@@ -28,161 +30,130 @@ not ok 2 should be strictly equal
28
30
  # tests 2
29
31
  # pass 1
30
32
  # fail 1`);
31
- } else if (flags.includes("--version") || flags.includes("-v")) {
32
- console.log("as-test" + " " + version.toString());
33
- } else {
34
- console.log(
35
- chalk.bold.blueBright("as-test") +
36
- " is a testing framework for AssemblyScript. " +
37
- chalk.dim("(v" + version + ")") +
38
- "\n",
39
- );
40
- console.log(
41
- chalk.bold("Usage: as-test") +
42
- " " +
43
- chalk.dim("<command>") +
44
- " " +
45
- chalk.bold.blueBright("[...flags]") +
46
- " " +
47
- chalk.bold("[...args]") +
33
+ }
34
+ else if (flags.includes("--version") || flags.includes("-v")) {
35
+ console.log("as-test" + " " + version.toString());
36
+ }
37
+ else {
38
+ console.log(chalk.bold.blueBright("as-test") +
39
+ " is a testing framework for AssemblyScript. " +
40
+ chalk.dim("(v" + version + ")") +
41
+ "\n");
42
+ console.log(chalk.bold("Usage: as-test") +
43
+ " " +
44
+ chalk.dim("<command>") +
45
+ " " +
46
+ chalk.bold.blueBright("[...flags]") +
47
+ " " +
48
+ chalk.bold("[...args]") +
49
+ " " +
50
+ chalk.dim("(alias: ast)") +
51
+ "\n");
52
+ console.log(chalk.bold("Commands:"));
53
+ console.log(" " +
54
+ chalk.bold.blueBright("run") +
55
+ " " +
56
+ chalk.dim("<my-test.spec.ts>") +
57
+ " " +
58
+ "Run unit tests with selected runtime");
59
+ console.log(" " +
60
+ chalk.bold.blueBright("build") +
61
+ " " +
62
+ chalk.dim("<my-test.spec.ts>") +
63
+ " " +
64
+ "Build unit tests and compile");
65
+ console.log(" " +
66
+ chalk.bold.blueBright("test") +
67
+ " " +
68
+ chalk.dim("<my-test.spec.ts>") +
69
+ " " +
70
+ "Build and run unit tests with selected runtime" +
71
+ "\n");
72
+ console.log(" " +
73
+ chalk.bold.magentaBright("init") +
74
+ " " +
75
+ chalk.strikethrough.dim("") +
76
+ " " +
77
+ "Initialize an empty testing template");
78
+ console.log(" " +
79
+ chalk.strikethrough.bold.magentaBright("config") +
80
+ " " +
81
+ chalk.strikethrough.dim("as-test.config.json") +
82
+ " " +
83
+ "Specify the configuration file");
84
+ console.log(" " +
85
+ chalk.strikethrough.bold.magentaBright("reporter") +
86
+ " " +
87
+ chalk.strikethrough.dim("<tap>") +
88
+ " " +
89
+ "Specify the test reporter to use");
90
+ console.log(" " +
91
+ chalk.strikethrough.bold.magentaBright("use") +
92
+ " " +
93
+ chalk.strikethrough.dim("wasmtime") +
94
+ " " +
95
+ "Specify the runtime to use" +
96
+ "\n");
97
+ console.log(chalk.bold("Flags:"));
98
+ console.log(" " +
99
+ chalk.strikethrough.dim("run") +
100
+ " " +
101
+ chalk.strikethrough.bold.blue("--coverage") +
102
+ " " +
103
+ "Use code coverage");
104
+ console.log(" " +
105
+ chalk.strikethrough.dim("run") +
106
+ " " +
107
+ chalk.strikethrough.bold.blue("--snapshot") +
108
+ " " +
109
+ "Take a snapshot of the tests");
110
+ console.log(" " +
111
+ chalk.strikethrough.dim("use") +
112
+ " " +
113
+ chalk.strikethrough.bold.blue("--list") +
114
+ " " +
115
+ "List supported runtimes");
116
+ console.log(" " +
117
+ chalk.strikethrough.dim("reporter") +
118
+ " " +
119
+ chalk.strikethrough.bold.blue("--list") +
120
+ " " +
121
+ "List supported reporters");
122
+ console.log(" " +
123
+ chalk.strikethrough.dim("<command>") +
124
+ " " +
125
+ chalk.strikethrough.bold.blue("--help") +
126
+ " " +
127
+ "Print info about command" +
128
+ "\n");
129
+ console.log(chalk.dim("If your using this, consider dropping a star, it would help a lot!") + "\n");
130
+ console.log("View the repo: " +
131
+ chalk.magenta("https://github.com/JairusSW/as-test"));
132
+ console.log("View the docs: " +
133
+ chalk.strikethrough.blue("https://docs.jairus.dev/as-test"));
134
+ }
135
+ }
136
+ else if (COMMANDS.includes(args[0])) {
137
+ const command = args.shift();
138
+ if (command === "build") {
139
+ build(args);
140
+ }
141
+ else if (command === "run") {
142
+ run();
143
+ }
144
+ else if (command === "test") {
145
+ build(args).then(() => {
146
+ run();
147
+ });
148
+ }
149
+ else if (command === "init") {
150
+ init(args);
151
+ }
152
+ }
153
+ else {
154
+ console.log(chalk.bgRed(" ERROR ") +
155
+ chalk.dim(":") +
48
156
  " " +
49
- chalk.dim("(alias: ast)") +
50
- "\n",
51
- );
52
- console.log(chalk.bold("Commands:"));
53
- console.log(
54
- " " +
55
- chalk.bold.blueBright("run") +
56
- " " +
57
- chalk.dim("<my-test.spec.ts>") +
58
- " " +
59
- "Run unit tests with selected runtime",
60
- );
61
- console.log(
62
- " " +
63
- chalk.bold.blueBright("build") +
64
- " " +
65
- chalk.dim("<my-test.spec.ts>") +
66
- " " +
67
- "Build unit tests and compile",
68
- );
69
- console.log(
70
- " " +
71
- chalk.bold.blueBright("test") +
72
- " " +
73
- chalk.dim("<my-test.spec.ts>") +
74
- " " +
75
- "Build and run unit tests with selected runtime" +
76
- "\n",
77
- );
78
- console.log(
79
- " " +
80
- chalk.bold.magentaBright("init") +
81
- " " +
82
- chalk.strikethrough.dim("") +
83
- " " +
84
- "Initialize an empty testing template",
85
- );
86
- console.log(
87
- " " +
88
- chalk.strikethrough.bold.magentaBright("config") +
89
- " " +
90
- chalk.strikethrough.dim("as-test.config.json") +
91
- " " +
92
- "Specify the configuration file",
93
- );
94
- console.log(
95
- " " +
96
- chalk.strikethrough.bold.magentaBright("reporter") +
97
- " " +
98
- chalk.strikethrough.dim("<tap>") +
99
- " " +
100
- "Specify the test reporter to use",
101
- );
102
- console.log(
103
- " " +
104
- chalk.strikethrough.bold.magentaBright("use") +
105
- " " +
106
- chalk.strikethrough.dim("wasmtime") +
107
- " " +
108
- "Specify the runtime to use" +
109
- "\n",
110
- );
111
- console.log(chalk.bold("Flags:"));
112
- console.log(
113
- " " +
114
- chalk.strikethrough.dim("run") +
115
- " " +
116
- chalk.strikethrough.bold.blue("--coverage") +
117
- " " +
118
- "Use code coverage",
119
- );
120
- console.log(
121
- " " +
122
- chalk.strikethrough.dim("run") +
123
- " " +
124
- chalk.strikethrough.bold.blue("--snapshot") +
125
- " " +
126
- "Take a snapshot of the tests",
127
- );
128
- console.log(
129
- " " +
130
- chalk.strikethrough.dim("use") +
131
- " " +
132
- chalk.strikethrough.bold.blue("--list") +
133
- " " +
134
- "List supported runtimes",
135
- );
136
- console.log(
137
- " " +
138
- chalk.strikethrough.dim("reporter") +
139
- " " +
140
- chalk.strikethrough.bold.blue("--list") +
141
- " " +
142
- "List supported reporters",
143
- );
144
- console.log(
145
- " " +
146
- chalk.strikethrough.dim("<command>") +
147
- " " +
148
- chalk.strikethrough.bold.blue("--help") +
149
- " " +
150
- "Print info about command" +
151
- "\n",
152
- );
153
- console.log(
154
- chalk.dim(
155
- "If your using this, consider dropping a star, it would help a lot!",
156
- ) + "\n",
157
- );
158
- console.log(
159
- "View the repo: " +
160
- chalk.magenta("https://github.com/JairusSW/as-test"),
161
- );
162
- console.log(
163
- "View the docs: " +
164
- chalk.strikethrough.blue("https://docs.jairus.dev/as-test"),
165
- );
166
- }
167
- } else if (COMMANDS.includes(args[0])) {
168
- const command = args.shift();
169
- if (command === "build") {
170
- build(args);
171
- } else if (command === "run") {
172
- run();
173
- } else if (command === "test") {
174
- build(args).then(() => {
175
- run();
176
- });
177
- } else if (command === "init") {
178
- init(args);
179
- }
180
- } else {
181
- console.log(
182
- chalk.bgRed(" ERROR ") +
183
- chalk.dim(":") +
184
- " " +
185
- chalk.bold("Unknown command: ") +
186
- args[0],
187
- );
157
+ chalk.bold("Unknown command: ") +
158
+ args[0]);
188
159
  }