|
| 1 | +import path from "path"; |
| 2 | +import { clearStyle } from "./utils"; |
| 3 | + |
| 4 | +import { runCLI } from "@web3api/test-env-js"; |
| 5 | + |
| 6 | +const HELP = ` |
| 7 | +w3 build [options] [<web3api-manifest>] |
| 8 | +
|
| 9 | +Options: |
| 10 | + -h, --help Show usage information |
| 11 | + -i, --ipfs [<node>] Upload build results to an IPFS node (default: dev-server's node) |
| 12 | + -o, --output-dir <path> Output directory for build results (default: build/) |
| 13 | + -e, --test-ens <[address,]domain> Publish the package to a test ENS domain locally (requires --ipfs) |
| 14 | + -w, --watch Automatically rebuild when changes are made (default: false) |
| 15 | +
|
| 16 | +`; |
| 17 | + |
| 18 | +describe("e2e tests for build command", () => { |
| 19 | + const projectRoot = path.resolve(__dirname, "../project/"); |
| 20 | + |
| 21 | + test("Should show help text", async () => { |
| 22 | + const { exitCode: code, stdout: output, stderr: error } = await runCLI({ |
| 23 | + args: ["build", "--help"], |
| 24 | + cwd: projectRoot |
| 25 | + }, "../../../bin/w3"); |
| 26 | + |
| 27 | + expect(code).toEqual(0); |
| 28 | + expect(error).toBe(""); |
| 29 | + expect(clearStyle(output)).toEqual(HELP); |
| 30 | + }); |
| 31 | + |
| 32 | + test("Should throw error for invalid params - outputDir", async () => { |
| 33 | + const { exitCode: code, stdout: output, stderr: error } = await runCLI({ |
| 34 | + args: ["build", "--output-dir"], |
| 35 | + cwd: projectRoot |
| 36 | + }, "../../../bin/w3"); |
| 37 | + |
| 38 | + expect(code).toEqual(0); |
| 39 | + expect(error).toBe(""); |
| 40 | + expect(clearStyle(output)) |
| 41 | + .toEqual(`--output-dir option missing <path> argument |
| 42 | +${HELP}`); |
| 43 | + }); |
| 44 | + |
| 45 | + test("Should throw error for invalid params - testEns", async () => { |
| 46 | + const { exitCode: code, stdout: output, stderr: error } = await runCLI({ |
| 47 | + args: ["build", "--test-ens"], |
| 48 | + cwd: projectRoot |
| 49 | + }, "../../../bin/w3"); |
| 50 | + |
| 51 | + expect(code).toEqual(0); |
| 52 | + expect(error).toBe(""); |
| 53 | + expect(clearStyle(output)) |
| 54 | + .toEqual(`--test-ens option missing <[address,]domain> argument |
| 55 | +${HELP}`); |
| 56 | + }); |
| 57 | + |
| 58 | + test("Should throw error for invalid params - ipfs", async () => { |
| 59 | + const { exitCode: code, stdout: output, stderr: error } = await runCLI({ |
| 60 | + args: ["build", "--test-ens", "test.eth"], |
| 61 | + cwd: projectRoot |
| 62 | + }, "../../../bin/w3"); |
| 63 | + |
| 64 | + expect(code).toEqual(0); |
| 65 | + expect(error).toBe(""); |
| 66 | + expect(clearStyle(output)) |
| 67 | + .toEqual(`--test-ens option requires the --ipfs [<node>] option |
| 68 | +${HELP}`); |
| 69 | + }); |
| 70 | + |
| 71 | + test("Should throw error for invalid web3api - invalid route", async () => { |
| 72 | + const { exitCode: code, stdout: output, stderr: error } = await runCLI({ |
| 73 | + args: ["build", "invalid-web3api-1.yaml"], |
| 74 | + cwd: projectRoot |
| 75 | + }, "../../../bin/w3"); |
| 76 | + |
| 77 | + expect(code).toEqual(1); |
| 78 | + expect(error).toBe(""); |
| 79 | + expect(clearStyle(output)).toContain(`- Compile Web3API |
| 80 | +- Load web3api from invalid-web3api-1.yaml |
| 81 | +✔ Load web3api from invalid-web3api-1.yaml |
| 82 | +✖ Failed to compile Web3API: ENOENT: no such file or directory, open '${projectRoot}/src/wrong/schema.graphql' |
| 83 | +`); |
| 84 | + }); |
| 85 | + |
| 86 | + test("Should throw error for invalid web3api - invalid field", async () => { |
| 87 | + const { exitCode: code, stdout: output, stderr: error } = await runCLI({ |
| 88 | + args: ["build", "invalid-web3api-2.yaml"], |
| 89 | + cwd: projectRoot |
| 90 | + }, "../../../bin/w3"); |
| 91 | + |
| 92 | + expect(code).toEqual(1); |
| 93 | + expect(error).toBe(""); |
| 94 | + expect(clearStyle(output)).toContain(`- Compile Web3API |
| 95 | +- Load web3api from invalid-web3api-2.yaml |
| 96 | +✖ Failed to load web3api from invalid-web3api-2.yaml: Field wrong_mutation is not accepted in the schema. Please check the accepted fields here:`); |
| 97 | + }); |
| 98 | + |
| 99 | + test("Successfully build the project", async () => { |
| 100 | + const { exitCode: code, stdout: output, stderr: error } = await runCLI({ |
| 101 | + args: ["build"], |
| 102 | + cwd: projectRoot |
| 103 | + }, "../../../bin/w3"); |
| 104 | + |
| 105 | + expect(code).toEqual(0); |
| 106 | + expect(error).toBe(""); |
| 107 | + expect(clearStyle(output)).toEqual(`- Compile Web3API |
| 108 | +- Load web3api from web3api.yaml |
| 109 | +✔ Load web3api from web3api.yaml |
| 110 | + Compiling WASM module: ./src/mutation/index.ts => ${projectRoot}/build/mutation.wasm |
| 111 | +- Compile Web3API |
| 112 | + Compiling WASM module: ./src/query/index.ts => ${projectRoot}/build/query.wasm |
| 113 | +- Compile Web3API |
| 114 | +- Output web3api to build/web3api.yaml |
| 115 | +✔ Output web3api to build/web3api.yaml |
| 116 | +✔ Compile Web3API |
| 117 | +`); |
| 118 | + }); |
| 119 | +}); |
0 commit comments