Webpack's code splitting feature causes issues with importing Jest when dealing with VueJs components

I am encountering an issue with Jest when attempting to load a VueJs component that includes dynamic import code.

Component:

<script>
    const Modal = () => import(/* webpackChunkName: "Modal" */ "../pages/common/Modal.vue");

    export default {
        name: "TileEditModal",
        components: {
            Modal
        },
        data() {
            return 
        },
        methods: {
            test() {
                return true;
            }
        }
    }
</script>

Test:

import TileEditModal from "./TileEditModal.vue"

Even without running any tests, simply importing that component triggers the following error:

  return import( /* webpackChunkName: "Modal" */"../pages/common/Modal.vue");
           ^^^^^^
SyntaxError: Unexpected token import

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
  at Object.<anonymous> (srcVue/pages/landing/TileEditModal.vue.test.js:3:22)

I have attempted to implement this solution, but it has not resolved the issue for me.

I am using jest-vue-preprocessor with jest:

  "jest": {
    "moduleFileExtensions": [
      "js",
      "vue"
    ],
    "transform": {
      "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
      ".*\\.(vue)$": "<rootDir>/node_modules/jest-vue-preprocessor"
    },
    "clearMocks": true,
    "resetMocks": true,
    "resetModules": true
  },

I have also tried adding the env.test preset to babel:

{
    "presets": [
      "es2015",
      "stage-2",
      "stage-0"
    ],
    "env": {
      "test": {
        "presets": [
          "es2015",
          "stage-2",
          "stage-0"
        ]
      }
    } 
  }

Unfortunately, none of these solutions have worked so far. Any suggestions on how I can make code splitting work for individual components?

Answer №1

In order to solve my issue, I found success by utilizing the dynamic import babel plugin and also executing jest without using cache.

jest --no-cache

Unfortunately, when attempting to run it with cache again afterward, the test failed. It's a bit puzzling, but everything works fine as long as I include --no-cache. The only downside is that it makes the tests slower.

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

Arranging Images, Buttons, and Text Vertically in React with CSS

https://i.sstatic.net/0WaeI.png In my current application, whenever the button is clicked, a random image is generated but it results in everything appearing bunched up. I would like the images, which are of different sizes, to be displayed with the dog&a ...

What is the process for exporting Three.js files to .stl format for use in 3D printing?

I stumbled upon a page with this link: Is it possible to convert Three.js to .stl for 3D printing? var exporter = new THREE.STLExporter(); var str = exporter.parse(scene); console.log(str); However, despite using the code provided, I am unable to export ...

I need the language chosen using Lingui's Language Switcher (i18n) to persist throughout the app, even after a refresh

I have been experimenting with Lingui for internationalization (i18n) in my app. I followed the documentation and tutorial to create a language switcher, but I am unsure how to set it up so that the selected language persists throughout the app even after ...

AngularJS login form with JSON data

I am currently learning Angular and focusing on the login form implementation. The specific model I am working with can be found in this PLNKR. There are two challenges that I am struggling to resolve. Issue 1: I'm trying to figure out how to tur ...

When the wireframe property is set to true, the react-three-renderer will only render geometry using the <meshBasicMaterial>

I am new to using the three.js library along with react-three-renderer. I have encountered a problem where only geometry renders when the wireframe property is set to true. If the wireframe property is false (which is the default setting), I expect the mes ...

Hide Details tag only on mobile view

How can I easily close the default opened <details> tag on mobile? I am working with Wordpress. Any suggestions on how to achieve this? <details open> <summary>Details</summary> Something small enough to go unnoticed. </ ...

Implement the map function to validate every input and generate a border around inputs that are deemed invalid or empty upon button click

Currently, I'm in the process of developing a form with multiple steps. At each step, when the next button is clicked, I need to validate the active step page using the map function. My goal is to utilize the map function to validate every input and ...

Check the length of a ngRepeat array after the initial filtering before applying limitTo for further refinement

Currently, I am implementing pagination in my Angular application by using a custom startAtIndex filter along with the limitTo filter. My goal is to show the total number of results in the dataset, regardless of the current page. However, due to the use of ...

Assigning a default value to a select dropdown using objects in Angular

I am in need of assistance. I am struggling to convert $scope.headline.category into an object; it must remain a string. The groupList needs to be a list of objects. Is there a way to set the initial value of the dropdown to the 2nd item (1 index) without ...

Setting up Vue.js Okta Authentication app for deployment on Netlify

Recently, I completed the development of a Vue.js application with Okta authentication. Now, I am in the process of deploying this application on Netlify. Upon creating a new project on Netlify, I brought the Vue.js application into the project from GitHub ...

How can I retrieve the value of a hidden field using Jquery?

Here is a snippet of HTML code that I am working with: <input type="hidden" name="conf1" value="7th IEEE/IFIP International Conference on Embedded and Ubiquitous Computing (EUC-09)"> <input type="hidden" name="conf2" value="IEEE International ...

What is the best way to figure out the resolution of a computer screen using javascript?

It came to my attention that the <canvas> element can be adjusted to have varying scales. For example, if I specify the CSS width and height as 100px but use JavaScript to set them to 200px, the element will shrink so that everything drawn on it appe ...

Detecting when the page is done loading in CasperJS with the help of $.ajaxStop()

During my CasperJS tests, I've relied on waitForSelector() to check if a page has finished loading, including all asynchronous AJAX requests. However, I'm interested in finding a more standard approach for waiting for page load. Is it possible to ...

Utilizing GraphQL Global Object Identification with NestJS using the code-first strategy

Currently, I am trying to incorporate Global Object Identification as outlined in the GraphQL documentation into my NestJS project. 1.) First, I created a Node interface: import { ID, InterfaceType, Field } from '@nestjs/graphql' @InterfaceType ...

Parsing error: Unforeseen token encountered. Consider adding a supplementary loader to manage the output of these loaders

Could someone please break down this syntax message?.length === 1 and show me how to convert it into standard JavaScript? https://i.stack.imgur.com/20Ui6.png I am encountering an error when trying to use a Vue.js component that I downloaded from another ...

Retrieving information from subscription

I am currently diving into Angular 2 and have successfully fetched data from my service. However, before displaying it on the view, I need to iterate over it using a for() loop. To achieve this, I have stored the fetched JSON data into an array. But I&apos ...

Deleting a nested field in a document with NodeJS and MongoDB

I have a JSON object saved in a collection in the following format: [ { "username":"j", "pass":"t", "tracking": { "bettercallsaul":["1x1","1x2","1x3"] } } ] I am trying to remove the field "bet ...

Saving a pointer to "this" for usage in a named function callback through the prototype pattern in JavaScript

Having encountered difficulties obtaining a reference to a JavaScript object structured with the prototype pattern within a callback, specifically coming from a 3rd party component integrated into my object. The said 3rd party object connects to a message ...

How can one HTML form be used to request a unique identifier from the user, retrieve a record from the database, parse it into JSON format, and then populate an HTML page form using the

As someone who isn't an expert in any specific coding language, I have a knack for piecing things together to make them work. However, my current challenge is stretching my technical abilities a bit too far. Here's what I'm trying to achieve ...

Ensure that the React Material UI Textfield with the type "number" is validated to have exactly 10 characters

<TextField variant="outlined" required fullWidth id="accno" label="Main Account Number" type="number" name="accno" //inputProps={{ className:"input-acc", pattern: "^.{0,10}$"}} autoComplete="accno" onChange={(e) = ...