Problem with exporting default class in Babel/Jest conflict

Currently, I am facing a challenge while testing the code using jest. The issue seems to be related to babel, especially regarding the export of a default class. Here is an example of the code causing the problem...

export default class Test {
  get() {
    return {}
  }
}

The test setup looks like this...

import Test from './test'

describe('test', () => {
  it('should', () => {
    // [...]
  });
});

However, when running the test, I encounter the following error...

node_modules/@babel/runtime-corejs2/helpers/esm/classCallCheck.js:1 ({"Object.":function(module,exports,require,__dirname,__filename,global,jest){export default function _classCallCheck(instance, Constructor) { ^^^^^^

SyntaxError: Unexpected token export

This project is a vue web app with the following configuration...

// babel.config.js
module.exports = {
  presets: ['@vue/app', '@babel/env']
};
// jest.config.js
module.exports = {
  collectCoverage: true,
  collectCoverageFrom: [
    'src/**'
  ],
  coverageDirectory: '.coverage',
  moduleFileExtensions: [
    'js',
    'json',
    'vue'
  ],
  transform: {
    '^.+\\.vue$': 'vue-jest',
    '.+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
    '^.+\\.js$': 'babel-jest'
  },
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1'
  },
  testMatch: [
    '<rootDir>/src/**/*.spec.js'
  ],
  transformIgnorePatterns: ['<rootDir>/node_modules/']
};

Lastly, here is my test script in the package.json file...

// package.json
[...]
"test": "jest"
[...]

I am struggling to find a solution to this problem, especially since all my .vue files and tests are working correctly. The issue arises only with specific .js files that use the mentioned syntax. Any suggestions on how to resolve this?

Your input would be greatly appreciated.

Answer №1

After some investigation, I made the decision to remove @vue/app as a preset from my configuration. Surprisingly, this change did not affect my Vue testing at all, as everything continued to function as expected with @babel/env. The resulting configuration in my `babel.config.js` file looks like this...

// babel.config.js
module.exports = {
  presets: ['@babel/env']
};

It may seem underwhelming, but this simple adjustment resolved the issue without the need for further exploration into what potential impact @vue/app was having on my setup.

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 TypeScript compiler generates a blank JavaScript file within the WebStorm IDE

My introduction to TypeScript was an interesting experience. I decided to convert a simple JavaScript application, consisting of two files, into TypeScript. The first file, accounts.ts, contains the main code, while the second one, fiat.ts, is a support f ...

Implement jQuery to dynamically assign an "Active" class to tab elements based on the current page being loaded

INQUIRIES I have include: How do I apply a class to an element using jQuery, or any other method, for the active tab? Ensure that the dropdown tab appearing is the one containing the active tab, not always the Company one. In essence, I want the ac ...

Is there a way to ensure that a "catch all other" route in the Vue Router will also capture the URL if a portion of it matches a predefined route?

After following the suggestion to implement a catch all route from this article, I realized that it does not capture URLs that partially match a defined route. routes: [ { path: "/album/:album", name: "album", component: Album, } ...

Using Nuxt.js with Vagrant and Homestead for seamless port forwarding

I am encountering an issue where I can't seem to connect to my Nuxt.js application OUTSIDE of the vagrant box (i.e., on my host or local machine), although it is able to fetch content INSIDE the vagrant box. Here's what I'm doing: I'v ...

Next.js app experiencing issues with Chakra UI not transitioning to dark mode

After attempting to incorporate Chakra UI into my Next.js application, I carefully followed every step outlined in their documentation: Despite setting the initialColorMode to "dark" for the ColorModeScript prop, it seems that the dark mode is not being a ...

Transform the data format received from the AJAX request - modify the input value

I have a data variable that contains an object structured as follows: { "details": { "id": 10, "name": John Doe, "hobbies": [{ "id": 1, "name": "Football" }, { "id": 2, "name": "Badminton" }, ...

Utilizing RxJS finalize in Angular to control the frequency of user clicks

Can someone provide guidance on using rxjs finalized in angular to prevent users from clicking the save button multiple times and sending multiple requests? When a button click triggers a call in our form, some users still tend to double-click, leading to ...

How can I transform area, city, state, and country into latitude and longitude using Google Maps API v3?

Is there a way to retrieve the latitude and longitude for a string that includes area name, city name, state name, and country name using Google Maps API V3? ...

Is there a way to disable automatic spacing in VS code for a React file?

I am currently working on my code in VS Code within my JSX file, but I keep encountering an error. The issue seems to be that the closing tag < /h1> is not being recognized. I have attempted multiple methods to prevent this automatic spacing, but so ...

Require checkboxes in AngularJS forms

Currently, I have a form that requires users to provide answers by selecting checkboxes. There are multiple checkboxes available, and AngularJS is being utilized for validation purposes. One essential validation rule is to ensure that all required fields a ...

Continue iterating only when all promises have been resolved

My AngularJS requirement involves the following: for (var i = 0, len = self.Scope.data.length; i < len; i++) { var data = self.Scope.data[i]; var self = this; //Executing First asynchronous function self.EcritureService.createNewDa ...

What is the best way for me to incorporate images retrieved from an API call into my

Hey everyone, this is my first time posting on here so if there's anything I'm missing, please let me know! I've run into an issue with the images on my categories page not aligning properly and being different sizes after I incorporated som ...

Can TypeScript automatically deduce keys from a changing object structure?

My goal here is to implement intellisense/autocomplete for an object created from an array, similar to an Action Creator for Redux. The array consists of strings (string[]) that can be transformed into an object with a specific shape { [string]: string }. ...

"Upon subscribing, the object fails to appear on the screen

Why is the subscription object not displaying? Did I make a mistake? this.service.submitGbtForm(formValue) .subscribe((status) => { let a = status; // a = {submitGbtFrom: 'success'} console.log(a, 'SINGLE ...

Running a code from a plugin in Wordpress site

I am currently utilizing the "wp-video-lightbox" plugin for WordPress, which generates small floating boxes for my videos. I am interested in incorporating variables like http://www.example.com/?video3 to provide shortcuts similar to what YouTube offers. ...

Is it possible to utilize the "let" keyword in JavaScript as a way to prevent global-scope variables?

While working on a JavaScript test, I came across an interesting observation related to the let keyword. Take a look at this code snippet: let variable = "I am a variable"; console.log(window.variable); Surprisingly, when I tried to access the variable p ...

Using JQuery, a button is programmed to take a URL and then proceed to submit

My application is designed to shorten URLs for users who are authenticated. The form only requires the full URL input, and it will then generate a shortened version of the link. I am interested in creating a button that can be embedded on pages with long, ...

Exploring Node.js and JSON: Retrieving specific object attributes

Utilizing ExpressJS, NodeJS, and Bookshelf.js In an attempt to display a user's list of friends, encountering the error "Unhandled rejection TypeError: Cannot read property 'friends' of undefined" when trying to access a property of the obj ...

It's next to impossible to secure expedited work on an ongoing project using Vercel

Yesterday, I successfully deployed an application on Vercel using only ReactJS. Today, I made the decision to develop an API for my application, To clarify, I have a folder housing the React app, and within that, I created a directory named "api" followi ...

"Enhance Your Text Fields with Angular2 Text Masks for Added Text Formatting

Is there a way to combine text and numbers in my mask? This is what I am currently using: [/\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/] The above code only allows for numbers. How can I modify it to allow f ...