環境
Vue 2.6.11
cli-plugin-unit-jest 4.5.0
発生したエラー
$ yarn test:unit
● Test suite failed to run
TypeError: Cannot read property 'module' of undefined
...
該当のテストコード
login.spec.ts
import Test from './component/Test.vue';
test('test Test Component', () => {
console.log(Login);
// 本来はここにテストコードを書く
});
テストコードは、記事の本筋から逸れてしまい、長くなるので載せていません。あしからずorz
1行目のimportがうまくいってないようです。
解決方法
まず、大前提として、importのパスが正しいか、import先でexportがされているかを確認し、vueファイルが読み込めてないのか?と思い、package.jsonやjest.config.jsの設定を見直しても直らず。。。
そこで下記のGitHub issuesを発見。
tsconfig.json with no compilerOptions causes typescript-compiler to error
下記のように、tsconfig.jsonに"compilerOptions": {}
を追加することで直りました。
tsconfig.json
{
"extends": "../../tsconfig",
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
],
"compilerOptions": {}
}