質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Nuxt.js

Nuxt.jsは、ユニバーサルなSPAが開発可能なVue.jsベースのフレームワーク。UIの描画サポートに特化しており、SSRにおけるサーバーサイドとクライアントサイドのUIレンダリングなどさまざまな機能を持ちます。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

Q&A

0回答

632閲覧

Nuxt + TypeScript で Jest テスト実行時に "Unexpected token <" エラーが発生する

naoki855

総合スコア75

Nuxt.js

Nuxt.jsは、ユニバーサルなSPAが開発可能なVue.jsベースのフレームワーク。UIの描画サポートに特化しており、SSRにおけるサーバーサイドとクライアントサイドのUIレンダリングなどさまざまな機能を持ちます。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

0グッド

0クリップ

投稿2019/06/01 11:03

create-nuxt-app を使って作成されたプロジェクトに TypeScript 対応をしました。
dev サーバーでの起動は確認できましたが、 Jest でのテストで下記のようなエラーが出ました。
試行錯誤はしているのですが、解決には至っておらず、ご教授いただきたく質問いたしました。

FAIL test/Logo.spec.ts ● 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. By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules". Here's what you can do: • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config. • If you need a custom transformation specify a "transform" option in your config. • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option. You'll find more details and examples of these config options in the docs: https://jestjs.io/docs/en/configuration.html Details: /typescript-test/components/Logo.vue:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){<template> ^ SyntaxError: Unexpected token < 1 | import { shallowMount } from '@vue/test-utils' > 2 | import Logo from '../components/Logo.vue' | ^ 3 | 4 | describe('Logo', () => { 5 | test('is a Vue instance', () => { at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:471:17) at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:513:25) at Object.<anonymous> (test/Logo.spec.ts:2:1)

関係するファイルの内容を記載します。(記載不足がありましたらご指摘ください。)
よろしくお願いいたします。

関係ファイル

components/Logo.vue

js

1<template> 2 <div class="VueToNuxtLogo"> 3 <div class="Triangle Triangle--two" /> 4 <div class="Triangle Triangle--one" /> 5 <div class="Triangle Triangle--three" /> 6 <div class="Triangle Triangle--four" /> 7 </div> 8</template> 9 10<script lang="ts"> 11import { Component, Vue } from 'vue-property-decorator' 12@Component 13export default class Logo extends Vue {} 14</script> 15 16<style> 17.VueToNuxtLogo { 18 display: inline-block; 19 animation: turn 2s linear forwards 1s; 20 transform: rotateX(180deg); 21 position: relative; 22 overflow: hidden; 23 height: 180px; 24 width: 245px; 25} 26 27.Triangle { 28 position: absolute; 29 top: 0; 30 left: 0; 31 width: 0; 32 height: 0; 33} 34 35.Triangle--one { 36 border-left: 105px solid transparent; 37 border-right: 105px solid transparent; 38 border-bottom: 180px solid #41b883; 39} 40 41.Triangle--two { 42 top: 30px; 43 left: 35px; 44 animation: goright 0.5s linear forwards 3.5s; 45 border-left: 87.5px solid transparent; 46 border-right: 87.5px solid transparent; 47 border-bottom: 150px solid #3b8070; 48} 49 50.Triangle--three { 51 top: 60px; 52 left: 35px; 53 animation: goright 0.5s linear forwards 3.5s; 54 border-left: 70px solid transparent; 55 border-right: 70px solid transparent; 56 border-bottom: 120px solid #35495e; 57} 58 59.Triangle--four { 60 top: 120px; 61 left: 70px; 62 animation: godown 0.5s linear forwards 3s; 63 border-left: 35px solid transparent; 64 border-right: 35px solid transparent; 65 border-bottom: 60px solid #fff; 66} 67 68@keyframes turn { 69 100% { 70 transform: rotateX(0deg); 71 } 72} 73 74@keyframes godown { 75 100% { 76 top: 180px; 77 } 78} 79 80@keyframes goright { 81 100% { 82 left: 70px; 83 } 84} 85</style>

test/Logo.spec.ts

js

1import { shallowMount } from '@vue/test-utils' 2import Logo from '../components/Logo.vue' 3 4describe('Logo', () => { 5 test('is a Vue instance', () => { 6 const wrapper = shallowMount(Logo) 7 expect(wrapper.isVueInstance()).toBeTruthy() 8 }) 9})

package.json

json

1{ 2 // ... 3 "dependencies": { 4 "@nuxtjs/axios": "^5.3.6", 5 "@nuxtjs/pwa": "^3.0.0-beta.16", 6 "bootstrap": "^4.1.3", 7 "bootstrap-vue": "^2.0.0-rc.11", 8 "cross-env": "^5.2.0", 9 "nuxt": "^2.8.0", 10 "vue-property-decorator": "^8.1.1" 11 }, 12 "devDependencies": { 13 "@nuxt/typescript": "^2.8.0", 14 "@nuxtjs/eslint-config": "^0.0.1", 15 "@types/jest": "^24.0.13", 16 "@types/node": "^12.0.4", 17 "@vue/cli-plugin-typescript": "^3.8.1", 18 "@vue/test-utils": "^1.0.0-beta.27", 19 "babel-core": "7.0.0-bridge.0", 20 "babel-eslint": "^10.0.1", 21 "babel-jest": "^24.8.0", 22 "eslint": "^5.0.1", 23 "eslint-config-prettier": "^4.3.0", 24 "eslint-config-standard": ">=12.0.0", 25 "eslint-loader": "^2.0.0", 26 "eslint-plugin-import": ">=2.14.0", 27 "eslint-plugin-jest": ">=21.24.1", 28 "eslint-plugin-node": ">=7.0.1", 29 "eslint-plugin-prettier": "3.1.0", 30 "eslint-plugin-promise": ">=4.0.1", 31 "eslint-plugin-standard": ">=4.0.0", 32 "eslint-plugin-vue": "^5.0.0", 33 "jest": "^24.8.0", 34 "nodemon": "^1.18.9", 35 "prettier": "1.17.1", 36 "ts-jest": "^24.0.2", 37 "ts-node": "^8.2.0", 38 "typescript": "^3.5.1", 39 "vue-jest": "^3.0.2", 40 "vue-template-compiler": "^2.6.10" 41 } 42}

tsconfig.json

json

1{ 2 "compilerOptions": { 3 "baseUrl": ".", 4 "types": [ 5 "@types/jest", 6 "@types/node", 7 "@nuxt/vue-app" 8 ], 9 "paths": { "@/*": [ "./*" ], "~/*": [ "./*" ] }, 10 "target": "esNext", 11 "strict": true, 12 "module": "esNext", 13 "moduleResolution": "node", 14 "experimentalDecorators": true, 15 "allowJs": true 16 } 17}

jest.config.ts

js

1module.exports = { 2 globals: { 3 'ts-jest': { 4 tsConfig: 'tsconfig.json', 5 diagnostics: false 6 } 7 }, 8 moduleNameMapper: { 9 '^@/(.*)$': '<rootDir>/$1', 10 '^~/(.*)$': '<rootDir>/$1', 11 '^vue$': 'vue/dist/vue.common.js' 12 }, 13 moduleFileExtensions: ['js', 'jsx', 'json', 'vue', 'ts', 'tsx'], 14 transform: { 15 '^.+\.js$': 'babel-jest', 16 '.*\.(vue)$': 'vue-jest', 17 '^.+\.tsx?$': 'ts-jest' 18 } 19}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問