SharePointVault ~/gotchas/spfx ⌕ Search 459 pages, error messages, scripts…Ctrl K ★ Star
M↓ README.mdM↓ source-guard-test-reads-tsx-from-lib-commonjs.md
sharepoint-vault›gotchas›spfx›source-guard-test-reads-tsx-from-lib-commonjs.mdhistoryedit on GitHub
● GOTCHAspfx· 1 min read

A Jest test that reads a .tsx source file passes locally and fails in the SPFx production build

applies to: SPFx projects on the heft toolchain (1.22+, `heft test --clean --production`)reviewed: 2026-11-17#spfx#heft#jest#testing#build

In an SPFx project, heft runs Jest over the compiled output in lib-commonjs, not over src. Only .js files land there. A "source guard" test that does fs.readFileSync(path.join(__dirname, 'MyComponent.tsx')) therefore throws ENOENT in the release build. Map the directory back to src first: __dirname.replace(/([\\/])lib-commonjs([\\/])/, '$1src$2').

Heft v SPFx spouští Jest nad PŘELOŽENÝM výstupem v lib-commonjs, ne nad src, a tam jsou jen .js soubory. Test, který čte zdrojový .tsx přes __dirname, v produkčním buildu spadne na ENOENT. Cestu nejdřív převeďte zpět na src: __dirname.replace(/([\\/])lib-commonjs([\\/])/, '$1src$2').

Symptom

You add a small guard test that reads a component's source and asserts it calls a shared helper. The check catches the case where someone later swaps the helper for a hand-rolled copy:

const src = fs.readFileSync(path.join(__dirname, 'DocPreview.tsx'), 'utf8');
expect(src).toMatch(/return urlPathExt\(url\);/);

The functional assertions in the same file pass. The release build (heft test --clean --production) stops with:

ENOENT: no such file or directory, open '…\lib-commonjs\webparts\…\DocPreview.tsx'

Cause

Heft compiles TypeScript first, then runs Jest over lib-commonjs/**/*.test.js. At run time __dirname points inside lib-commonjs, which holds .js files only. The .tsx exists solely under src. A runner that loads the .ts test straight from src (for example a ts-jest setup) would resolve __dirname to src and hide the problem.

Fix

Resolve the source directory explicitly:

// Tests run from compiled lib-commonjs; the .tsx source only exists in src.
const SRC_DIR = __dirname.replace(/([\\/])lib-commonjs([\\/])/, '$1src$2');
const src = fs.readFileSync(path.join(SRC_DIR, 'DocPreview.tsx'), 'utf8');

The replacement is a no-op when the test already runs from src, so the same code works in both runners. An alternative is to walk up from __dirname until a src/<relative path> exists.

Validate any new test that reads files through the project's own test command (npm test → heft), never only through a standalone jest run.

Was this helpful?ask or add your experience on GitHub →
Saved you an hour? A star on GitHub helps other SharePoint people find the vault.★ Star on GitHub
last reviewed: 2026-11-17last updated: 2026-11-17historyedit on GitHubview source