[Typescript]testファイルをどこに置くべきか

備忘録。Typescript + Jest + eslintなプロジェクトでjestとeslintが喧嘩していたのでなるべく労力を使わずに和解させた。もっといい方法があればTwitterで教えて下さい。


issue

以下のようなディレクトリ構成で、

GitHub - foxytanuki/anyswap-fetcher at b012f3cfb8707e7994f7840d7be1c42852331dc2

tests/V2Fetcher.test.ts

にて以下のように怒られた。

Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: tests/V2Fetcher.test.ts.
The file must be included in at least one of the projects provided.eslint

これはtsconfig.json で tests ディレクトリをallowlistに入れていない為に起きる現象だが、 tests をここに入れてしまうとbuildファイルに含まれてしまうのでいろいろと困る。(tsc)

{
	"include": ["src/**/*"]
}

unit testファイルをlint対象にするが、最終的なbuildファイルには含めない状態を作りたい。

やったこと

testを src 配下に置き、build後にtestファイルを削除するようにした。

参考元

eslint-plugin-jest/package.json at 237b551fd46a19b18f44e8764dbb5be672a818a0 · jest-community/eslint-plugin-jest

適用例

eslint-plugin-jest/package.json at 237b551fd46a19b18f44e8764dbb5be672a818a0 · jest-community/eslint-plugin-jest

(記事に関係ない変更も含まれてるけど気にしない。)

  • testファイルを移動
  • jestのtestMatchを変更
  • build scriptでtsc後にtestファイルを削除するように変更

おわり。