Skip to content

Commit

Permalink
feat: ✨ added vitest arg passthrough (#451)
Browse files Browse the repository at this point in the history
* feat: ✨ added vitest arg passthrough

* feat: ✨ Added vitest arg passthrough to moonwall config

* style: 💄 lint
  • Loading branch information
timbrinded authored Dec 5, 2024
1 parent 543bda2 commit 7cce71c
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changeset/good-shoes-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@moonwall/cli": patch
"@moonwall/tests": patch
---

Added vitest arg pass through
30 changes: 30 additions & 0 deletions .changeset/smooth-bears-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
"@moonwall/cli": patch
---

### Enhanced Test CLI Options

#### New Features

- Added support for passing Vitest configuration options directly through CLI

```bash
moonwall test dev_test --vitest "bail=2 retry=2"
```

This can also be added to your `moonwall.config.json` file like:

```json
{
"name": "passthrough_test",
"testFileDir": ["suites/multi_fail"],
"description": "Testing that bail can be passed through",
"foundation": {
"type": "read_only"
},
"vitestArgs": {
"bail": 1
},
"connections": []
}
```
4 changes: 0 additions & 4 deletions TODO

This file was deleted.

6 changes: 6 additions & 0 deletions packages/cli/src/cmds/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ yargs(hideBin(process.argv))
describe: "Update all snapshots",
alias: "u",
type: "boolean",
})
.option("vitestArgPassthrough", {
describe: "Arguments to pass directly to Vitest (space-delimited)",
alias: "vitest",
type: "string",
});
},
async (args) => {
Expand All @@ -129,6 +134,7 @@ yargs(hideBin(process.argv))
subDirectory: args.subDirectory,
shard: args.testShard,
update: args.update,
vitestPassthroughArgs: args.vitestArgPassthrough?.split(" "),
}))
) {
process.exitCode = 1;
Expand Down
39 changes: 32 additions & 7 deletions packages/cli/src/cmds/runTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export type testRunArgs = {
subDirectory?: string;
shard?: string;
update?: boolean;
vitestPassthroughArgs?: string[];
};

export async function executeTests(env: Environment, testRunArgs?: testRunArgs) {
Expand Down Expand Up @@ -92,7 +93,23 @@ export async function executeTests(env: Environment, testRunArgs?: testRunArgs)
}
}

const additionalArgs: testRunArgs = testRunArgs || {};
const additionalArgs: Omit<testRunArgs, "vitestPassthroughArgs"> = testRunArgs
? {
testNamePattern: testRunArgs.testNamePattern,
subDirectory: testRunArgs.subDirectory,
shard: testRunArgs.shard,
update: testRunArgs.update,
}
: {};

const vitestOptions = testRunArgs?.vitestPassthroughArgs?.reduce<UserConfig>((acc, arg) => {
const [key, value] = arg.split("=");
return {
// biome-ignore lint/performance/noAccumulatingSpread: this is fine
...acc,
[key]: Number(value) || value,
};
}, {});

// transform in regexp pattern
if (env.skipTests && env.skipTests.length > 0) {
Expand All @@ -107,6 +124,7 @@ export async function executeTests(env: Environment, testRunArgs?: testRunArgs)
.setTimeout(env.timeout || globalConfig.defaultTestTimeout)
.setInclude(env.include || ["**/*{test,spec,test_,test-}*{ts,mts,cts}"])
.addThreadConfig(env.multiThreads)
.addVitestPassthroughArgs(env.vitestArgs)
.build();

if (
Expand All @@ -126,12 +144,14 @@ export async function executeTests(env: Environment, testRunArgs?: testRunArgs)
: env.testFileDir;

const folders = testFileDir.map((folder) => path.join(".", folder, "/"));
resolve(
(await startVitest("test", folders, {
...options,
...additionalArgs,
})) as Vitest
);
const optionsToUse = {
...options,
...additionalArgs,
...vitestOptions,
} satisfies UserConfig;

console.log(`Options to use: ${JSON.stringify(optionsToUse, null, 2)}`);
resolve((await startVitest("test", folders, optionsToUse)) as Vitest);
} catch (e) {
console.error(e);
reject(e);
Expand Down Expand Up @@ -199,6 +219,11 @@ class VitestOptionsBuilder {
return this;
}

addVitestPassthroughArgs(args?: object): this {
this.options = { ...this.options, ...args };
return this;
}

addThreadConfig(threads: number | boolean | object = false): this {
this.options.fileParallelism = false;
this.options.pool = "forks";
Expand Down
7 changes: 7 additions & 0 deletions packages/types/config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,9 @@
},
"type": "object"
},
"Record<string,any>": {
"type": "object"
},
"ZombieLaunchSpec": {
"description": "A launch specification object for the \"zombie\" foundation type.",
"properties": {
Expand Down Expand Up @@ -679,6 +682,10 @@
"timeout": {
"description": "The default timeout for tests and hooks",
"type": "number"
},
"vitestArgs": {
"$ref": "#/definitions/Record<string,any>",
"description": "An optional object to add extra arguments to the Vitest test runner.\n Use with caution as this will override the default arguments, which\nmay cause unexpected behaviour.\n\nVisit https://vitest.dev/config/ for more info"
}
},
"type": "object"
Expand Down
1 change: 1 addition & 0 deletions packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"ethers": "*",
"polkadot-api": "*",
"viem": "*",
"vitest": "*",
"web3": "*"
},
"publishConfig": {
Expand Down
9 changes: 9 additions & 0 deletions packages/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ export type Environment = {
* A list of test to skip.
*/
skipTests?: SkipTestSpec[];

/**
* An optional object to add extra arguments to the Vitest test runner.
* Use with caution as this will override the default arguments, which
* may cause unexpected behaviour.
*
* Visit https://vitest.dev/config/ for more info
*/
vitestArgs?: Record<string, any>;
};

export type SkipTestSpec = {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions test/moonwall.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@
},
"connections": []
},
{
"name": "passthrough_test",
"testFileDir": ["suites/multi_fail"],
"description": "Testing that bail can be passed through",
"foundation": {
"type": "read_only",
"launchSpec": {
"disableRuntimeVersionCheck": true
}
},
"vitestArgs": {
"bail": 3,
"retry": 4
},
"connections": []
},
{
"name": "failing_prescript",
"testFileDir": ["suites/basic"],
Expand Down
57 changes: 57 additions & 0 deletions test/suites/multi_fail/test_multifails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { expect, describeSuite, beforeAll } from "@moonwall/cli";
import { setupLogger } from "@moonwall/util";
import { setTimeout } from "timers/promises";
describeSuite({
id: "F01",
title: "This test suite fails multiple times",
foundationMethods: "read_only",
testCases: ({ it, log }) => {
const anotherLogger = setupLogger("additional");

beforeAll(() => {
log("Test suite setup");
});

it({
id: "T01",
title: "This is a bool test case",
test: async() => {
await setTimeout(500)
expect(false).to.be.true;
},
});
it({
id: "T02",
title: "This is a bool test case",
test: async() => {
const rand = Math.floor(Math.random() * 1000);
expect(rand).toBeGreaterThan(800)
},
});
it({
id: "T03",
title: "This is a bool test case",
test: async() => {
await setTimeout(500)
expect(false).to.be.true;
},
});
it({
id: "T04",
title: "This is a bool test case",
test: async() => {
await setTimeout(500)
expect(false).to.be.true;
},
});
it({
id: "T05",
title: "This is a bool test case",
test: async() => {
await setTimeout(500)
expect(false).to.be.true;
},
});

},
});

0 comments on commit 7cce71c

Please sign in to comment.