現在nuxt-ts下でjest+ts-jest+vue-jest+@vue/test-utilsを使用したunitテスト環境を構築しています。
セットアップ方法はVue Test Utils公式サイトを参考に行っていました。
Vue Test Utils->TypeScript と一緒に使う
上記ページを参考に必要なものをインストールし、jest.config.js
とtsconfig.json
を編集後、テストコードを用意しjestのテストを実行したところ、以下のエラーが表示されテストが失敗してしまいます。
sh
1 2 FAIL test/HelloWorld.spec.ts 3 ● Test suite failed to run 4 5 Jest encountered an unexpected token 6 7 This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript. 8 9 By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules". 10 11 Here's what you can do: 12 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config. 13 • If you need a custom transformation specify a "transform" option in your config. 14 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option. 15 16 You'll find more details and examples of these config options in the docs: 17 https://jestjs.io/docs/en/configuration.html 18 19 Details: 20 21 SyntaxError: Unexpected token } in JSON at position 504 22 at JSON.parse (<anonymous>) 23 24 at parse (node_modules/tsconfig/src/tsconfig.ts:195:15) 25 at readFileSync (node_modules/tsconfig/src/tsconfig.ts:181:10) 26 at Object.loadSync (node_modules/tsconfig/src/tsconfig.ts:151:18) 27 at find (node_modules/vue-jest/lib/load-typescript-config.js:32:39) 28 at loadTypescriptConfig (node_modules/vue-jest/lib/load-typescript-config.js:71:26) 29 at compileTypescript (node_modules/vue-jest/lib/compilers/typescript-compiler.js:9:20) 30 at processScript (node_modules/vue-jest/lib/process.js:23:12) 31 at Object.module.exports [as process] (node_modules/vue-jest/lib/process.js:42:18) 32 33Test Suites: 1 failed, 1 total 34Tests: 0 total 35Snapshots: 0 total 36Time: 8.494s 37Ran all test suites. 38error Command failed with exit code 1. 39info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
自身でも色々試行錯誤をし、また公式や類似エラーが出ているissueも確認しトライしてみましたが、同一エラーが表示されました。
こちら何が原因なのでしょうか?
またどのようにすればnuxt-ts下でjestのテストを行えるようになるでしょうか?
自身では原因が掴めず、長時間ハマってしまっているので、知見がある方いらっしゃいましたら、解決方法をご教授頂けると大変助かります。
以下に環境情報や、jest.config.js
などのコードを記載しておきますので、こちら確認の上ご返答頂けると助かります。
環境情報
- Node.js => 10.15.1
- yarn => 1.9.2
- @types/jest => ^24.0.4
- jest => ^23.6.0
- ts-jest => ^23.10.5
- vue-jest => ^3.0.2
各コード
jest.config.js
js
1module.exports = { 2 globals: { 3 'ts-jest': { 4 tsConfig: 'tsconfig.json' 5 } 6 }, 7 cache: false, 8 moduleFileExtensions: ['js', 'jsx', 'json', 'vue', 'ts', 'tsx'], 9 transform: { 10 '.*\.(vue)$': 'vue-jest', 11 '^.+\.tsx?$': 'ts-jest', 12 }, 13 moduleNameMapper: { 14 '^@/(.*)$': '<rootDir>/$1', 15 '^~/(.*)$': '<rootDir>/$1', 16 '^vue$': 'vue/dist/vue.common.js' 17 }, 18 transformIgnorePatterns: ['<rootDir>/node_modules//(?!(vue-jest|ts-jest)/)'], 19 testRegex: '(/__tests__/.*|(\.|/)(test|spec))\.(jsx?|tsx?)$', 20 testURL: 'http://localhost/' 21} 22
tsconfig.json
json
1{ 2 "extends": "@nuxt/typescript", 3 "compilerOptions": { 4 "baseUrl": ".", 5 "experimentalDecorators": true, 6 "allowJs": true, 7 "types": [ 8 "@types/node", 9 "@types/jest", 10 "@nuxt/vue-app" 11 ], 12 "paths": { 13 "@/*": ["./*"], 14 "~/*": ["./*"], 15 }, 16 }, 17} 18
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。