Skip to content

Commit 2da3c8e

Browse files
committed
feat(coverage): add TypeScript source map generation for accurate coverage
- Import envAsBoolean from @socketsecurity/lib/env/helpers - Enable source maps conditionally via COVERAGE env var - Add rebuild step in cover.mjs with COVERAGE=true - Matches socket-lib pattern for consistent behavior When COVERAGE=true or COVERAGE=1: - esbuild generates TypeScript source maps - Coverage tools can map JS back to TS sources - Improves coverage accuracy
1 parent 5491499 commit 2da3c8e

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

.config/esbuild.config.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { parse } from '@babel/parser'
1111
import MagicString from 'magic-string'
1212

1313
import { NODE_MODULES } from '@socketsecurity/lib/constants/paths'
14+
import { envAsBoolean } from '@socketsecurity/lib/env/helpers'
1415
import { getDefaultLogger } from '@socketsecurity/lib/logger'
1516

1617
const __dirname = path.dirname(fileURLToPath(import.meta.url))
@@ -210,7 +211,8 @@ export const buildConfig = {
210211
platform: 'node',
211212
// Target Node.js 18+ features.
212213
target: 'node18',
213-
sourcemap: false,
214+
// Enable source maps for coverage (set COVERAGE=true env var)
215+
sourcemap: envAsBoolean(process.env.COVERAGE),
214216
minify: false,
215217
treeShaking: true,
216218
// For bundle analysis

scripts/cover.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { fileURLToPath } from 'node:url'
1414

1515
import { parseArgs } from '@socketsecurity/lib/argv/parse'
1616
import { getDefaultLogger } from '@socketsecurity/lib/logger'
17+
import { spawn } from '@socketsecurity/lib/spawn'
1718
import { printHeader } from '@socketsecurity/lib/stdio/header'
1819

1920
import { runCommandQuiet } from './utils/run-command.mjs'
@@ -37,6 +38,23 @@ const { values } = parseArgs({
3738
printHeader('Test Coverage')
3839
logger.log('')
3940

41+
// Rebuild with source maps enabled for coverage
42+
logger.info('Building with source maps for coverage...')
43+
const buildResult = await spawn('node', ['scripts/build.mjs'], {
44+
cwd: rootPath,
45+
stdio: 'inherit',
46+
env: {
47+
...process.env,
48+
COVERAGE: 'true',
49+
},
50+
})
51+
if (buildResult.code !== 0) {
52+
logger.error('Build with source maps failed')
53+
process.exitCode = 1
54+
process.exit(1)
55+
}
56+
logger.log('')
57+
4058
// Run vitest with coverage enabled, capturing output
4159
// Filter out custom flags that vitest doesn't understand
4260
const customFlags = ['--code-only', '--type-only', '--summary']

0 commit comments

Comments
 (0)