.tsファイル(TypeScript)を使用してJestグローバルテストセットアップを構成する



Configure Jest Global Tests Setup With



解決:

私は解決策を見つけました。

私の最初のプロジェクト構造は次のとおりです。



。 | --src | | --service.ts | --tests | | --service.test.ts | --dist | --tsconfig.json | --package.json

package.jsonのJest構成:

'jest':{'globals':{'ts-jest':{'tsConfigFile': 'tsconfig.json'}}、 'moduleFileExtensions':['ts'、 'js']、 'transform':tsx)$ ':' ./node_modules/ts-jest/preprocessor.js '、' testMatch ':[' ** / tests / ** / *。test。(ts | js) ']、' testEnvironment ':' node '}

この構成ではts-jestは、.tsファイルのメモリトランスパイルを実行します。私が持っていますのoutDirオプション私のcompilerOptionstsconfig.jsonですが、そのoutDirには何も書き込まれていません。また、トランスパイルされたjest-config.jsを使用できないためです。



次に、src内のtestsフォルダーを移動し、Jest構成を変更します 。プロジェクトの新しい構造は次のとおりです。

。 | --src | | --service.ts | |-テスト| | --service.test.ts | | --jest-config.ts | --dist | --tsconfig.json | --package.json

新しいJest構成は次のとおりです。

'jest':{'globalSetup': './ dist / tests / jest-config.js'、 'moduleFileExtensions':['ts'、 'js'、 'json']、 'testMatch':['** / dist / **/*。test.js ']、' testEnvironment ':'ノード '}

これで、Jestのグローバルセットアップにjest-config.jsを使用できます。



添加

  • Jestセットアップファイル(私の例ではjest-config.js)は、1つの非同期関数をエクスポートする必要があります。

    module.exports = async function(){...}

  • テストを実行する前に、ソース.tsファイルをコンパイルする必要があります。