Commit 596cd1f
committed
test: cover __dirname, __filename, & require.cache in CJS TS file
## Problem
Two closed bugs left the CJS-scope-globals contract untested:
- [#694](#694) — Node 23.6+
began classifying `.ts` entrypoints as ESM in `type: "commonjs"`
packages, breaking `__dirname` and `__filename` with `ReferenceError:
__dirname is not defined in ES module scope`. Implicit fix landed in the
4.20/4.21 era; no paired regression test.
- [#726](#726) —
`require.cache` returned `undefined` in v4.20 after the loader refactor;
the maintainer reverted in v4.20.3 and explicitly noted it should land
as a test for the next release.
A future refactor of the CJS-classification path could silently
reintroduce either bug.
## Changes
One new `commonjs-mode-contracts.ts` test, iterated through both
`omitted type` and `explicit commonjs` package shapes (two process
spawns total), asserts that a `.ts` entrypoint sees:
- `__dirname` → fixture path
- `__filename` → fixture entry path
- `require.cache` → populated object after a sibling
`require('./dep.cjs')`1 parent 75d9bf0 commit 596cd1f
1 file changed
Lines changed: 46 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
148 | 149 | | |
149 | 150 | | |
150 | 151 | | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
151 | 197 | | |
152 | 198 | | |
153 | 199 | | |
| |||
0 commit comments