Skip to content

Commit 922d6e2

Browse files
authored
doc: add mjs config example (#6)
1 parent 114b6f9 commit 922d6e2

File tree

9 files changed

+50
-18
lines changed

9 files changed

+50
-18
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,22 @@ endTest(); // Don't forget it!
4141

4242
Create a config file in project root `as-test.config.js`:
4343

44+
for cjs:
45+
4446
```javascript
4547
module.exports = {
4648
include: ["source", "tests"],
4749
};
4850
```
4951

52+
for mjs:
53+
54+
```javascript
55+
export default {
56+
include: ["source", "tests"],
57+
};
58+
```
59+
5060
Add the following section to your `package.json`
5161

5262
```json

as-test.config.cjs renamed to as-test.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
/** file include in test */
33
include: ["assembly", "tests-as"],
44

example/as-test.config.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
include: ["example"],
33
exclude: ["assembly/coverageCollector.ts", "assembly/mock.ts"],
44

5-
/** assemblyscript compile flag, default is --exportStart _start --sourceMap --debug*/
5+
/** assemblyscript compile flag, default is --exportStart _start --sourceMap --debug -O0 */
66
flags: "",
77

88
/**

example/as-test.config.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default {
2+
include: ["example"],
3+
exclude: ["assembly/coverageCollector.ts", "assembly/mock.ts"],
4+
5+
/** assemblyscript compile flag, default is --exportStart _start --sourceMap --debug -O0 */
6+
flags: "",
7+
8+
/**
9+
* import functions
10+
* @param {ImportsArgument} runtime
11+
* @returns
12+
*/
13+
imports(runtime) {
14+
return {};
15+
},
16+
17+
/** template file path, default "coverage" */
18+
temp: "coverage",
19+
20+
/** report file path, default "coverage" */
21+
output: "coverage",
22+
};

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
"prettier": "@schleifner/prettier-config",
1414
"scripts": {
1515
"build": "tsc --build ./transform/tsconfig.json && node ./build/esbuild.js",
16-
"test": "node bin/as-test.js --config ./as-test.config.cjs && cross-env NODE_OPTIONS=--experimental-vm-modules jest",
16+
"test": "node bin/as-test.js && cross-env NODE_OPTIONS=--experimental-vm-modules jest",
1717
"lint": "eslint src assembly tests-ts/test --max-warnings=0 && prettier -c .",
1818
"lint:fix": "eslint src assembly --fix",
19-
"example": "node bin/as-test.js --config example/as-test.config.cjs"
19+
"example": "node bin/as-test.js --config example/as-test.config.cjs ; node bin/as-test.js --config example/as-test.config.js"
2020
},
2121
"dependencies": {
2222
"@assemblyscript/loader": ">=0.25.1",

src/core/precompile.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ export async function precompile(
2323
const testCodePaths = testcases ?? getRelatedFiles(includes, excludes, (path: string) => path.endsWith(".test.ts"));
2424

2525
const sourceCodePaths = getRelatedFiles(includes, excludes, (path: string) => !path.endsWith(".test.ts"));
26-
const sourceCodeTransfroms: Promise<void>[] = [];
26+
const sourceCodeTransforms: Promise<void>[] = [];
2727
for (const sourceCodePath of sourceCodePaths.values()) {
28-
sourceCodeTransfroms.push(transform(sourceCodePath, transformFunction));
28+
sourceCodeTransforms.push(transform(sourceCodePath, transformFunction));
2929
}
30-
await Promise.all(sourceCodeTransfroms);
30+
await Promise.all(sourceCodeTransforms);
3131

3232
return {
3333
testCodePaths,
@@ -43,11 +43,11 @@ export function getRelatedFiles(includes: string[], excludes: string[], filter:
4343
const exc = ignore.default().add(excludes);
4444

4545
for (const path of includeFiles) {
46-
const relativedPath = relative(".", path);
47-
if (relativedPath.startsWith("..")) {
46+
const relativePath = relative(".", path);
47+
if (relativePath.startsWith("..")) {
4848
throw new Error(`file ${path} out of scope (${resolve(".")})`);
4949
}
50-
if (exc.ignores(relativedPath)) {
50+
if (exc.ignores(relativePath)) {
5151
continue; // ab
5252
}
5353
if (filter(path)) {

tests-ts/test/core/__snapshots__/precompile.test.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`listFunction transfrom 1`] = `
3+
exports[`listFunction transform 1`] = `
44
Map {
55
"tests-ts/fixture/transformFunction.ts" => [
66
{

tests-ts/test/core/precompile.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import { join } from "node:path";
22
import { fileURLToPath, URL } from "node:url";
33
import { precompile } from "../../../src/core/precompile.js";
44

5-
test("listFunction transfrom", async () => {
6-
const transfromFunction = join(
5+
test("listFunction transform", async () => {
6+
const transformFunction = join(
77
fileURLToPath(new URL(".", import.meta.url)),
88
"..",
99
"..",
1010
"..",
1111
"transform",
1212
"listFunctions.mjs"
1313
);
14-
const unittestPackages = await precompile(["tests-ts/fixture/transformFunction.ts"], [], [], transfromFunction);
14+
const unittestPackages = await precompile(["tests-ts/fixture/transformFunction.ts"], [], [], transformFunction);
1515
expect(unittestPackages.testCodePaths).toEqual([]);
1616
expect(unittestPackages.sourceFunctions).toMatchSnapshot();
1717
});

tests-ts/test/core/throwError.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const { precompile } = await import("../../../src/core/precompile.js");
2020
const { compile } = await import("../../../src/core/compile.js");
2121

2222
test("transform error", async () => {
23-
const transfromFunction = join(
23+
const transformFunction = join(
2424
fileURLToPath(new URL(".", import.meta.url)),
2525
"..",
2626
"..",
@@ -30,12 +30,12 @@ test("transform error", async () => {
3030
);
3131
expect(jest.isMockFunction(main)).toBeTruthy();
3232
await expect(async () => {
33-
await precompile(["tests-ts/fixture/transformFunction.ts"], [], [], transfromFunction);
34-
}).rejects.toThrowError("mock asc.main() error");
33+
await precompile(["tests-ts/fixture/transformFunction.ts"], [], [], transformFunction);
34+
}).rejects.toThrow("mock asc.main() error");
3535
});
3636

3737
test("compile error", async () => {
3838
await expect(async () => {
3939
await compile(["non-exist.ts"], "mockFolder", "");
40-
}).rejects.toThrowError("mock asc.main() error");
40+
}).rejects.toThrow("mock asc.main() error");
4141
});

0 commit comments

Comments
 (0)