Skip to content

Commit 4c21c10

Browse files
authored
Merge pull request #180 from Web3-API/feature/cli-tests
CLI Automated Tests
2 parents ea8c550 + 4ffa204 commit 4c21c10

39 files changed

Lines changed: 1311 additions & 94 deletions

demos/simple-storage/protocol/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"scripts": {
1212
"build": "npx w3 build",
13+
"contracts:deploy": "node ./deploy-contracts.js",
1314
"test:env:up": "npx w3 test-env up && yarn contracts:deploy",
1415
"test:env:down": "npx w3 test-env down"
1516
},

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@
2121
"scripts": {
2222
"reset": "yarn clean && yarn && yarn build",
2323
"clean": "rimraf ./**/node_modules ./**/build ./**/coverage",
24-
"build": "lerna run build --no-private --ignore @web3api/cli && yarn build:cli",
25-
"build:cli": "lerna run build --scope @web3api/cli",
24+
"build": "lerna run build --no-private --ignore @web3api/cli && lerna run build --scope @web3api/cli",
2625
"lint": "lerna run lint",
2726
"lint:fix": "lerna run lint -- --fix",
2827
"lint:ci": "lerna run lint --parallel",
29-
"test": "lerna run test --no-private",
30-
"test:ci": "lerna run test:ci --no-private --parallel",
28+
"test": "lerna run test --no-private --ignore @web3api/client-js && lerna run test --scope @web3api/client-js",
29+
"test:ci": "lerna run test:ci --no-private --ignore @web3api/client-js && lerna run test:ci --scope @web3api/client-js",
3130
"version:apply": "npx lerna version $(cat VERSION) --exact --no-git-tag-version --yes",
3231
"postversion:apply": "git add . && git commit -m \"build(release): migrate to `cat ./VERSION`\"",
3332
"publish:npm": "lerna publish from-package --no-private --yes --registry https://registry.npmjs.org/ --no-verify-access"

packages/cli/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
collectCoverage: true,
33
preset: "ts-jest",
44
testEnvironment: "node",
5-
testMatch: ["**/__tests__/?(*.)+(spec|test).[jt]s?(x)"],
5+
testMatch: ["**/__tests__/**/?(*.)+(spec|test).[jt]s?(x)"],
66
globals: {
77
"ts-jest": {
88
diagnostics: false,
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import path from "path";
2+
import { clearStyle, run } from "./utils";
3+
4+
const HELP = `
5+
w3 build [options] [<web3api-manifest>]
6+
7+
Options:
8+
-h, --help Show usage information
9+
-i, --ipfs [<node>] Upload build results to an IPFS node (default: dev-server's node)
10+
-o, --output-dir <path> Output directory for build results (default: build/)
11+
-e, --test-ens <[address,]domain> Publish the package to a test ENS domain locally (requires --ipfs)
12+
-w, --watch Automatically rebuild when changes are made (default: false)
13+
14+
`;
15+
16+
describe("e2e tests for build command", () => {
17+
const projectRoot = path.resolve(__dirname, "../project/");
18+
19+
test("Should show help text", async () => {
20+
const errorHandler = jest.fn();
21+
22+
const { code, output } = await run(
23+
"../../../bin/w3",
24+
["build", "--help"],
25+
projectRoot,
26+
errorHandler
27+
);
28+
29+
expect(code).toEqual(0);
30+
expect(errorHandler).not.toBeCalled();
31+
expect(clearStyle(output)).toEqual(HELP);
32+
});
33+
34+
test("Should throw error for invalid params - outputDir", async () => {
35+
const errorHandler = jest.fn();
36+
37+
const { code, output } = await run(
38+
"../../../bin/w3",
39+
["build", "--output-dir"],
40+
projectRoot,
41+
errorHandler
42+
);
43+
44+
expect(code).toEqual(0);
45+
expect(errorHandler).not.toBeCalled();
46+
expect(clearStyle(output))
47+
.toEqual(`--output-dir option missing <path> argument
48+
${HELP}`);
49+
});
50+
51+
test("Should throw error for invalid params - testEns", async () => {
52+
const errorHandler = jest.fn();
53+
54+
const { code, output } = await run(
55+
"../../../bin/w3",
56+
["build", "--test-ens"],
57+
projectRoot,
58+
errorHandler
59+
);
60+
61+
expect(code).toEqual(0);
62+
expect(errorHandler).not.toBeCalled();
63+
expect(clearStyle(output))
64+
.toEqual(`--test-ens option missing <[address,]domain> argument
65+
${HELP}`);
66+
});
67+
68+
test("Should throw error for invalid params - ipfs", async () => {
69+
const errorHandler = jest.fn();
70+
71+
const { code, output } = await run(
72+
"../../../bin/w3",
73+
["build", "--test-ens", "test.eth"],
74+
projectRoot,
75+
errorHandler
76+
);
77+
78+
expect(code).toEqual(0);
79+
expect(errorHandler).not.toBeCalled();
80+
expect(clearStyle(output))
81+
.toEqual(`--test-ens option requires the --ipfs [<node>] option
82+
${HELP}`);
83+
});
84+
85+
test("Should throw error for invalid web3api - invalid route", async () => {
86+
const errorHandler = jest.fn();
87+
88+
const { code, output } = await run(
89+
"../../../bin/w3",
90+
["build", "invalid-web3api-1.yaml"],
91+
projectRoot,
92+
errorHandler
93+
);
94+
95+
expect(code).toEqual(1);
96+
expect(errorHandler).not.toBeCalled();
97+
expect(clearStyle(output)).toContain(`- Compile Web3API
98+
- Load web3api from invalid-web3api-1.yaml
99+
✔ Load web3api from invalid-web3api-1.yaml
100+
✖ Failed to compile Web3API: ENOENT: no such file or directory, open '${projectRoot}/src/wrong/schema.graphql'
101+
`);
102+
});
103+
104+
test("Should throw error for invalid web3api - invalid field", async () => {
105+
const errorHandler = jest.fn();
106+
107+
const { code, output } = await run(
108+
"../../../bin/w3",
109+
["build", "invalid-web3api-2.yaml"],
110+
projectRoot,
111+
errorHandler
112+
);
113+
114+
expect(code).toEqual(1);
115+
expect(errorHandler).not.toBeCalled();
116+
expect(clearStyle(output)).toContain(`- Compile Web3API
117+
- Load web3api from invalid-web3api-2.yaml
118+
✖ Failed to load web3api from invalid-web3api-2.yaml: Field wrong_mutation is not accepted in the schema. Please check the accepted fields here:`);
119+
});
120+
121+
test("Successfully build the project", async () => {
122+
const errorHandler = jest.fn();
123+
124+
const { code, output } = await run(
125+
"../../../bin/w3",
126+
["build"],
127+
projectRoot,
128+
errorHandler
129+
);
130+
131+
expect(code).toEqual(0);
132+
expect(errorHandler).not.toBeCalled();
133+
expect(clearStyle(output)).toEqual(`- Compile Web3API
134+
- Load web3api from web3api.yaml
135+
✔ Load web3api from web3api.yaml
136+
Compiling WASM module: ./src/mutation/index.ts => ${projectRoot}/build/mutation.wasm
137+
- Compile Web3API
138+
Compiling WASM module: ./src/query/index.ts => ${projectRoot}/build/query.wasm
139+
- Compile Web3API
140+
- Output web3api to build/web3api.yaml
141+
✔ Output web3api to build/web3api.yaml
142+
✔ Compile Web3API
143+
`);
144+
});
145+
});
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import path from "path";
2+
import { defaultGenerationFile, defaultManifest } from "../../commands/codegen";
3+
import { clearStyle, run } from "./utils";
4+
5+
const HELP = `
6+
w3 codegen [<generation-file>] [options]
7+
8+
Generation file:
9+
Path to the generation file (default: ${defaultGenerationFile})
10+
11+
Options:
12+
-h, --help Show usage information
13+
-m, --manifest-path <path> Path to the Web3API manifest file (default: ${defaultManifest.join(
14+
" | "
15+
)})
16+
-i, --ipfs [<node>] IPFS node to load external schemas (default: dev-server's node)
17+
-o, --output-dir <path> Output directory for generated types (default: types/)
18+
-e, --ens [<address>] ENS address to lookup external schemas (default: 0x0000...2e1e)
19+
20+
`;
21+
22+
describe("e2e tests for codegen command", () => {
23+
const projectRoot = path.resolve(__dirname, "../project/");
24+
25+
test("Should show help text", async () => {
26+
const errorHandler = jest.fn();
27+
28+
const { code, output } = await run(
29+
"../../../bin/w3",
30+
["codegen", "--help"],
31+
projectRoot,
32+
errorHandler
33+
);
34+
35+
expect(code).toEqual(0);
36+
expect(errorHandler).not.toBeCalled();
37+
expect(clearStyle(output)).toEqual(HELP);
38+
});
39+
40+
test("Should throw error for invalid params - outputDir", async () => {
41+
const errorHandler = jest.fn();
42+
43+
const { code, output } = await run(
44+
"../../../bin/w3",
45+
["codegen", "--output-dir"],
46+
projectRoot,
47+
errorHandler
48+
);
49+
50+
expect(code).toEqual(0);
51+
expect(errorHandler).not.toBeCalled();
52+
expect(clearStyle(output))
53+
.toEqual(`--output-dir option missing <path> argument
54+
${HELP}`);
55+
});
56+
57+
test("Should throw error for invalid params - ens", async () => {
58+
const errorHandler = jest.fn();
59+
60+
const { code, output } = await run(
61+
"../../../bin/w3",
62+
["codegen", "--ens"],
63+
projectRoot,
64+
errorHandler
65+
);
66+
67+
expect(code).toEqual(0);
68+
expect(errorHandler).not.toBeCalled();
69+
expect(clearStyle(output))
70+
.toEqual(`--ens option missing <[address,]domain> argument
71+
${HELP}`);
72+
});
73+
74+
test("Should throw error for invalid generation file - wrong file", async () => {
75+
const errorHandler = jest.fn();
76+
77+
const { code, output } = await run(
78+
"../../../bin/w3",
79+
["codegen", `web3api-invalid.gen.js`],
80+
projectRoot,
81+
errorHandler
82+
);
83+
84+
expect(code).toEqual(1);
85+
expect(errorHandler).not.toBeCalled();
86+
expect(clearStyle(output)).toContain(`- Generate types
87+
- Load web3api from web3api.yaml
88+
✔ Load web3api from web3api.yaml
89+
✖ Failed to generate types: Cannot find module '${projectRoot}/web3api-invalid.gen.js'`);
90+
});
91+
92+
test("Should throw error for invalid generation file - no run() method", async () => {
93+
const errorHandler = jest.fn();
94+
95+
const { code, output } = await run(
96+
"../../../bin/w3",
97+
["codegen", `web3api-norun.gen.js`],
98+
projectRoot,
99+
errorHandler
100+
);
101+
102+
expect(code).toEqual(1);
103+
expect(errorHandler).not.toBeCalled();
104+
expect(clearStyle(output)).toContain(`- Generate types
105+
- Load web3api from web3api.yaml
106+
✔ Load web3api from web3api.yaml
107+
✖ Failed to generate types: The generation file provided doesn't have the 'run' method.`);
108+
});
109+
110+
test("Should successfully generate types", async () => {
111+
const errorHandler = jest.fn();
112+
113+
const rimraf = require("rimraf");
114+
rimraf.sync(`${projectRoot}/types`);
115+
116+
const { code, output } = await run(
117+
"../../../bin/w3",
118+
["codegen"],
119+
projectRoot,
120+
errorHandler
121+
);
122+
123+
expect(code).toEqual(0);
124+
expect(errorHandler).not.toBeCalled();
125+
expect(clearStyle(output)).toEqual(`- Generate types
126+
- Load web3api from web3api.yaml
127+
✔ Load web3api from web3api.yaml
128+
Generating types from ./templates/schema.mustache
129+
- Generate types
130+
Generating types from ./templates/schema.mustache
131+
- Generate types
132+
Generating types from ./templates/schema.mustache
133+
- Generate types
134+
✔ Generate types
135+
🔥 Types were generated successfully 🔥
136+
`);
137+
138+
rimraf.sync(`${projectRoot}/types`);
139+
});
140+
});

0 commit comments

Comments
 (0)