Issue with importing and exporting external types causing failures in Jest unit tests for Vue 2

I am in the process of creating a package that will contain various types, enums, consts, and interfaces that I frequently use across different projects.

To achieve this, I have set up a main.ts file where I have consolidated all the exports and specified it in the package.json:

"main": "main.ts"
.

One of the exports requires some types from mapbox-gl. However, I do not want to include the entire mapbox-gl library in my package. To address this, I took the following approach:

"peerDependencies": {
  "mapbox-gl": "^1.13.0"
},
"devDependencies": {
  "@types/mapbox-gl": "^1.13.0",
}

Within the main.ts file, I have included the following:

import mapboxgl from "mapbox-gl";

export interface DataSourceOptions {
  shouldLoad?: boolean;
  name: string;
  map: mapboxgl.Map;
  layer: mapboxgl.Layer;
}

After publishing my package, I imported it into my project and everything worked smoothly until I tried testing components using this package.

Jest started throwing an error stating:

D:\path\to\project\node_modules\some-custom-package\main.ts:1 ({"Object.":function(module,exports,require,__dirname,__filename,global,jest){import mapboxgl from "mapbox-gl"; SyntaxError: Cannot use import statement outside a module

In an attempt to resolve this issue, I added my package to the transformIgnorePatterns in the jest.config.js:

transformIgnorePatterns: [
  "<rootDir>/node_modules/(?!(@mapbox/mapbox-gl-draw" +
    "|some-custom-package" +
    ")/)",
],

However, the error persisted.

I also tried bundling my package with rollup, utilizing both tsc and rollup-plugin-typescript2 plugins as I know that in rollup I can specify externals to declare mapbox-gl accordingly. Unfortunately, neither tsc nor rollup-plugin-typescript2 seemed to recognize my interfaces, only the constants, types, and enums (possibly related).

While it may seem like multiple questions bundled into one, I am solely seeking out a solution.

  • Resolve Jest's importing problem (as it works fine within the app but fails during tests)
  • Find a way to retain interfaces while bundling the exports with rollup and declaring mapbox-gl as external

A potential workaround could involve avoiding the direct import of mapboxgl in my package by making the types used from it dynamic:

export interface DataSourceOptions<M,L> {
  shouldLoad?: boolean;
  name: string;
  map: M;
  layer: L;
}

This would allow me to specify in my project:

options: DataSourceOptions<mapboxgl.Map, mapboxgl.Layer>
. However, I am not particularly fond of this solution as it sidesteps the issue rather than solving it.

Answer №1

After some troubleshooting, I managed to resolve the issue by including:

"type": "module"

within the modules' package.json, and also adding it to transformIgnorePatterns in the jest.config.js file of the project that utilizes it, as depicted above.

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

Combining Angular 2 and Sails.js for Ultimate Web Development

Looking to integrate Sails with Angular 2 and add TypeScript to my project. As a newcomer in this field, I'm unsure how to configure this. I have created a Sails app using the command: sails new myApp Could anyone guide me on how to incorporate thi ...

Send the template to the enclosed grid column

I enclosed a kendo-grid-column within a new component by using <imx-gridColumn field="Count" title="Count"> ... </imx-gridColumn> The component that includes the imx-gridColumn is templated with <kendo-grid-column #column field="{{field}} ...

Pressing the Enter key will navigate to the next element in Vuejs2

I am utilizing a click function in Vue.js 2 with the following code: @change='units(chosed_item.id,$event)' Here is the code for the 'units' function, which sends the ID and stores the result in an array: units:function(item_id,$even ...

Sharing code between a node.js server and browser with Typescript: A step-by-step guide

I have an exciting project in mind to develop a multiplayer javascript game using a node.js server (with socket.io) and I am looking for a way to share code, specifically classes, between the web client and the server. Luckily, I came across this resource: ...

Next.js routes handlers do not have defined methods parameters

Struggling to find the cause of undefined params Currently delving into the world of Nextjs api routes, I keep encountering an issue where my params are coming up as undefined when trying to use them in the HTTP method. My setup includes prisma as my ORM ...

Create unique error messages using Vue validators

Two fields named text and email are included in my template. Below is the structure of the template: HTML <!DOCTYPE html> <html> <head> <title>Vue - Validations</title> <script src="Libraries/vue/vue.min.js&qu ...

Failed deployment of a Node.js and Express app with TypeScript on Vercel due to errors

I'm having trouble deploying a Nodejs, Express.js with Typescript app on Vercel. Every time I try, I get an error message saying "404: NOT_FOUND". My index.ts file is located inside my src folder. Can anyone guide me on the correct way to deploy this? ...

Guide on editing the index html file within a Vue CLI project

During the build process of Vue CLI, I am looking to make changes to the index.html file (vue.config.js). Is there a way to achieve this? This is what my current index.html looks like: <!DOCTYPE html> <html lang="en"> <head> < ...

Is it advisable to switch all properties to Angular Signals?

Recently, I've been utilizing signals to replace certain properties in my components that would typically require computed logic or be reactive to the effect hook. It got me thinking - should I be replacing all of my properties with signals, even if t ...

integrating material design components into Vue.js applications

My website is built on the Vue framework and webpack technology. I recently discovered Google's Material Components Web CSS framework, which can be easily implemented using a cdn or npm package for basic html/javascript websites. However, I am encoun ...

Leveraging an external Typescript function within Angular's HTML markup

I have a TypeScript utility class called myUtils.ts in the following format: export class MyUtils { static doSomething(input: string) { // perform some action } } To utilize this method in my component's HTML, I have imported the class into m ...

The child route's vue-router named view is failing to function accurately

The nested route with a named router view is not loading correctly. When clicking the links in the navigation drawer, the corresponding content should be displayed in the v-content component. I have implemented this using named router views. Below are the ...

How can I create an Array of objects that implement an interface? Here's an example: myData: Array<myInterface> = []; However, I encountered an issue where the error message "Property 'xxxxxx' does not exist on type 'myInterface[]'" appears

Currently, I am in the process of defining an interface for an array of objects. My goal is to set the initial value within the component as an empty array. Within a file, I have created the following interface: export interface myInterface{ "pictur ...

The function does not throw a compiler error when a parameter is missing

Currently utilizing TSC Version 2.4.2 Please take note of the following interface: interface CallbackWithNameParameter { cb: (name: string) => void } This code snippet: const aCallback: CallbackWithNameParameter = { cb: () => {} }; Manages t ...

The router-link feature in Vue.js is experiencing issues with functionality in the Firefox browser

Presenting the button component below: <button :class="classes" v-on="$listeners" v-bind="$attrs"> <template v-if="to"> <router-link :to="to" class="flex center-v"> <AqIcon :icon="icon" v-if="icon" /> ...

Organizing a mat-table by date does not properly arrange the rows

My API retrieves a list of records for me. I would like to display these records sorted by date, with the latest record appearing at the top. However, the TypeScript code I have written does not seem to be ordering my rows correctly. Can anyone assist me ...

Where is the best place to import styles in NextJS?

I have an existing project with a file importing the following function import theme from 'src/configs/theme' However, when I created a new Next.js project and replicated the same folder structure and imported the same function, it is showing "m ...

The meaning behind a textual representation as a specific type of data

This snippet is extracted from the file lib.es2015.symbol.wellknown.d.ts interface Promise<T> { readonly [Symbol.toStringTag]: "Promise"; } The concept of readonly seems clear, and the notation [Symbol.toStringTag] likely refers to "'toStr ...

Why is my Typescript event preventDefault function ineffective?

Despite all my efforts, I am still unable to prevent the following a tag from refreshing the page every time it's clicked. <p> <a onClick={(e) => handleClick} href="&qu ...

Filtering JSON data with Angular 4 input range feature

Hello there, I'm currently working on a search pipe in Angular 4 to filter company team sizes from JSON data using a range input between 0 and 100. However, I'm facing an issue with the range filter as I am relatively new to TypeScript and Angula ...