From 97c4f6dffa54c849f0293bcd43f15356a45d8365 Mon Sep 17 00:00:00 2001 From: bhavik-m Date: Wed, 23 Mar 2022 17:33:37 +0530 Subject: [PATCH] added test script --- hardhat.config.ts | 2 +- package.json | 3 +- scripts/tests/run_test_through_cmd.ts | 63 +++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 scripts/tests/run_test_through_cmd.ts diff --git a/hardhat.config.ts b/hardhat.config.ts index e679a097..df188825 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -6,7 +6,7 @@ import "@nomiclabs/hardhat-web3"; import "hardhat-deploy"; import "hardhat-deploy-ethers"; import "@typechain/hardhat"; - +import "./scripts/tests/run_test_through_cmd" import { resolve } from "path"; import { config as dotenvConfig } from "dotenv"; import { HardhatUserConfig } from "hardhat/config"; diff --git a/package.json b/package.json index f312326d..edad4d01 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "test:runner": "hardhat run scripts/tests/run-tests.ts", "typechain": "hardhat typechain", "compile": "hardhat compile", - "deploy:runner": "hardhat run scripts/deployment/deployConnectorsFromCmd.ts" + "deploy:runner": "hardhat run scripts/deployment/deployConnectorsFromCmd.ts", + "tests": "hardhat run_tests" }, "repository": { "type": "git", diff --git a/scripts/tests/run_test_through_cmd.ts b/scripts/tests/run_test_through_cmd.ts new file mode 100644 index 00000000..6fad438c --- /dev/null +++ b/scripts/tests/run_test_through_cmd.ts @@ -0,0 +1,63 @@ +import { promises as fs } from "fs"; + +import { join } from "path"; +import { execScript } from "./command"; + +import { task } from "hardhat/config"; + + +let start: number, end: number; + +task("run_tests", "runs specified test on a specified chain") +.addPositionalParam("chain") +.addPositionalParam("test") +.setAction(async (taskArgs) => { + const chain = taskArgs.chain; + const test = taskArgs.test; + await testRunner(chain,test) + .then(() => + console.log( + `🙌 finished the test runner, time taken ${(end - start) / 1000} sec` + ) + ) + .catch((err) => console.error("❌ failed due to error: ", err)); + +}); + +async function testRunner(chain: string, testName: string) { + + const testsPath = join(__dirname, "../../test", chain); + await fs.access(testsPath); + const availableTests = await fs.readdir(testsPath); + if (availableTests.length === 0) { + throw new Error(`No tests available for ${chain}`); + } + + start = Date.now(); + let path: string; + if (testName === "all") { + for (let test of availableTests) { + path = join(testsPath, test); + path += "/*"; + await execScript({ + cmd: "npx", + args: ["hardhat", "test", path], + env: { + networkType: chain, + }, + }).catch((err)=>console.log(`failed ${test}`)) + } + } else { + path = join(testsPath, testName); + path += "/*"; + + await execScript({ + cmd: "npx", + args: ["hardhat", "test", path], + env: { + networkType: chain, + }, + }); + } + end = Date.now(); +} \ No newline at end of file