0

I would like to do vue util test using jest, but since i used import statement, it keeps making errors. it says "SyntaxError: Cannot use import statement outside a module "

" at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)"

i've tried adding type:module to my package.json, transformIgnorePatterns to jest.config.js , and module to my script tag. none of them worked. i think it is related to version, as i am using vue 2 to get track of lessons im listening to. please take a look at my code

this is my package.json

{
  "name": "jest-test",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "test:unit": "vue-cli-service test:unit",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "axios": "^1.2.3",
    "core-js": "^3.6.5",
    "vue": "^2.6.11",
    "vuex": "^3.4.0"
  },
  "devDependencies": {
    "@babel/preset-env": "^7.20.2",
    "@vue/cli-plugin-babel": "^4.1.2",
    "@vue/cli-plugin-eslint": "^4.1.2",
    "@vue/cli-plugin-unit-jest": "^4.1.2",
    "@vue/cli-plugin-vuex": "^4.1.2",
    "@vue/cli-service": "^4.1.2",
    "@vue/eslint-config-standard": "^5.1.2",
    "@vue/test-utils": "^1.0.3",
    "babel-eslint": "^10.1.0",
    "eslint": "^6.7.2",
    "eslint-plugin-import": "^2.20.2",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-standard": "^4.0.0",
    "eslint-plugin-vue": "^6.2.2",
    "node-sass": "^4.12.0",
    "sass-loader": "^8.0.2",
    "vue-template-compiler": "^2.6.11"
  }
}

this is my error code to test

<template>
  <div>
    {{ todo.title }}
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      todo: {},
    };
  },
  created() {
    this.fetchTodo();
  },
  methods: {
    async fetchTodo() {
      const { data } = await axios.get(
        "https://jsonplaceholder.typicode.com/todos/1"
      );
      this.todo = data;
    },
  },
};
</script>

and this is testing code

import { shallowMount } from "@vue/test-utils";
import TodoTitle from "@/components/TodoTitle";

describe("TodoTitle Component", () =\> {
test("rendering", () =\> {
const wrapper = shallowMount(TodoTitle);
console.log(wrapper);
expect(wrapper.text()).toBe("");
});
});
YESSS
  • 31
  • 4
  • 1
    Does this answer your question? [Jest - SyntaxError: Cannot use import statement outside a module](https://stackoverflow.com/questions/59879689/jest-syntaxerror-cannot-use-import-statement-outside-a-module) – Thatkookooguy Jan 18 '23 at 08:03
  • still making the same error. thanks though ! – YESSS Jan 18 '23 at 08:16

0 Answers0