LoginSignup
1
0

More than 1 year has passed since last update.

【Vue】Jest実行時にCannot read property 'module' of undefined【Jest】

Last updated at Posted at 2021-07-30

環境

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": {}
}

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0