Skip to content

Commit 7cce71c

Browse files
authored
feat: ✨ added vitest arg passthrough (#451)
* feat: ✨ added vitest arg passthrough * feat: ✨ Added vitest arg passthrough to moonwall config * style: 💄 lint
1 parent 543bda2 commit 7cce71c

File tree

11 files changed

+167
-11
lines changed

11 files changed

+167
-11
lines changed

.changeset/good-shoes-grab.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@moonwall/cli": patch
3+
"@moonwall/tests": patch
4+
---
5+
6+
Added vitest arg pass through

.changeset/smooth-bears-hunt.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
"@moonwall/cli": patch
3+
---
4+
5+
### Enhanced Test CLI Options
6+
7+
#### New Features
8+
9+
- Added support for passing Vitest configuration options directly through CLI
10+
11+
```bash
12+
moonwall test dev_test --vitest "bail=2 retry=2"
13+
```
14+
15+
This can also be added to your `moonwall.config.json` file like:
16+
17+
```json
18+
{
19+
"name": "passthrough_test",
20+
"testFileDir": ["suites/multi_fail"],
21+
"description": "Testing that bail can be passed through",
22+
"foundation": {
23+
"type": "read_only"
24+
},
25+
"vitestArgs": {
26+
"bail": 1
27+
},
28+
"connections": []
29+
}
30+
```

TODO

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/cli/src/cmds/entrypoint.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ yargs(hideBin(process.argv))
118118
describe: "Update all snapshots",
119119
alias: "u",
120120
type: "boolean",
121+
})
122+
.option("vitestArgPassthrough", {
123+
describe: "Arguments to pass directly to Vitest (space-delimited)",
124+
alias: "vitest",
125+
type: "string",
121126
});
122127
},
123128
async (args) => {
@@ -129,6 +134,7 @@ yargs(hideBin(process.argv))
129134
subDirectory: args.subDirectory,
130135
shard: args.testShard,
131136
update: args.update,
137+
vitestPassthroughArgs: args.vitestArgPassthrough?.split(" "),
132138
}))
133139
) {
134140
process.exitCode = 1;

packages/cli/src/cmds/runTests.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export type testRunArgs = {
5555
subDirectory?: string;
5656
shard?: string;
5757
update?: boolean;
58+
vitestPassthroughArgs?: string[];
5859
};
5960

6061
export async function executeTests(env: Environment, testRunArgs?: testRunArgs) {
@@ -92,7 +93,23 @@ export async function executeTests(env: Environment, testRunArgs?: testRunArgs)
9293
}
9394
}
9495

95-
const additionalArgs: testRunArgs = testRunArgs || {};
96+
const additionalArgs: Omit<testRunArgs, "vitestPassthroughArgs"> = testRunArgs
97+
? {
98+
testNamePattern: testRunArgs.testNamePattern,
99+
subDirectory: testRunArgs.subDirectory,
100+
shard: testRunArgs.shard,
101+
update: testRunArgs.update,
102+
}
103+
: {};
104+
105+
const vitestOptions = testRunArgs?.vitestPassthroughArgs?.reduce<UserConfig>((acc, arg) => {
106+
const [key, value] = arg.split("=");
107+
return {
108+
// biome-ignore lint/performance/noAccumulatingSpread: this is fine
109+
...acc,
110+
[key]: Number(value) || value,
111+
};
112+
}, {});
96113

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

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

128146
const folders = testFileDir.map((folder) => path.join(".", folder, "/"));
129-
resolve(
130-
(await startVitest("test", folders, {
131-
...options,
132-
...additionalArgs,
133-
})) as Vitest
134-
);
147+
const optionsToUse = {
148+
...options,
149+
...additionalArgs,
150+
...vitestOptions,
151+
} satisfies UserConfig;
152+
153+
console.log(`Options to use: ${JSON.stringify(optionsToUse, null, 2)}`);
154+
resolve((await startVitest("test", folders, optionsToUse)) as Vitest);
135155
} catch (e) {
136156
console.error(e);
137157
reject(e);
@@ -199,6 +219,11 @@ class VitestOptionsBuilder {
199219
return this;
200220
}
201221

222+
addVitestPassthroughArgs(args?: object): this {
223+
this.options = { ...this.options, ...args };
224+
return this;
225+
}
226+
202227
addThreadConfig(threads: number | boolean | object = false): this {
203228
this.options.fileParallelism = false;
204229
this.options.pool = "forks";

packages/types/config_schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,9 @@
454454
},
455455
"type": "object"
456456
},
457+
"Record<string,any>": {
458+
"type": "object"
459+
},
457460
"ZombieLaunchSpec": {
458461
"description": "A launch specification object for the \"zombie\" foundation type.",
459462
"properties": {
@@ -679,6 +682,10 @@
679682
"timeout": {
680683
"description": "The default timeout for tests and hooks",
681684
"type": "number"
685+
},
686+
"vitestArgs": {
687+
"$ref": "#/definitions/Record<string,any>",
688+
"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"
682689
}
683690
},
684691
"type": "object"

packages/types/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"ethers": "*",
6767
"polkadot-api": "*",
6868
"viem": "*",
69+
"vitest": "*",
6970
"web3": "*"
7071
},
7172
"publishConfig": {

packages/types/src/config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ export type Environment = {
132132
* A list of test to skip.
133133
*/
134134
skipTests?: SkipTestSpec[];
135+
136+
/**
137+
* An optional object to add extra arguments to the Vitest test runner.
138+
* Use with caution as this will override the default arguments, which
139+
* may cause unexpected behaviour.
140+
*
141+
* Visit https://vitest.dev/config/ for more info
142+
*/
143+
vitestArgs?: Record<string, any>;
135144
};
136145

137146
export type SkipTestSpec = {

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/moonwall.config.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@
8989
},
9090
"connections": []
9191
},
92+
{
93+
"name": "passthrough_test",
94+
"testFileDir": ["suites/multi_fail"],
95+
"description": "Testing that bail can be passed through",
96+
"foundation": {
97+
"type": "read_only",
98+
"launchSpec": {
99+
"disableRuntimeVersionCheck": true
100+
}
101+
},
102+
"vitestArgs": {
103+
"bail": 3,
104+
"retry": 4
105+
},
106+
"connections": []
107+
},
92108
{
93109
"name": "failing_prescript",
94110
"testFileDir": ["suites/basic"],

0 commit comments

Comments
 (0)