Do you believe this problem with transpilation has been properly reported to babel-jest?

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...

https://i.sstatic.net/4dFBi.png

Answer №1

Your code is creating a circular dependency issue by adding a new import statement in Button.tsx. This can be seen in the error stack trace provided:

  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)

The issue arises because when your test loads util.tsx, it imports components/index.ts and so on, eventually leading to Buttons.tsx importing logic.ts, which then again imports util.tsx. Since your code initially started with importing util.tsx, it has not finished executing yet as it's still loading all its dependencies. Consequently, when Button.tsx tries to import logic.ts, it calls sortEntries, which depends on

util.tsx</code being fully executed.</p>
<p>To resolve this, you should restructure your code to eliminate this cyclic dependency. It seems unusual for a file named <code>util
to import components, so segregating downloadPdf into its own file like downloadPdf.ts would be the recommended solution. This way, util.ts would no longer need to import components and break the cycle.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The class-transformer malfunctioning: The transformation function is not being executed

I am facing an issue with implementing class-transformer in my codebase, despite using type-graphql and @typegoose/typegoose libraries. Below is the snippet of my code: Custom Decorator import { Transform } from 'class-transformer'; export func ...

Tips on how to retrieve a stubbed Observable<void> in RxJS

Currently, I am attempting to stub an API and would like to retrieve a stubbed response from my Service. The method in my service appears as follows: public addItem(item): Observable<void> { this.listOfItems.push(item); return of(); } As f ...

Update the path dynamically in Next.js without the need to reload the page

Every time the user clicks on the continue button, a function is triggered. Within that function, I make the following call: push("/signup/page-2", undefined, { shallow: true }); All dynamic routes resembling /signup/[page].js that return <Component / ...

Is the nodejs pm2 server consuming excessive resources?

When I request a website in my browser, the server specifications are 2GB RAM and dual core Ubuntu based. I have hosted the website using pm2, but it seems like the node.js app is not performing well. Have you tried restarting or reinstalling it? I have ...

Typescript: Declaring object properties with interfaces

Looking for a solution to create the childTitle property in fooDetail interface by combining two properties from fooParent interface. export interface fooParent { appId: string, appName: string } export interface fooDetail { childTitle: fooParent. ...

Issue with importing Typescript and Jquery - $ function not recognized

Currently, I am utilizing TypeScript along with jQuery in my project, however, I keep encountering the following error: Uncaught TypeError: $ is not a function Has anyone come across this issue before? The process involves compiling TypeScript to ES20 ...

Bringing in TypeScript from external Node packages

I am looking to organize my application by splitting it into separate node modules, with a main module responsible for building all other modules. Additionally, I plan to use TypeScript with ES6 modules. Below is the project structure I have in mind: ma ...

Make the download window appear automatically when downloading a file

How can I use JavaScript/TypeScript to prompt the browser to open the download window? My goal is to give users the ability to rename the file and select the download folder, as most downloads are saved directly in the default location. This is how I curr ...

Ways to resolve issues related to null type checking in TypeScript

I am encountering an issue with a property that can be null in my code. Even though I check for the value not being null and being an array before adding a new value to it, the type checker still considers the value as potentially null. Can anyone shed lig ...

Unable to connect information to list item

I'm struggling to figure out why I am unable to bind this data into the li element. When I check the console, I can see the data under calendar.Days and within that are the individual day values. Any assistance would be highly appreciated. Code @Comp ...

Getting TypeScript errors when incorporating a variant into a Material-UI button using a custom component

I have created a unique Link component inspired by this particular example. Take a look at the code below: import classNames from 'classnames'; import {forwardRef} from 'react'; import MuiLink, {LinkProps as MuiLinkProps} from '@ma ...

Simultaneously iterate through two recursive arrays (each containing another array) using JavaScript

I have two sets of arrays composed of objects, each of which may contain another set of arrays. How can I efficiently iterate through both arrays and compare them? interface items { name:string; subItems:items[]; value:string; } Array A=['parent1&ap ...

Angular 2/4 - Saving User Object Information in the Front-End Instead of Repeatedly Contacting the Back-End Server

Is there a more efficient way to store and update the current user details in the frontend, without constantly making new HTTP GET requests to the backend every time a new component loads? The solution I came up with is a UserService class that handles se ...

Endpoint path for reverse matching in Mongodb API

I am currently in the process of setting up a webhook system that allows users to connect to any method on my express server by specifying a method and an endpoint to listen to (for example PUT /movies/*). This setup will then send the updated movie to the ...

Utilizing TypeScript for various return types with the same parameters

Exploring TypeScript Signatures In an effort to embrace TypeScript fully, I am implementing strongly typed signatures in my Components and Services, including custom validation functions for angular2 forms. I have discovered that while function overloadi ...

TypeScript overload does not take into account the second choice

Here is the method signature I am working with: class CustomClass<T> { sanitize (value: unknown): ReturnType<T> sanitize (value: unknown[]): ReturnType<T>[] sanitize (value: unknown | unknown[]): ReturnType<T> | ReturnType< ...

What could be causing my data to undergo alterations when transitioning from a frontend form submission to a backend API?

In my experience with Next.js 13 and Prisma, I encountered a peculiar issue. I had set up a basic form to collect user information for an api request. Oddly enough, when I printed the data right before sending it, everything seemed fine. However, upon arri ...

Invalid component prop provided to ButtonBase in Material UI. Ensure that the children prop is correctly rendered in this custom component

Forgive me for asking a basic question, as I am not the most proficient frontend developer and have searched extensively online. Whenever I inspect my frontend application in Chrome, I keep encountering this error. (3) Material-UI: The component prop pro ...

When running `aws-cdk yarn synth -o /tmp/artifacts`, an error is thrown stating "ENOENT: no such file or directory, open '/tmp/artifacts/manifest.json'"

Starting a new aws-cdk project with the structure outlined below src └── cdk ├── config ├── index.ts ├── pipeline.ts └── stacks node_modules cdk.json package.json The package.json file looks like this: " ...

Angular 2 orderByPipe not displaying any results initially

At first, I thought the reason for not displaying anything initially was due to it not being async and returning an empty array. Everything worked fine without the pipe, but the posts weren't shown on startup. After submitting a post, all the posts ap ...