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 +3 -2
- package/README.md +18 -17
- package/asconfig.json +2 -4
- package/assembly/__tests__/example.spec.ts +2 -13
- package/assembly/index.ts +7 -2
- package/bin/build.js +125 -98
- package/bin/index.js +161 -132
- package/bin/init.js +35 -31
- package/bin/run.js +62 -42
- package/bin/types.js +29 -29
- package/bin/util.js +20 -20
- package/cli/build.ts +10 -3
- package/cli/index.ts +1 -1
- package/cli/run.ts +27 -16
- package/package.json +4 -4
- package/transform/lib/mock.js +9 -2
- package/transform/lib/mock.js.map +1 -1
- package/transform/package.json +1 -1
- package/transform/src/mock.ts +8 -6
- package/unision +38 -0
- package/unision.pub +1 -0
- package/assembly/test.ts +0 -71
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.
|
|
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.
|
|
6
|
+
v0.2.0
|
|
7
7
|
</pre>
|
|
8
8
|
</h5>
|
|
9
9
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
@@ -8,20 +8,10 @@ import {
|
|
|
8
8
|
afterEach,
|
|
9
9
|
log,
|
|
10
10
|
run,
|
|
11
|
-
|
|
11
|
+
mockFn,
|
|
12
12
|
} from "..";
|
|
13
13
|
|
|
14
|
-
|
|
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
|
-
|
|
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.
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
33
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
packageManagerCommand = "bunx";
|
|
106
|
+
if (config.buildOptions.args) {
|
|
107
|
+
command += " " + config.buildOptions.args.join(" ");
|
|
63
108
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
-
|
|
116
|
-
|
|
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
|
}
|