Skip to content

Commit 0c9b1a8

Browse files
committed
Fix testing commands
1 parent 4f574c0 commit 0c9b1a8

File tree

63 files changed

+557
-379
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+557
-379
lines changed

devserver/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"dev": "vite",
4242
"lint": "eslint src",
4343
"postinstall": "playwright install --with-deps",
44-
"test": "buildtools test .",
44+
"test": "buildtools test --project .",
4545
"tsc": "tsc --project ./tsconfig.json"
4646
}
4747
}

lib/buildtools/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@
3838
"lint": "eslint src",
3939
"prepare": "node ./build.js",
4040
"tsc": "tsc --project ./tsconfig.json",
41-
"test": "buildtools test ."
41+
"test": "buildtools test --project ."
4242
}
4343
}

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { describe, expect, test, vi } from 'vitest';
2+
import { testMocksDir } from '../../__tests__/fixtures.js';
23
import * as runner from '../../testing/runner.js';
34
import * as configs from '../../testing/utils.js';
45
import { getTestCommand } from '../testing.js';
56
import { getCommandRunner } from './testingUtils.js';
67

8+
vi.mock(import('../../getGitRoot.js'));
79
vi.spyOn(process, 'cwd').mockReturnValue('/');
810
vi.spyOn(runner, 'runVitest').mockResolvedValue();
911

@@ -22,13 +24,15 @@ describe('Test regular test command', () => {
2224
};
2325

2426
mockedTestConfiguration.mockResolvedValueOnce(mockConfig);
27+
const projectPath = `${testMocksDir}/dir`;
2528

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+
await expect(runCommand('--project', projectPath, `${projectPath}/dir1`)).commandSuccess();
30+
expect(configs.getTestConfiguration).toHaveBeenCalledExactlyOnceWith(projectPath, false);
31+
expect(runner.runVitest).toHaveBeenCalledExactlyOnceWith('test', [`${projectPath}/dir1`], [mockConfig.config], { allowOnly: true });
2932
});
3033

3134
test('Providing both the project directory but no patterns', async () => {
35+
const projectPath = `${testMocksDir}/dir`;
3236
const mockConfig: configs.GetTestConfigurationResult = {
3337
severity: 'success',
3438
config: {
@@ -40,17 +44,23 @@ describe('Test regular test command', () => {
4044

4145
mockedTestConfiguration.mockResolvedValueOnce(mockConfig);
4246

43-
await expect(runCommand('/dir')).commandSuccess();
44-
expect(configs.getTestConfiguration).toHaveBeenCalledExactlyOnceWith('/dir', false);
45-
expect(runner.runVitest).toHaveBeenCalledExactlyOnceWith('test', [], [mockConfig.config], {});
47+
await expect(runCommand('--project', projectPath)).commandSuccess();
48+
expect(configs.getTestConfiguration).toHaveBeenCalledExactlyOnceWith(projectPath, false);
49+
expect(runner.runVitest).toHaveBeenCalledExactlyOnceWith('test', [], [mockConfig.config], { allowOnly: true });
4650
});
4751

4852
test('Expect command to exit with no issues if no tests were found', async () => {
53+
const projectPath = `${testMocksDir}/dir`;
4954
mockedTestConfiguration.mockResolvedValueOnce({
5055
severity: 'success',
5156
config: null
5257
});
5358

54-
await expect(runCommand('/dir')).commandSuccess();
59+
await expect(runCommand('--project', projectPath)).commandSuccess();
60+
});
61+
62+
test('Command should error if the command was called from beyond the git root', async () => {
63+
const projectPath = `${testMocksDir}/..`;
64+
await expect(runCommand('--project', projectPath)).commandExit();
5565
});
5666
});

lib/buildtools/src/commands/testing.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { resolve } from 'path';
1+
import pathlib from 'path';
22
import { Command, Option } from '@commander-js/extra-typings';
33
import chalk from 'chalk';
44
import type { VitestRunMode } from 'vitest/node';
5+
import { getGitRoot } from '../getGitRoot.js';
56
import { runVitest } from '../testing/runner.js';
67
import { getAllTestConfigurations, getTestConfiguration } from '../testing/utils.js';
78
import { logCommandErrorAndExit } from './commandUtils.js';
@@ -13,18 +14,28 @@ const vitestModeOption = new Option('--mode <mode>', 'Vitest Run Mode. See https
1314
const watchOption = new Option('-w, --watch', 'Run tests in watch mode');
1415
const updateOption = new Option('-u, --update', 'Update snapshots');
1516
const coverageOption = new Option('--coverage');
17+
const allowOnlyOption = new Option('--no-allow-only', 'Allow the use of .only')
18+
.default(!process.env.CI);
1619

1720
export const getTestCommand = () => new Command('test')
1821
.description('Run test for the specific bundle or tab at the specified directory.')
1922
.addOption(vitestModeOption)
2023
.addOption(watchOption)
2124
.addOption(updateOption)
2225
.addOption(coverageOption)
23-
.argument('[project]', 'Directory that contains the vitest config file')
26+
.addOption(allowOnlyOption)
27+
.option('-p, --project <directory>', 'Path to the directory that is the root of your test project')
2428
.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));
29+
.action(async (patterns, { mode, project, ...options }) => {
30+
const gitRoot = await getGitRoot();
31+
const relative = pathlib.relative(project ?? process.cwd(), gitRoot);
32+
33+
if (relative && !relative.startsWith('..') && !pathlib.isAbsolute(relative)) {
34+
logCommandErrorAndExit('Cannot be run from a parent directory of the git root');
35+
}
36+
37+
const fullyResolvedProject = pathlib.resolve(project ?? process.cwd());
38+
const fullyResolvedPatterns = patterns.map(each => pathlib.resolve(process.cwd(), each));
2839

2940
const configResult = await getTestConfiguration(fullyResolvedProject, !!options.watch);
3041

@@ -47,6 +58,7 @@ export const getTestAllCommand = () => new Command('testall')
4758
.addOption(watchOption)
4859
.addOption(updateOption)
4960
.addOption(coverageOption)
61+
.addOption(allowOnlyOption)
5062
.action(async (patterns, { mode, ...options }) => {
5163
const configs = await getAllTestConfigurations(!!options.watch);
5264
if (configs.length === 0) {

0 commit comments

Comments
 (0)