From 4226ef917598fe58ef69f34c7b50c6522ee8bfd2 Mon Sep 17 00:00:00 2001 From: timbrinded <79199034+timbrinded@users.noreply.github.com> Date: Mon, 13 Nov 2023 16:41:45 +0000 Subject: [PATCH] updated test fns --- packages/cli/src/cmds/entrypoint.ts | 35 +++++++++------------- packages/cli/src/cmds/runTests.ts | 45 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 21 deletions(-) diff --git a/packages/cli/src/cmds/entrypoint.ts b/packages/cli/src/cmds/entrypoint.ts index 17b140dd..7adfb006 100755 --- a/packages/cli/src/cmds/entrypoint.ts +++ b/packages/cli/src/cmds/entrypoint.ts @@ -3,7 +3,7 @@ import "@moonbeam-network/api-augment"; import yargs from "yargs"; import fs from "fs"; import { hideBin } from "yargs/helpers"; -import { testCmd } from "./runTests"; +import { testEffect } from "./runTests"; import { runNetworkCmd } from "./runNetwork"; import { generateConfig } from "../internal/cmdFunctions/initialisation"; import { fetchArtifact } from "../internal/cmdFunctions/fetchArtifact"; @@ -119,6 +119,7 @@ const cliStart = Effect.try(() => { .command( `test [GrepTest]`, "Run tests for a given Environment", + (yargs) => yargs .positional("envName", { @@ -130,18 +131,19 @@ const cliStart = Effect.try(() => { type: "string", description: "Pattern to grep test ID/Description to run", }), + async ({ envName, GrepTest }) => { - const effect = pipe( + const effect = Effect.all([ + setEnvVar("MOON_RUN_SCRIPTS", "true"), Effect.gen(function* (_) { if (envName) { - return yield* _(runTestEffect(envName as any, GrepTest)); + return yield* _(testEffect(envName as any, { testNamePattern: GrepTest })); } else { yield* _(Effect.logError("👉 Run 'pnpm moonwall --help' for more information")); - yield* _(Effect.fail("❌ No environment specified")); + return yield* _(Effect.fail("❌ No environment specified")); } }), - Effect.catchAll((error: any) => Effect.logError(`Error: ${error.message}`)) - ); + ]); await Effect.runPromise(effect); } @@ -159,10 +161,12 @@ const cliStart = Effect.try(() => { description: "Pattern to grep test ID/Description to run", }), async (argv) => { - const effect = Effect.gen(function* (_) { - yield* _(setEnvVar("MOON_RUN_SCRIPTS", "true")); - yield* _(Effect.tryPromise(() => runNetworkCmd(argv as any))); - }); + const effect = Effect.all([ + setEnvVar("MOON_RUN_SCRIPTS", "true"), + Effect.gen(function* (_) { + yield* _(Effect.tryPromise(() => runNetworkCmd(argv as any))); + }), + ]); await Effect.runPromise(effect); } @@ -172,17 +176,6 @@ const cliStart = Effect.try(() => { .parse(); }); -const runTestEffect = (envName: string, grepTest?: string) => - pipe( - setEnvVar("MOON_RUN_SCRIPTS", "true"), - Effect.flatMap(() => Effect.tryPromise(() => testCmd(envName, { testNamePattern: grepTest }))), - Effect.tap((result) => - Effect.succeed(() => { - process.exitCode = result ? 0 : 1; - }) - ) - ); - const cli = pipe( setupConfigFileEnv, Effect.flatMap(() => cliStart), diff --git a/packages/cli/src/cmds/runTests.ts b/packages/cli/src/cmds/runTests.ts index a3dedd1b..05870fbf 100644 --- a/packages/cli/src/cmds/runTests.ts +++ b/packages/cli/src/cmds/runTests.ts @@ -1,6 +1,7 @@ import { Environment } from "@moonwall/types"; import chalk from "chalk"; import path from "path"; +import { Effect } from "effect"; import type { UserConfig, Vitest } from "vitest"; import { startVitest } from "vitest/node"; import { clearNodeLogs } from "../internal/cmdFunctions/tempLogs"; @@ -8,6 +9,50 @@ import { commonChecks } from "../internal/launcherCommon"; import { cacheConfig, importAsyncConfig, loadEnvVars } from "../lib/configReader"; import { MoonwallContext, contextCreator, runNetworkOnly } from "../lib/globalContext"; +export const testEffect = (envName: string, additionalArgs?: object) => { + return Effect.gen(function* (_) { + yield* _(Effect.tryPromise(() => cacheConfig())); + const globalConfig = yield* _(Effect.tryPromise(() => importAsyncConfig())); + const env = yield* _( + Effect.sync(() => globalConfig.environments.find(({ name }) => name === envName)!) + ); + yield* _(Effect.sync(() => (process.env.MOON_TEST_ENV = envName))); + + if (!env) { + const envList = yield* _(Effect.sync(() => globalConfig.environments.map((env) => env.name))); + return Effect.fail( + `No environment found in config for: ${chalk.bgWhiteBright.blackBright( + envName + )}\n Environments defined in config are: ${envList}\n` + ); + } + + yield* _(Effect.sync(() => loadEnvVars())); + yield* _(Effect.promise(() => commonChecks(env))); + + if ( + (env.foundation.type == "dev" && !env.foundation.launchSpec[0].retainAllLogs) || + (env.foundation.type == "chopsticks" && !env.foundation.launchSpec[0].retainAllLogs) + ) { + yield* _(Effect.sync(() => clearNodeLogs())); + } + const vitest = yield* _(Effect.promise(() => executeTests(env, additionalArgs))); + const failed = yield* _( + Effect.sync(() => vitest!.state.getFiles().filter((file) => file.result!.state === "fail")) + ); + + if (failed.length === 0) { + console.log("✅ All tests passed"); + Effect.logInfo("✅ All tests passed"); + return true; + } else { + console.log("❌ Some tests failed"); + Effect.logInfo("❌ Some tests failed"); + return Effect.fail("❌ Some tests failed"); + } + }); +}; + export async function testCmd(envName: string, additionalArgs?: object): Promise { await cacheConfig(); const globalConfig = await importAsyncConfig();