Skip to content

Commit f9beead

Browse files
committed
Add the silent option to tests
1 parent 1f7206b commit f9beead

File tree

4 files changed

+102
-8
lines changed

4 files changed

+102
-8
lines changed

lib/buildtools/src/commands/__tests__/testing.test.ts

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import pathlib from 'path';
2+
import { Command } from '@commander-js/extra-typings';
23
import { describe, expect, test, vi } from 'vitest';
34
import { testMocksDir } from '../../__tests__/fixtures.js';
45
import * as runner from '../../testing/runner.js';
56
import * as configs from '../../testing/utils.js';
6-
import { getTestCommand } from '../testing.js';
7+
import { getTestCommand, silentOption } from '../testing.js';
78
import { getCommandRunner } from './testingUtils.js';
89

910
vi.spyOn(process, 'cwd').mockReturnValue(testMocksDir);
@@ -29,7 +30,15 @@ describe('Test regular test command', () => {
2930

3031
await expect(runCommand('--project', projectPath, filterPath)).commandSuccess();
3132
expect(configs.getTestConfiguration).toHaveBeenCalledExactlyOnceWith(projectPath, false);
32-
expect(runner.runVitest).toHaveBeenCalledExactlyOnceWith('test', [filterPath], [mockConfig.config], { allowOnly: expect.any(Boolean) });
33+
expect(runner.runVitest).toHaveBeenCalledExactlyOnceWith(
34+
'test',
35+
[filterPath],
36+
[mockConfig.config],
37+
{
38+
allowOnly: expect.any(Boolean),
39+
silent: 'passed-only'
40+
}
41+
);
3342
});
3443

3544
test('Providing both the project directory but no patterns', async () => {
@@ -47,7 +56,15 @@ describe('Test regular test command', () => {
4756

4857
await expect(runCommand('--project', projectPath)).commandSuccess();
4958
expect(configs.getTestConfiguration).toHaveBeenCalledExactlyOnceWith(projectPath, false);
50-
expect(runner.runVitest).toHaveBeenCalledExactlyOnceWith('test', [], [mockConfig.config], { allowOnly: expect.any(Boolean) });
59+
expect(runner.runVitest).toHaveBeenCalledExactlyOnceWith(
60+
'test',
61+
[],
62+
[mockConfig.config],
63+
{
64+
allowOnly: expect.any(Boolean),
65+
silent: 'passed-only'
66+
}
67+
);
5168
});
5269

5370
test('Expect command to exit with no issues if no tests were found', async () => {
@@ -81,7 +98,10 @@ describe('Test regular test command', () => {
8198
'test',
8299
[],
83100
[mockConfig.config],
84-
{ allowOnly: false }
101+
{
102+
allowOnly: false,
103+
silent: 'passed-only'
104+
}
85105
);
86106
});
87107

@@ -103,10 +123,63 @@ describe('Test regular test command', () => {
103123
'test',
104124
[],
105125
[mockConfig.config],
106-
{ allowOnly: false }
126+
{
127+
allowOnly: false,
128+
silent: 'passed-only'
129+
}
107130
);
108131
} finally {
109132
vi.unstubAllEnvs();
110133
}
111134
});
112135
});
136+
137+
describe('Test silent option', () => {
138+
const runCommand = (...args: string[]) => new Promise<undefined | boolean | 'passed-only'>(
139+
(resolve, reject) => {
140+
const command = new Command()
141+
.exitOverride()
142+
.addOption(silentOption)
143+
.action(option => {
144+
resolve(option.silent);
145+
});
146+
147+
try {
148+
command.parse(args, { from: 'user' });
149+
} catch (error) {
150+
reject(error);
151+
}
152+
}
153+
);
154+
155+
test('running command without option', async () => {
156+
const value = await runCommand();
157+
expect(value).toEqual('passed-only');
158+
});
159+
160+
test('running command with without value', async () => {
161+
const value = await runCommand('--silent');
162+
expect(value).toEqual(true);
163+
});
164+
165+
test('running command with \'true\'', async () => {
166+
const value = await runCommand('--silent', 'true');
167+
expect(value).toEqual(true);
168+
});
169+
170+
test('running command with \'false\'', async () => {
171+
const value = await runCommand('--silent', 'false');
172+
expect(value).toEqual(false);
173+
});
174+
175+
test('running command with \'passed-only\'', async () => {
176+
const value = await runCommand('--silent', 'passed-only');
177+
expect(value).toEqual('passed-only');
178+
});
179+
180+
test('running command with invalid option', () => {
181+
return expect(runCommand('--silent', 'unknown'))
182+
.rejects
183+
.toThrowError('Invalid value for silent: unknown');
184+
});
185+
});

