|
| 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 | +}); |
0 commit comments