It appears that Jest is having trouble comprehending the concept of "import type"

We've just completed a major update to our monorepository, bringing it up to date with the following versions:

  • Nx 14.3.6
  • Angular 14.0.3
  • Jest 28.1.1
  • TypeScript 4.7.4

Although the compilation process went smoothly after the upgrade, we encountered numerous runtime errors such as "emitDecoratorMetadata causes runtime errors by referencing type-only imports with namespaces" (https://github.com/microsoft/TypeScript/issues/42624). This issue was also flagged by ESLint.

To address this, we had to replace all "import" statements with "import type" for types and interfaces across the board. This resolved the runtime errors and restored the functionality of the application. Additionally, to resolve the ESLint error, we installed and utilized the "eslint-plugin-import" extension.

However, this fix led to a new problem - our tests stopped functioning properly. It appears that Jest does not recognize the "import type" statement. In every unit test for a class utilizing "import type", the tests failed with the error:

ReferenceError: Zyz is not defined

(where xyz represents an imported type in the tested class, e.g.

// some-component.ts
import type { Xyz } from '...';
...

If we remove the "type" keyword from the "import type" statement, the tests run successfully but the earlier runtime errors resurface.

I have conducted thorough research (primarily focusing on babel usage/reconfiguration) based on findings from posts like this one: https://github.com/babel/babel/issues/10981, yet I am still grappling with solving this issue.

Answer №1

Looks like the issue is resolved by following the workaround provided here: https://github.com/thymikee/jest-preset-angular/issues/1199#issuecomment-1168802943

To fix this in tsconfig.spec.json, update the "include" property to include all typescript files:

// tsconfig.spec.json
{
  ...
  "include": ["**/*.ts"]
}

Prior configuration was:

// tsconfig.spec.json
{
  ...
  "include": ["**/*.spec.ts", "**/*.test.ts", "**/*.d.ts", "jest.config.ts"]
}

Edit: Besides the above adjustments, it appears that ts-jest also needs non-isolatedModules. We had set isolatedModules: true before, which led to issues with type-only imports.

If anyone has insights on why or a better solution, please share your thoughts.

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

Using Typescript/JSX to assign a class instance by reference

Looking to access an object's property by reference? See the code snippet below; class Point{ x:number; y:number; constructor(x,y) { this.x=x; this.y=y; } } const a = { first: new Point(8,9), second: new Point(10,12) }; let someBoo ...

The primary filter for ag-Grid in Angular 2+

When using the default filtering on ag-grid, I find that the timing between typing and filtering is too quick. Is there a method to slow down this process? Perhaps instead of triggering the filter when typing stops, it could be activated by pressing a bu ...

After installing TypeScript community stubs, WebStorm is unable to locate the module

Recently, I tried to incorporate the ramda library into my project and went ahead to install its TypeScript community stubs in WebStorm. https://i.stack.imgur.com/fCFG8.png Despite my efforts, I encountered an error that stated — Cannot find module &a ...

Tips on updating checkbox background color after being selected

I've been working on creating checkboxes for seat selection in Angular using a TypeScript file, but I'm facing an issue where the background color of the checkbox doesn't change after checking them. Here's my TypeScript function: gener ...

Warning: The gulp_jspm module is now deprecated and will be removed

Whenever I run gulp_jspm, a DeprecationWarning pops up. Is there an alternative method to generate my bundle files without encountering this warning? It seems like when I used gulp-jspm-build, I had to include some node files that were not necessary before ...

Excluding node modules when not included in tsconfig

Within my Angular project, there is a single tsconfig file that stands alone without extending any other tsconfigs or including any additional properties. Towards the end of the file, we have the following snippet: "angularCompilerOptions": { ...

Obtain the last used row in column A of an Excel sheet using Javascript Excel API by mimicking the VBA function `Range("A104857

Currently in the process of converting a few VBA macros to Office Script and stumbled upon an interesting trick: lastRow_in_t = Worksheets("in_t").Range("A1048576").End(xlUp).Row How would one begin translating this line of code into Typescript/Office Scr ...

How can I modify the pristine state of NgModel in Angular 2 using code?

Whenever you update the NgModel field, it will automatically set model.pristine to true. Submitting the form does not change the "pristine" status, which is expected behavior and not a bug. In my situation, I need to display validation errors when the fo ...

When a user clicks on the download link, it redirects them to the homepage in Angular

When using Angular 6 and the downloadFile method to download an Excel sheet from the WebAPI, everything runs smoothly. A dialog box opens up asking to save the file on the drive, but then it unexpectedly navigates me back to the home page. This redirects ...

Using an array of functions in Typescript: A simple guide

The code below shows that onResizeWindowHandles is currently of type any, but it should be an array of functions: export default class PageLayoutManager { private $Window: JQuery<Window>; private onResizeWindowHandlers: any; constructor () { ...

Type inference and the extends clause in TypeScript 4.6 for conditional types

My focus was on TypeScript 4.7 when I created the following types: const routes = { foo: '/foo/:paramFoo', bar: '/bar/:paramFoo/:paramBar', baz: '/baz/baz2/:paramFoo/:paramBar', } as const; type Routes = typeof routes; ...

Is it possible for an object to receive notifications when a component object undergoes changes in Angular 2/4?

When working with Angular components, it's possible to pass a variable or object as @Input(). The component will be notified whenever the value of this input changes, which is pretty cool... I'm currently developing an application that features ...

What steps should be followed to implement ng-zorro-antd?

I'm currently in the process of developing an Angular project with ng-zorro. I've followed these steps: npm install --location=global @angular/cli ng new ng-zorro-demo This Angular project includes routing. cd ng-zorro-demo/ ng add ng-zorro-antd ...

Introduction to React with Typescript: Greeting the World

I am attempting to create a Hello World React application with Typescript. Below is the code I have written. Method 1 works without any issues, but method 2 throws an error. Method 1 - TypeScriptComponent.tsx import React from 'react' import Re ...

Can TypeScript modules be designed to function in this way?

Seeking to create a versatile function / module / class that can be called in various ways: const myvar = MyModule('a parameter').methodA().methodB().methodC(); //and also this option should work const myvar = MyModule('a parameter') ...

There is no index signature that accepts a parameter of type 'string' in the type '{ [key: string]: AbstractControl; }'

I'm currently tackling a challenge in my Angular project where I am creating a custom validator for a reactive form. However, I've encountered an error within the custom validators function that I am constructing. Below you will find the relevan ...

Tips for preventing repetition in http subscribe blocks across various components

Imagine a scenario where there is a service used for making HTTP request calls. There are two different components (which could be more than two) that need to send the same request using the same observables via this service. After receiving the result, it ...

The html-duration-picker is not being displayed in the proper format

I've been working on integrating an external library that allows for inputting document length. Specifically, I'm using the html-duration-picker library, but it seems like the input functionality is not quite right for durations. Could it be th ...

The Angular Material Stepper seems to have a glitch where calling the next() function does not work until I manually click

Currently, I am in the process of developing an Electron app using Angular 7 and Angular Material. Within my application, I have implemented a Stepper component where the second step involves making a call to the Electron main process to prompt the user t ...

Is there a way to display the number of search results in the CodeMirror editor?

After conducting some research on how to integrate the search result count in Codemirror like the provided image, I was unable to find any solutions. I am currently working with Angular and utilizing ngx-codemirror, which led me to realize that editing the ...