[Angular Jest] VSCodeのエディタでCannot find name 〇〇が出た場合の対処法

angular jest Can not find name エラー Angular

メモとしてタイトルの事象が発生した時の対処法を残す

はじめに

Nx Angularでプロジェクトを作成して、nx testでjestのUnit testが通るのに、エディタ上ではエラーとなってる事象について

出力されるエラー

確認できたのは、下記

describe

Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha` and then add 'jest' or 'mocha' to the types field in your tsconfig.ts(2593)

beforeach

Cannot find name 'beforeEach'.ts(2304)

it

Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha` and then add 'jest' or 'mocha' to the types field in your tsconfig.ts(2593)

expect

Cannot find name 'expect'.ts(2304)

対処法

tsconfig.jsonから"path": "./tsconfig.editor.json"のブロックを削除して、エディタを開き直せば、エラーが消えます。

(参考)解決方法の気付き方

Cannot find nameで検索して、Stack Overflowとかを参考に試しても上手くいかず。

この時にnx generateでlibsを作成していて、そちらではspecファイルを開いてもエラーがなかったので、jest.configなど設定ファイルを比較してったら、tsconfig.tsの差分に気づいた。

コメント

タイトルとURLをコピーしました