Skip to content

Commit 942e9bc

Browse files
committedMay 16, 2024
fix: V8 coverage support
closes #433
1 parent 05c382f commit 942e9bc

File tree

4 files changed

+72
-26
lines changed

4 files changed

+72
-26
lines changed
 

‎src/utils/transform/get-esbuild-options.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ export const cacheConfig = {
1616
sourcemap: true,
1717

1818
/**
19-
* Improve performance by generating smaller source maps
20-
* that doesn't include the original source code
19+
* Improve performance by only generating sourcesContent
20+
* when V8 coverage is enabled
2121
*
2222
* https://esbuild.github.io/api/#sources-content
2323
*/
24-
sourcesContent: false,
24+
sourcesContent: Boolean(process.env.NODE_V8_COVERAGE),
2525

2626
/**
2727
* Smaller output for cache and marginal performance improvement:

‎tests/specs/smoke.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { testSuite, expect } from 'manten';
44
import { createFixture } from 'fs-fixture';
55
import outdent from 'outdent';
66
import type { NodeApis } from '../utils/tsx.js';
7+
import { hasCoverageSourcesContent } from '../utils/coverage-sources-content.js';
78

89
const cjsContextCheck = 'typeof module !== \'undefined\'';
910
const tsCheck = '1 as number';
@@ -352,7 +353,6 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
352353
describe(packageType, ({ test, describe }) => {
353354
test('from .js', async ({ onTestFinish, onTestFail }) => {
354355
const fixture = await createFixture({
355-
...files,
356356
'package.json': JSON.stringify({ type: packageType }),
357357
'import-from-js.js': outdent`
358358
import assert from 'assert';
@@ -459,7 +459,8 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
459459
// Could .js import TS files?
460460
461461
// Comment at EOF: could be a sourcemap declaration. Edge case for inserting functions here
462-
`.trim(),
462+
`,
463+
...files,
463464
});
464465
onTestFinish(async () => await fixture.rm());
465466

@@ -488,7 +489,6 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
488489

489490
describe('from .ts', async ({ test, onFinish }) => {
490491
const fixture = await createFixture({
491-
...files,
492492
'package.json': JSON.stringify({ type: packageType }),
493493

494494
'import-from-ts.ts': ({ fixturePath }) => outdent`
@@ -652,12 +652,18 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
652652
}));
653653
654654
// Comment at EOF: could be a sourcemap declaration. Edge case for inserting functions here
655-
`.trim(),
655+
`,
656+
...files,
656657
});
657658
onFinish(async () => await fixture.rm());
658659

659660
test('import all', async ({ onTestFail }) => {
660-
const p = await tsx(['import-from-ts.ts'], fixture.path);
661+
const p = await tsx(['import-from-ts.ts'], {
662+
cwd: fixture.path,
663+
env: {
664+
NODE_V8_COVERAGE: 'coverage',
665+
},
666+
});
661667
onTestFail((error) => {
662668
console.error(error);
663669
console.log(p);
@@ -678,6 +684,10 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
678684
// By "require()"ing an ESM file, it forces it to be compiled in a CJS context
679685
expect(p.stdout).toMatch(`"mjs":{"mjsHasCjsContext":${isCommonJs}}`);
680686
expect(p.stderr).toBe('');
687+
688+
const coverageDirectory = fixture.getPath('coverage');
689+
const coverageSourceMapCache = await hasCoverageSourcesContent(coverageDirectory);
690+
expect(coverageSourceMapCache).toBe(true);
681691
});
682692

683693
test('tsconfig', async ({ onTestFail }) => {
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import path from 'node:path';
2+
import fs from 'node:fs/promises';
3+
4+
type SourceMapCache = Record<string, {
5+
data: {
6+
sourcesContent: string[];
7+
} | null;
8+
}>;
9+
10+
export const hasCoverageSourcesContent = async (
11+
coverageDirectory: string,
12+
) => {
13+
const fileNames = await fs.readdir(coverageDirectory);
14+
const coverageData = await Promise.all(
15+
fileNames.map(
16+
async file => JSON.parse(
17+
await fs.readFile(path.join(coverageDirectory, file), 'utf8'),
18+
),
19+
),
20+
);
21+
return coverageData.some(coverage => (
22+
coverage['source-map-cache']
23+
&& (
24+
Object.values(coverage['source-map-cache'] as SourceMapCache)
25+
.some(value => typeof value.data?.sourcesContent?.[0] === 'string')
26+
)
27+
));
28+
};

‎tests/utils/tsx.ts

+26-18
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,33 @@ export const createNode = async (
6767
tsx: (
6868
args: string[],
6969
cwdOrOptions?: string | NodeOptions,
70-
) => execaNode(
71-
tsxPath,
72-
args,
73-
{
74-
env: {
75-
TSX_DISABLE_CACHE: '1',
76-
DEBUG: '1',
70+
) => {
71+
const isCwd = typeof cwdOrOptions === 'string';
72+
return execaNode(
73+
tsxPath,
74+
args,
75+
{
76+
nodePath: node.path,
77+
nodeOptions: [],
78+
reject: false,
79+
all: true,
80+
...(
81+
isCwd
82+
? { cwd: cwdOrOptions }
83+
: cwdOrOptions
84+
),
85+
env: {
86+
TSX_DISABLE_CACHE: '1',
87+
DEBUG: '1',
88+
...(
89+
(cwdOrOptions && !isCwd)
90+
? cwdOrOptions.env
91+
: {}
92+
),
93+
},
7794
},
78-
nodePath: node.path,
79-
nodeOptions: [],
80-
reject: false,
81-
all: true,
82-
...(
83-
typeof cwdOrOptions === 'string'
84-
? { cwd: cwdOrOptions }
85-
: cwdOrOptions
86-
),
87-
},
88-
),
95+
);
96+
},
8997

9098
cjsPatched: (
9199
args: string[],

0 commit comments

Comments
 (0)
Please sign in to comment.