I'm currently working on a VueJS 2 project and I've been trying to integrate TypeScript into it. The challenge I'm facing is setting up Jest tests for my components.
Here's a snippet of my TypeScript component:
<template>
<div>some template<div>
</template>
<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
});
</script>
The build and serve process is functioning properly. However, when it comes to my spec file (just a dummy one named component.spec.ts):
import { shallowMount} from '@vue/test-utils';
//@ts-ignore
import Form from "@/MyComponent";
describe("Solicitacao Form component", () => {
let wrapper: any;
beforeEach(() => {
wrapper = shallowMount(Form, {
});
})
test('Component created', () => {
expect(wrapper).toBeDefined();
})
})
Every time I run the test, it throws an error message saying:
Test suite failed to run Jest encountered an unexpected token This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
Here's an excerpt from my jest.config.js file:
module.exports = {
preset: "@vue/cli-plugin-unit-jest/presets/typescript-and-babel",
//testEnvironment: 'node',
setupFiles: ["<rootDir>/tests/unit/index.js"],
moduleFileExtensions: ["js", "ts", "vue", "json"],
transform: {
".*\\.(vue)$": "vue-jest",
"^.+\\.ts?$": "ts-jest",
"^.+\\.js$": "babel-jest"
},
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1"
},
testMatch: [
"**/tests/unit/**/*.spec.(js|jsx|ts|tsx)"
]
}
Any suggestions on how to resolve this Jest setup issue?