I recently encountered a problem in the jest project regarding babel-jest transpilation. I added some code that appeared to be error-free, but caused transpilation to fail completely.
Although the issue seemed related to a Typescript Next project, there are multiple layers of transpilation involved, leaving me unsure if this is where the problem lies.
Should this issue be directed towards the babel-jest project, or is there a more suitable place to file it?
BUG
In a rather complex Next Typescript project, I discovered that adding just a few lines of code to one file disrupted the babel-jest transpilation process and resulted in unexpected runtime errors in an entirely different file.
After reverting to commit 04c4c7b and running yarn run test
, the tests passed successfully. You can view the successful source tree at https://github.com/cefn/jest-transpile-failure-repro/tree/04c4c7b7013e8b88c25d3ab2a7d4a33ccd3fb191
However, upon adding the aforementioned lines (found in commit 25703fc), the babel-jest transpiler malfunctioned, rendering the tests unexecutable, and triggering unrelated runtime errors such as...
ReferenceError: Cannot access 'SCORERS' before initialization
47 | sortedEntries.sort((a: Immutable<Entry>, b: Immutable<Entry>) => {
48 | for (const scoreName of scorePriority) {
> 49 | const scorer = SCORERS[scoreName]
| ^
50 | const diff = scorer(b) - scorer(a)
51 | if (diff !== 0) {
52 | return diff
at sort (src/util.tsx:49:22)
at Array.sort (<anonymous>)
at sortEntries (src/util.tsx:47:17)
at Object.<anonymous> (src/logic.ts:10:20)
at Object.<anonymous> (src/components/controls/Buttons.tsx:6:1)
at Object.<anonymous> (src/components/Controls.tsx:8:1)
at Object.<anonymous> (src/components/index.ts:1:1)
at Object.<anonymous> (src/util.tsx:6:1)
at Object.<anonymous> (test/util.test.ts:3:1)
Note that commit 25703fc compiles successfully with tsc
and runs without issues.
The occurrence of this error is puzzling considering that 'SCORERS' is a const defined in the same module closure as the function itself.
Moreover, the affected file was not even modified in commit 25703fc, suggesting that the transpiler encountered an error due to changes in the language constructs.
Since the impacted file functions correctly in production, I do not believe there are any TypeScript errors present.
Should this issue be addressed with babel, nextjs, or is it better suited for the babel-jest project, or perhaps another platform?
If anyone has a workaround, like using a different babel system for Transpiling TypeScript Next.js code, feel free to suggest one until I establish whether babel-jest is responsible for the issue.
To demonstrate, you can see the compiled and functional code despite the babel-jest error at
The image below displays the exact lines that led to the transpilation error, captured from the GitHub commit diff...