3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[TypeScript] Jestでユニットテストを書こうとしたら、'cannot find name jest' と表示された時に確認する箇所

Last updated at Posted at 2020-12-03

概要

TypeScriptを利用しているプロジェクトでJestを導入して、ユニットテストを書こうとしたら、cannot find name jestと表示され少しつまづいたので、対応した内容を記載します

実行環境

Node.js: v12.18.3
@babel/core: 7.10.1
@babel/preset-env: 7.10.1
@babel/preset-typescript: 7.12.1
babel-jest: 26.6.0

インストール時にやったこと

Jest公式のインストール手順に沿って、下記の手順でインストールを進めました。
(すでにプロジェクトには、babel, babel.comnfig.js, TypeScriptの設定は存在していている前提です。)

  1. yarn add --dev jest でJestをインストール
  2. yarn add --dev babel-jestで devDependenciesに babel用のJestのモジュールを追加

エラー発生時の状況

外部リソース(ここではS3)を利用したメソッドをテストするために、Mockを作ろうとしたタイミングでエディターにcannot find name jestと表示されました。

s3Operation.test.ts
jest.mock(aws) // ここで cannot find name jest のエラー

import{ S3 } from 'aws-sdk';

対応した内容

yarn add --dev @types/jest で jestの型をdevDependenciesに追加することで解決できました。

完全に自分のミスなのですが、Jest公式のページの下記の記載を読んだ時に、@types/jest は入れなくても動作すると勘違いしていたのがよくなかったです。

他にもstackoverflow などで同じようにつまづいている人もいたので、もう少しわかりやすい記載だとありがたかったかなという印象です。

You may also want to install the @types/jest module for the version of Jest you're using. This will help provide full typing when writing your tests with TypeScript.

参考

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?