Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/star-and-slash-fixes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'test-inventory': patch
---

A `#` import or workspace package export whose target repeats the `*` of its pattern, as in
`"#desks/*": "./desks/*/*-fixtures.ts"`, is followed to the file Node.js loads, with every `*` filled in. Only the
first was filled in before, so the import wasn't followed and its tests were missing, with an `unresolved-test-import`
error. A folder given as the pattern with a long run of slashes, as in `tests////unit`, no longer slows the scan down
while the hint for it is written.
8 changes: 5 additions & 3 deletions src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function aliasFiles(specifier: string, options: PathOptions): string[] | null {
const match = options.paths ? matchPattern(Object.keys(options.paths), specifier) : null;
if (!match || !options.paths) return null;
return targetStrings(options.paths[match.key]).map((target) =>
path.resolve(options.pathsBase, target.replaceAll('${configDir}', options.configDir).replace('*', match.star)),
path.resolve(options.pathsBase, target.replaceAll('${configDir}', options.configDir).replaceAll('*', match.star)),
);
}

Expand All @@ -215,7 +215,9 @@ function subpathImportFiles(specifier: string, directory: string): string[] {
if (manifest) {
const match = imports ? matchPattern(Object.keys(imports), specifier) : null;
if (!match || !imports) return [];
return targetStrings(imports[match.key]).map((target) => path.resolve(folder, target.replace('*', match.star)));
return targetStrings(imports[match.key]).map((target) =>
path.resolve(folder, target.replaceAll('*', match.star)),
);
}
if (path.dirname(folder) === folder) return [];
}
Expand Down Expand Up @@ -248,7 +250,7 @@ function packageEntry(folder: string, subpath: string): { file: string } | { rea
let targets: string[];
if (isSubpathMap) {
const match = matchPattern(Object.keys(map), subpath);
targets = match ? targetStrings(map[match.key]).map((target) => target.replace('*', match.star)) : [];
targets = match ? targetStrings(map[match.key]).map((target) => target.replaceAll('*', match.star)) : [];
} else {
targets = subpath === '.' ? targetStrings(exports) : [];
}
Expand Down
5 changes: 4 additions & 1 deletion src/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ function folderHint(root: string, patterns: string[], framework: Framework): str
return existsSync(target) && statSync(target).isDirectory();
});
if (folder === undefined) return '';
const example = `${folder.replace(/[\\/]+$/, '')}/**/*.${framework === 'playwright' ? 'spec' : 'test'}.ts`;
// A loop, as a regular expression for trailing slashes takes quadratic time on a long run of slashes.
let end = folder.length;
while (end > 0 && '/\\'.includes(folder[end - 1])) end -= 1;
const example = `${folder.slice(0, end)}/**/*.${framework === 'playwright' ? 'spec' : 'test'}.ts`;
return ` "${folder}" is a folder; to scan the test files in it, use a pattern such as "${example}".`;
}

Expand Down
26 changes: 26 additions & 0 deletions tests/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ describe('a custom test object imported through package.json imports', () => {
]);
});

it('is followed through a pattern whose target repeats the star, as Node fills in each one', async () => {
const inventory = await scanSpec({
'package.json': JSON.stringify({
devDependencies: { '@playwright/test': '1.63.0' },
imports: { '#desks/*': './desks/*/*-fixtures.ts' },
}),
'desks/equities/equities-fixtures.ts': FIXTURES,
'tests/portfolio.spec.ts': spec('#desks/equities'),
});
expect(inventory.summary.testCount).toBe(1);
expect(inventory.diagnostics).toEqual([]);
});

it('is reported when no package.json is found above the file', async () => {
const inventory = await scanSpec({ 'tests/portfolio.spec.ts': spec('#fixtures') }, 'playwright');
expect(messages(inventory)).toContain(
Expand Down Expand Up @@ -280,6 +293,19 @@ describe('a custom test object from a workspace package', () => {
expect(inventory.diagnostics).toEqual([]);
});

it('is followed through a subpath pattern whose target repeats the star, as Node fills in each one', async () => {
const root = workspace(
{ exports: { './*': './src/*/*-fixtures.ts' } },
{
'packages/test-utils/src/equities/equities-fixtures.ts': FIXTURES,
'tests/portfolio.spec.ts': spec('@trading/test-utils/equities'),
},
);
const inventory = await scan({ root, patterns: ['tests/*.spec.ts'], framework: 'playwright' });
expect(inventory.summary.testCount).toBe(1);
expect(inventory.diagnostics).toEqual([]);
});

it.each([
[
'only built JavaScript with no source next to it',
Expand Down
7 changes: 7 additions & 0 deletions tests/scan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ describe('scan', () => {
]);
});

it('says how to scan a folder given with a long run of slashes, without slowing down', async () => {
const root = project({ 'package.json': PLAYWRIGHT_PACKAGE, 'tests/unit/orders.spec.ts': SPEC });
const inventory = await scanProject(root, { patterns: [`tests${'/'.repeat(200_000)}unit`] });
expect(inventory.diagnostics.map((diagnostic) => diagnostic.code)).toEqual(['no-files-matched']);
expect(inventory.diagnostics[0].message).toMatch(/use a pattern such as "tests\/+unit\/\*\*\/\*\.spec\.ts"\.$/);
});

it('does not read files inside a folder named like a test file', async () => {
const root = project({
'package.json': PLAYWRIGHT_PACKAGE,
Expand Down
Loading