[type-summarizer/integration] fix flaky tests (#127038)

This commit is contained in:
Spencer 2022-03-07 08:24:04 -08:00 committed by GitHub
parent 73f7ac419f
commit 004f0d4daa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 27 additions and 19 deletions

View file

@ -17,6 +17,8 @@ import { tryReadFile } from './helpers/fs';
import { parseJson } from './helpers/json';
import { isNodeModule } from './is_node_module';
type SourceMapConsumerEntry = [ts.SourceFile, BasicSourceMapConsumer | undefined];
export class SourceMapper {
static async forSourceFiles(
log: Logger,
@ -24,10 +26,8 @@ export class SourceMapper {
repoRelativePackageDir: string,
sourceFiles: readonly ts.SourceFile[]
) {
const consumers = new Map<ts.SourceFile, BasicSourceMapConsumer | undefined>();
await Promise.all(
sourceFiles.map(async (sourceFile) => {
const entries = await Promise.all(
sourceFiles.map(async (sourceFile): Promise<undefined | SourceMapConsumerEntry> => {
if (isNodeModule(dtsDir, sourceFile.fileName)) {
return;
}
@ -35,8 +35,7 @@ export class SourceMapper {
const text = sourceFile.getText();
const match = text.match(/^\/\/#\s*sourceMappingURL=(.*)/im);
if (!match) {
consumers.set(sourceFile, undefined);
return;
return [sourceFile, undefined];
}
const relSourceFile = Path.relative(process.cwd(), sourceFile.fileName);
@ -50,11 +49,16 @@ export class SourceMapper {
}
const json = parseJson(sourceJson, `source map at [${relSourceMapPath}]`);
consumers.set(sourceFile, await new SourceMapConsumer(json));
log.debug('loaded sourcemap for', relSourceFile);
return [sourceFile, await new SourceMapConsumer(json)];
})
);
const consumers = new Map(entries.filter((e): e is SourceMapConsumerEntry => !!e));
log.debug(
'loaded sourcemaps for',
Array.from(consumers.keys()).map((s) => Path.relative(process.cwd(), s.fileName))
);
return new SourceMapper(consumers, repoRelativePackageDir);
}

View file

@ -69,7 +69,7 @@ it('prints basic class correctly', async () => {
}
`);
expect(output.logs).toMatchInlineSnapshot(`
"debug loaded sourcemap for packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts' ]
debug Ignoring 1 global declarations for \\"Record\\"
debug Ignoring 5 global declarations for \\"Promise\\"
"

View file

@ -74,8 +74,10 @@ it('prints the function declaration, including comments', async () => {
}
`);
expect(result.logs).toMatchInlineSnapshot(`
"debug loaded sourcemap for packages/kbn-type-summarizer/tests/__tmp__/dist_dts/bar.d.ts
debug loaded sourcemap for packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts
"debug loaded sourcemaps for [
'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/bar.d.ts',
'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts'
]
"
`);
});

View file

@ -52,7 +52,7 @@ it('output type links to named import from node modules', async () => {
}
`);
expect(output.logs).toMatchInlineSnapshot(`
"debug loaded sourcemap for packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts' ]
"
`);
});
@ -84,7 +84,7 @@ it('output type links to default import from node modules', async () => {
}
`);
expect(output.logs).toMatchInlineSnapshot(`
"debug loaded sourcemap for packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts' ]
"
`);
});

View file

@ -55,7 +55,7 @@ it('prints the whole interface, including comments', async () => {
}
`);
expect(result.logs).toMatchInlineSnapshot(`
"debug loaded sourcemap for packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts' ]
debug Ignoring 5 global declarations for \\"Promise\\"
"
`);

View file

@ -59,9 +59,11 @@ it('collects references from source files which contribute to result', async ()
}
`);
expect(result.logs).toMatchInlineSnapshot(`
"debug loaded sourcemap for packages/kbn-type-summarizer/tests/__tmp__/dist_dts/files/foo.d.ts
debug loaded sourcemap for packages/kbn-type-summarizer/tests/__tmp__/dist_dts/files/index.d.ts
debug loaded sourcemap for packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts
"debug loaded sourcemaps for [
'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/files/foo.d.ts',
'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/files/index.d.ts',
'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts'
]
debug Ignoring 5 global declarations for \\"Promise\\"
debug Ignoring 4 global declarations for \\"Symbol\\"
debug Ignoring 2 global declarations for \\"Component\\"

View file

@ -36,7 +36,7 @@ it('prints basic type alias', async () => {
}
`);
expect(output.logs).toMatchInlineSnapshot(`
"debug loaded sourcemap for packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts' ]
"
`);
});

View file

@ -62,7 +62,7 @@ it('prints basic variable exports with sourcemaps', async () => {
}
`);
expect(output.logs).toMatchInlineSnapshot(`
"debug loaded sourcemap for packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts' ]
"
`);
});