Skip to content

Commit 4f574c0

Browse files
committed
Add tests for test command
1 parent 50af717 commit 4f574c0

File tree

7 files changed

+640
-85
lines changed

7 files changed

+640
-85
lines changed

lib/buildtools/build.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const command = getBuildCommand({
1818
outfile: './bin/index.js',
1919
packages: 'external',
2020
platform: 'node',
21-
target: 'node20',
2221
tsconfig: './tsconfig.json',
2322
plugins: [{
2423
// If the given path resolves to something in the root of the repository
@@ -29,10 +28,8 @@ const command = getBuildCommand({
2928
filter: /^\.{1,2}(\/.+)?/
3029
}, args => {
3130
const absolutePath = pathlib.resolve(args.resolveDir, args.path);
32-
const { dir, ext, base } = pathlib.parse(absolutePath);
33-
// evan claims that esbuild doesn't rewrite import attributes
34-
// but i can't get json imports to work properly
35-
if (ext === '.json' || dir !== repoRoot) return undefined;
31+
const { dir, base } = pathlib.parse(absolutePath);
32+
if (dir !== repoRoot) return undefined;
3633

3734
// The new import path should be relative to the bin/index.js file
3835
const newDirectory = pathlib.relative('./bin', repoRoot);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { describe, expect, test, vi } from 'vitest';
2+
import * as runner from '../../testing/runner.js';
3+
import * as configs from '../../testing/utils.js';
4+
import { getTestCommand } from '../testing.js';
5+
import { getCommandRunner } from './testingUtils.js';
6+
7+
vi.spyOn(process, 'cwd').mockReturnValue('/');
8+
vi.spyOn(runner, 'runVitest').mockResolvedValue();
9+
10+
describe('Test regular test command', () => {
11+
const mockedTestConfiguration = vi.spyOn(configs, 'getTestConfiguration');
12+
const runCommand = getCommandRunner(getTestCommand);
13+
14+
test('Providing both the project directory and pattern', async () => {
15+
const mockConfig: configs.GetTestConfigurationResult = {
16+
severity: 'success',
17+
config: {
18+
test: {
19+
name: 'Test0'
20+
}
21+
}
22+
};
23+
24+
mockedTestConfiguration.mockResolvedValueOnce(mockConfig);
25+
26+
await expect(runCommand('/dir', '/dir/dir1')).commandSuccess();
27+
expect(configs.getTestConfiguration).toHaveBeenCalledExactlyOnceWith('/dir', false);
28+
expect(runner.runVitest).toHaveBeenCalledExactlyOnceWith('test', ['/dir/dir1'], [mockConfig.config], {});
29+
});
30+
31+
test('Providing both the project directory but no patterns', async () => {
32+
const mockConfig: configs.GetTestConfigurationResult = {
33+
severity: 'success',
34+
config: {
35+
test: {
36+
name: 'Test0'
37+
}
38+
}
39+
};
40+
41+
mockedTestConfiguration.mockResolvedValueOnce(mockConfig);
42+
43+
await expect(runCommand('/dir')).commandSuccess();
44+
expect(configs.getTestConfiguration).toHaveBeenCalledExactlyOnceWith('/dir', false);
45+
expect(runner.runVitest).toHaveBeenCalledExactlyOnceWith('test', [], [mockConfig.config], {});
46+
});
47+
48+
test('Expect command to exit with no issues if no tests were found', async () => {
49+
mockedTestConfiguration.mockResolvedValueOnce({
50+
severity: 'success',
51+
config: null
52+
});
53+
54+
await expect(runCommand('/dir')).commandSuccess();
55+
});
56+
});

lib/buildtools/src/commands/testing.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,39 @@ export const getTestCommand = () => new Command('test')
2020
.addOption(watchOption)
2121
.addOption(updateOption)
2222
.addOption(coverageOption)
23-
.argument('[directory]', 'Directory to search for tests. If no directory is specified, the current working directory is used')
24-
.action(async (directory, { mode, ...options }) => {
25-
const fullyResolved = resolve(directory ?? process.cwd());
26-
const configResult = await getTestConfiguration(fullyResolved, !!options.watch);
23+
.argument('[project]', 'Directory that contains the vitest config file')
24+
.argument('[patterns...]', 'Test patterns to filter by.')
25+
.action(async (project, patterns, { mode, ...options }) => {
26+
const fullyResolvedProject = resolve(project ?? process.cwd());
27+
const fullyResolvedPatterns = patterns.map(each => resolve(process.cwd(), each));
28+
29+
const configResult = await getTestConfiguration(fullyResolvedProject, !!options.watch);
2730

2831
if (configResult.severity === 'error') {
2932
logCommandErrorAndExit(configResult);
3033
}
3134

3235
if (configResult.config === null) {
33-
console.log(chalk.yellowBright(`No tests found for in ${fullyResolved}`));
36+
console.log(chalk.yellowBright(`No tests found for ${fullyResolvedProject}`));
3437
return;
3538
}
3639

37-
await runVitest(mode, [fullyResolved], [configResult.config], options);
40+
await runVitest(mode, fullyResolvedPatterns, [configResult.config], options);
3841
});
3942

4043
export const getTestAllCommand = () => new Command('testall')
4144
.description('Run all tests based on the configuration of the root vitest file')
45+
.argument('[patterns...]', 'Test patterns to filter by.')
4246
.addOption(vitestModeOption)
4347
.addOption(watchOption)
4448
.addOption(updateOption)
4549
.addOption(coverageOption)
46-
.action(async ({ mode, ...options }) => {
50+
.action(async (patterns, { mode, ...options }) => {
4751
const configs = await getAllTestConfigurations(!!options.watch);
48-
await runVitest(mode, undefined, configs, options);
52+
if (configs.length === 0) {
53+
console.log(chalk.yellowBright('No tests found.'));
54+
return;
55+
}
56+
57+
await runVitest(mode, patterns, configs, options);
4958
});

0 commit comments

Comments
 (0)