-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: unit test Web3FunctionBuilder (#40)
* chore: Builder._validateSchema to private from public * feat: TS-ESM compatible library versioning * fix: import for jest unit testing * test: Web3FunctionBuilder unit tests * fix: test for invalid userargs * chore: update fail message checker
- Loading branch information
Showing
15 changed files
with
44,670 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,7 @@ src/logs/ | |
.eslintcache | ||
|
||
# resolver builds | ||
.tmp | ||
.tmp | ||
|
||
# version file | ||
src/lib/version.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import path from "node:path"; | ||
import { Web3FunctionBuilder } from "./Web3FunctionBuilder"; | ||
|
||
describe("Web3FunctionBuilder.build", () => { | ||
const TEST_FOLDER_BASE = path.join( | ||
process.cwd(), | ||
"src/lib/builder/__test__/" | ||
); | ||
|
||
const buildTestPath = (folder: string): string => { | ||
return path.join(TEST_FOLDER_BASE, folder); | ||
}; | ||
|
||
const buildSchemaPath = (folder: string): string => { | ||
return path.join(buildTestPath(folder), "index.ts"); | ||
}; | ||
|
||
test("should fail when input path does not exist", async () => { | ||
const res = await Web3FunctionBuilder.build( | ||
buildSchemaPath("not-existing") | ||
); | ||
|
||
expect(res.success).toBeFalsy(); | ||
if (res.success === false) { | ||
expect(res.error.message).toMatch("Missing Web3Function schema"); | ||
} | ||
}); | ||
|
||
test("should fail when input path does not have schema.json", async () => { | ||
const res = await Web3FunctionBuilder.build(buildSchemaPath("no-schema")); | ||
|
||
expect(res.success).toBeFalsy(); | ||
if (res.success === false) { | ||
expect(res.error.message).toMatch("Missing Web3Function schema"); | ||
} | ||
}); | ||
|
||
test("should fail when schema is missing a required field", async () => { | ||
const res = await Web3FunctionBuilder.build( | ||
buildSchemaPath("missing-required-field") | ||
); | ||
|
||
expect(res.success).toBeFalsy(); | ||
if (res.success === false) { | ||
expect(res.error.message).toMatch("must have required property"); | ||
} | ||
}); | ||
|
||
test("should fail when schema major version does not match with the SDK version", async () => { | ||
const res = await Web3FunctionBuilder.build( | ||
buildSchemaPath("invalid-schema-version") | ||
); | ||
|
||
expect(res.success).toBeFalsy(); | ||
if (res.success === false) { | ||
expect(res.error.message).toMatch( | ||
"must match the major version of the installed sdk" | ||
); | ||
} | ||
}); | ||
|
||
test("should fail when schema memory config is invalid", async () => { | ||
const res = await Web3FunctionBuilder.build( | ||
buildSchemaPath("invalid-schema-memory") | ||
); | ||
|
||
expect(res.success).toBeFalsy(); | ||
if (res.success === false) { | ||
expect(res.error.message).toMatch( | ||
"'memory' must be equal to one of the allowed values" | ||
); | ||
} | ||
}); | ||
|
||
test("should fail when schema runtime config is invalid", async () => { | ||
const res = await Web3FunctionBuilder.build( | ||
buildSchemaPath("invalid-schema-runtime") | ||
); | ||
|
||
expect(res.success).toBeFalsy(); | ||
if (res.success === false) { | ||
expect(res.error.message).toMatch( | ||
"'runtime' must be equal to one of the allowed values" | ||
); | ||
} | ||
}); | ||
|
||
test("should fail when schema timeout is invalid", async () => { | ||
const res = await Web3FunctionBuilder.build( | ||
buildSchemaPath("invalid-schema-timeout") | ||
); | ||
|
||
expect(res.success).toBeFalsy(); | ||
if (res.success === false) { | ||
expect(res.error.message.includes("'timeout' must be")).toBeTruthy(); | ||
} | ||
}); | ||
|
||
test("should fail when schema userArgs include unknown types", async () => { | ||
const res = await Web3FunctionBuilder.build( | ||
buildSchemaPath("invalid-schema-userargs") | ||
); | ||
|
||
expect(res.success).toBeFalsy(); | ||
if (res.success === false) { | ||
expect(res.error.message).toMatch( | ||
"must be equal to one of the allowed values" | ||
); | ||
} | ||
}); | ||
|
||
test("should pass when schema is valid", async () => { | ||
const filePath = path.join(buildTestPath("valid-schema"), "index.js"); | ||
const res = await Web3FunctionBuilder.build( | ||
buildSchemaPath("valid-schema"), | ||
{ | ||
filePath, | ||
sourcePath: path.join(buildTestPath("valid-schema"), "source.js"), | ||
} | ||
); | ||
|
||
expect(res.success).toBeTruthy(); | ||
if (res.success) { | ||
expect(res.filePath).toEqual(filePath); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"web3FunctionVersion": "2.0.0", | ||
"runtime": "js-1.0", | ||
"memory": 512, | ||
"timeout": 30, | ||
"userArgs": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"web3FunctionVersion": "2.0.0", | ||
"runtime": "js-2.0", | ||
"memory": 128, | ||
"timeout": 30, | ||
"userArgs": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"web3FunctionVersion": "2.0.0", | ||
"runtime": "js-1.0", | ||
"memory": 128, | ||
"timeout": 300, | ||
"userArgs": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"web3FunctionVersion": "2.0.0", | ||
"runtime": "js-1.0", | ||
"memory": 128, | ||
"timeout": 30, | ||
"userArgs": { | ||
"argument": "invalid" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"web3FunctionVersion": "1.0.0", | ||
"runtime": "js-1.0", | ||
"memory": 128, | ||
"timeout": 30, | ||
"userArgs": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"web3FunctionVersion": "2.0.0", | ||
"runtime": "js-1.0", | ||
"memory": 128, | ||
"timeout": 30 | ||
} |
Empty file.
Oops, something went wrong.