lib/buildtools/src/commands/testing.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pathlib from 'path';
2-
import { Command, Option } from '@commander-js/extra-typings';
2+
import { Command, InvalidOptionArgumentError, Option } from '@commander-js/extra-typings';
33
import { gitRoot } from '@sourceacademy/modules-repotools/getGitRoot';
44
import chalk from 'chalk';
55
import type { VitestRunMode } from 'vitest/node';
@@ -13,14 +13,32 @@ const vitestModeOption = new Option('--mode <mode>', 'Vitest Run Mode. See https
1313

1414
const watchOption = new Option('-w, --watch', 'Run tests in watch mode');
1515
const updateOption = new Option('-u, --update', 'Update snapshots');
16-
const coverageOption = new Option('--coverage');
16+
const coverageOption = new Option('--coverage', 'Run coverage testing');
17+
18+
export const silentOption = new Option('--silent [option]', 'Silent mode')
19+
.choices(['passed-only', 'false', 'true'] as const)
20+
.argParser(value => {
21+
switch (value) {
22+
case 'passed-only':
23+
return 'passed-only';
24+
case 'false':
25+
return false;
26+
case 'true':
27+
case undefined:
28+
return true;
29+
default:
30+
throw new InvalidOptionArgumentError(`Invalid value for silent: ${value}`);
31+
}
32+
})
33+
.default('passed-only');
1734

1835
export const getTestCommand = () => new Command('test')
1936
.description('Run test for the specific bundle or tab at the specified directory.')
2037
.addOption(vitestModeOption)
2138
.addOption(watchOption)
2239
.addOption(updateOption)
2340
.addOption(coverageOption)
41+
.addOption(silentOption)
2442
.option('--no-allow-only', 'Allow the use of .only in tests', !process.env.CI)
2543
.option('-p, --project <directory>', 'Path to the directory that is the root of your test project')
2644
.argument('[patterns...]', 'Test patterns to filter by.')
@@ -55,6 +73,7 @@ export const getTestAllCommand = () => new Command('testall')
5573
.addOption(watchOption)
5674
.addOption(updateOption)
5775
.addOption(coverageOption)
76+
.addOption(silentOption)
5877
.option('--no-allow-only', 'Allow the use of .only in tests', !process.env.CI)
5978
.action(async (patterns, { mode, ...options }) => {
6079
const configs = await getAllTestConfigurations(!!options.watch);

lib/buildtools/src/testing/runner.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ interface RunVitestBoolOptions {
77
coverage?: boolean;
88
update?: boolean;
99
allowOnly?: boolean;
10+
silent?: boolean | 'passed-only';
1011
}
1112

1213
function getIncludes({ test }: TestProjectInlineConfiguration) {
@@ -43,6 +44,7 @@ export async function runVitest(mode: VitestRunMode, filters: string[], projects
4344
},
4445
allowOnly: !!options.allowOnly,
4546
watch: !!options.watch,
47+
silent: options.silent
4648
};
4749

4850
const finalConfig = mergeConfig(

vitest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ export default defineConfig({
5656
`${import.meta.dirname}/lib/buildtools/src/build/docs/drawdown.ts`
5757
]
5858
},
59-
silent: 'passed-only',
59+
silent: 'passed-only'
6060
}
6161
});

0 commit comments

Comments
 (0)