diff --git a/package.json b/package.json index c9a2bbab..575a6ad6 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,6 @@ "node-downloader-helper": "2.1.9", "ora": "8.0.1", "semver": "7.6.0", - "shelljs": "0.8.5", "toml": "^3.0.0", "ts-mocha": "^10.0.0", "winston": "^3.12.0" @@ -73,7 +72,6 @@ "@types/mocha": "10.0.6", "@types/node": "^20.11.26", "@types/semver": "7.5.8", - "@types/shelljs": "0.8.12", "@typescript-eslint/eslint-plugin": "7.2.0", "@typescript-eslint/parser": "7.2.0", "chai": "5", diff --git a/src/commands/contract/test.ts b/src/commands/contract/test.ts index 29bd6e6a..9fd1e804 100644 --- a/src/commands/contract/test.ts +++ b/src/commands/contract/test.ts @@ -4,7 +4,6 @@ import path from "node:path"; import { globby } from "globby"; import Mocha from "mocha"; import { emptyDir, pathExistsSync } from "fs-extra/esm"; -import shell from "shelljs"; import { Contract } from "../../lib/contract.js"; import { SwankyCommand } from "../../lib/swankyCommand.js"; import { ConfigError, FileError, InputError, ProcessError, TestError } from "../../lib/errors.js"; @@ -25,8 +24,8 @@ export class TestContract extends SwankyCommand { description: "Run tests for all contracts", }), mocha: Flags.boolean({ - default: false, - description: "Run tests with mocha", + default: false, + description: "Run tests with mocha", }), }; @@ -69,7 +68,9 @@ export class TestContract extends SwankyCommand { console.log(`Testing contract: ${contractName}`); - if (!flags.mocha) { + if (flags.mocha) { + await this.runMochaTests(contract); + } else { await spinner.runCommand( async () => { return new Promise((resolve, reject) => { @@ -79,7 +80,7 @@ export class TestContract extends SwankyCommand { "e2e-tests", "--manifest-path", `contracts/${contractName}/Cargo.toml`, - "--release" + "--release", ]; const compile = spawn("cargo", compileArgs); @@ -114,15 +115,17 @@ export class TestContract extends SwankyCommand { `Testing ${contractName} contract`, `${contractName} testing finished successfully` ); - } else { - - const testDir = path.resolve("tests"); + } + } + } - if (!pathExistsSync(testDir)) { - throw new FileError(`Tests folder does not exist: ${testDir}`); - } + async runMochaTests(contract: Contract): Promise { + const testDir = path.resolve("tests", contract.name); + if (!pathExistsSync(testDir)) { + throw new FileError(`Test directory does not exist: ${testDir}`); + } - const artifactsCheck = await contract.artifactsExist(); + const artifactsCheck = await contract.artifactsExist(); if (!artifactsCheck.result) { throw new FileError( @@ -130,8 +133,8 @@ export class TestContract extends SwankyCommand { ); } - const artifactPath = path.resolve("typedContracts", `${contractName}`); - const typedContractCheck = await contract.typedContractExists(contractName); + const artifactPath = path.resolve("typedContracts", `${contract.name}`); + const typedContractCheck = await contract.typedContractExists(contract.name); this.log(`artifactPath: ${artifactPath}`); @@ -141,46 +144,37 @@ export class TestContract extends SwankyCommand { ); } - const reportDir = path.resolve(testDir, contract.name, "testReports"); - - await emptyDir(reportDir); - - const mocha = new Mocha({ - timeout: 200000, - reporter: "mochawesome", - reporterOptions: { - reportDir, - charts: true, - reportTitle: `${contractName} test report`, - quiet: true, - json: false, - }, - }); - - const tests = await globby(`${path.resolve(testDir, contractName)}/*.test.ts`); - - tests.forEach((test) => { - mocha.addFile(test); + const reportDir = path.resolve(testDir, "testReports"); + await emptyDir(reportDir); + + const mocha = new Mocha({ + timeout: 200000, + reporter: "mochawesome", + reporterOptions: { + reportDir, + quiet: true, + }, + }); + + const testFiles = await globby(`${testDir}/*.test.ts`); + testFiles.forEach((file) => mocha.addFile(file)); + + global.contractTypesPath = path.resolve(testDir, "typedContract"); + + + try { + await new Promise((resolve, reject) => { + mocha.run((failures) => { + if (failures) { + reject(new Error(`Tests failed. See report: ${reportDir}`)); + } else { + console.log(`All tests passed. See report: ${reportDir}`); + resolve(); + } }); - - global.contractTypesPath = path.resolve(testDir, contractName, "typedContract"); - - shell.cd(`${testDir}/${contractName}`); - try { - await new Promise((resolve, reject) => { - mocha.run((failures) => { - if (failures) { - reject(`At least one of the tests failed. Check report for details: ${reportDir}`); - } else { - this.log(`All tests passing. Check the report for details: ${reportDir}`); - resolve(); - } - }); - }); - } catch (cause) { - throw new TestError("Error in test", { cause }); - } - } + }); + } catch (error) { + throw new TestError("Mocha tests failed", { cause: error }); } } } diff --git a/yarn.lock b/yarn.lock index 68fb5e94..62b15612 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2529,14 +2529,6 @@ "@types/jsonfile" "*" "@types/node" "*" -"@types/glob@~7.2.0": - version "7.2.0" - resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.2.0.tgz#bc1b5bf3aa92f25bd5dd39f35c57361bdce5b2eb" - integrity sha512-ZUxbzKl0IfJILTS6t7ip5fQQM/J3TJYubDm3nMbgubNNYS62eXeUpoLUC8/7fJNiFYHTrGPQn7hspDUzIHX3UA== - dependencies: - "@types/minimatch" "*" - "@types/node" "*" - "@types/http-cache-semantics@*": version "4.0.4" resolved "https://registry.yarnpkg.com/@types/http-cache-semantics/-/http-cache-semantics-4.0.4.tgz#b979ebad3919799c979b17c72621c0bc0a31c6c4" @@ -2616,11 +2608,6 @@ resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.17.0.tgz#d774355e41f372d5350a4d0714abb48194a489c3" integrity sha512-t7dhREVv6dbNj0q17X12j7yDG4bD/DHYX7o5/DbDxobP0HnGPgpRz2Ej77aL7TZT3DSw13fqUTj8J4mMnqa7WA== -"@types/minimatch@*": - version "5.1.2" - resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-5.1.2.tgz#07508b45797cb81ec3f273011b054cd0755eddca" - integrity sha512-K0VQKziLUWkVKiRVrx4a40iPaxTUefQmjtkQofBkYRcoaaL/8rhwDWww9qWbrgicNOgnpIsMxyNIUM4+n6dUIA== - "@types/minimatch@^3.0.3": version "3.0.5" resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40" @@ -2660,14 +2647,6 @@ resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.8.tgz#8268a8c57a3e4abd25c165ecd36237db7948a55e" integrity sha512-I8EUhyrgfLrcTkzV3TSsGyl1tSuPrEDzr0yd5m90UgNxQkyDXULk3b6MlQqTCpZpNtWe1K0hzclnZkTcLBe2UQ== -"@types/shelljs@0.8.12": - version "0.8.12" - resolved "https://registry.yarnpkg.com/@types/shelljs/-/shelljs-0.8.12.tgz#79dc9632af7d5ca1b5afb65a6bfc1422d79b5fa0" - integrity sha512-ZA8U81/gldY+rR5zl/7HSHrG2KDfEb3lzG6uCUDhW1DTQE9yC/VBQ45fXnXq8f3CgInfhZmjtdu/WOUlrXRQUg== - dependencies: - "@types/glob" "~7.2.0" - "@types/node" "*" - "@types/sinon@*": version "17.0.3" resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-17.0.3.tgz#9aa7e62f0a323b9ead177ed23a36ea757141a5fa" @@ -7902,7 +7881,7 @@ shebang-regex@^3.0.0: resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-3.0.0.tgz#ae16f1644d873ecad843b0307b143362d4c42172" integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A== -shelljs@0.8.5, shelljs@^0.8.5: +shelljs@^0.8.5: version "0.8.5" resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.8.5.tgz#de055408d8361bed66c669d2f000538ced8ee20c" integrity sha512-TiwcRcrkhHvbrZbnRcFYMLl30Dfov3HKqzp5tO5b4pt6G/SezKcYhmDg15zXVBswHmctSAQKznqNW2LO5tTDow==