簡體   English   中英

Jest typescript測試運行兩次,一次用於ts文件,一次用於js文件

[英]Jest typescript tests runs twice, one for ts files, and one for js files

我開始使用Jest和typescript編寫一些測試,但是我遇到了一些錯誤,問題似乎是測試運行了兩次,一次用於ts文件,第二次用於js文件。

打字稿測試通過,但編譯的javascript測試沒有。

yarn run v1.5.1
$ jest
 PASS  src/__tests__/some.test.ts (7.955s)
  ● Console

    console.log src/lib/google-analytics/ga-api.ts:75
      Succsess!!
    console.log src/__tests__/some.test.ts:42
      { reports: { batchGet: [Function: batchGet] } }

 FAIL  dist/__tests__/some.test.js
  ● Console

    console.log dist/lib/google-analytics/ga-api.js:64
      Reject

  ● it gets a full google analytics report

    No key or keyFile set.

      68 |
      69 |         return new Promise((resolve, reject) => {
    > 70 |             jwtClient.authorize((err: any) => {
      71 |                 if (err) {
      72 |                     console.log("Reject");
      73 |                     reject(err);

      at GoogleToken.<anonymous> (node_modules/googleapis/node_modules/gtoken/src/index.ts:102:13)
      at step (node_modules/googleapis/node_modules/gtoken/build/src/index.js:42:23)
      at Object.next (node_modules/googleapis/node_modules/gtoken/build/src/index.js:23:53)
      at node_modules/googleapis/node_modules/gtoken/build/src/index.js:17:71
      at Object.<anonymous>.__awaiter (node_modules/googleapis/node_modules/gtoken/build/src/index.js:13:12)
      at GoogleToken.Object.<anonymous>.GoogleToken.getTokenAsync (node_modules/googleapis/node_modules/gtoken/build/src/index.js:102:16)
      at GoogleToken.Object.<anonymous>.GoogleToken.getToken (node_modules/googleapis/node_modules/gtoken/src/index.ts:93:17)
      at JWT.<anonymous> (node_modules/googleapis/node_modules/google-auth-library/src/auth/jwtclient.ts:181:37)
      at step (node_modules/googleapis/node_modules/google-auth-library/build/src/auth/jwtclient.js:57:23)
      at Object.next (node_modules/googleapis/node_modules/google-auth-library/build/src/auth/jwtclient.js:38:53)
      at node_modules/googleapis/node_modules/google-auth-library/build/src/auth/jwtclient.js:32:71
      at Object.<anonymous>.__awaiter (node_modules/googleapis/node_modules/google-auth-library/build/src/auth/jwtclient.js:28:12)
      at JWT.Object.<anonymous>.JWT.refreshToken (node_modules/googleapis/node_modules/google-auth-library/build/src/auth/jwtclient.js:181:16)
      at JWT.<anonymous> (node_modules/googleapis/node_modules/google-auth-library/src/auth/jwtclient.ts:154:31)
      at step (node_modules/googleapis/node_modules/google-auth-library/build/src/auth/jwtclient.js:57:23)
      at Object.next (node_modules/googleapis/node_modules/google-auth-library/build/src/auth/jwtclient.js:38:53)
      at node_modules/googleapis/node_modules/google-auth-library/build/src/auth/jwtclient.js:32:71
      at Object.<anonymous>.__awaiter (node_modules/googleapis/node_modules/google-auth-library/build/src/auth/jwtclient.js:28:12)
      at JWT.Object.<anonymous>.JWT.authorizeAsync (node_modules/googleapis/node_modules/google-auth-library/build/src/auth/jwtclient.js:156:16)
      at JWT.Object.<anonymous>.JWT.authorize (node_modules/googleapis/node_modules/google-auth-library/src/auth/jwtclient.ts:147:12)
      at Promise (src/lib/google-analytics/ga-api.ts:70:23)
      at GoogleAnalyticsApiClient.getGCPAuthToken (src/lib/google-analytics/ga-api.ts:69:16)
      at GoogleAnalyticsApiClient.<anonymous> (src/lib/google-analytics/ga-api.ts:52:42)
      at dist/lib/google-analytics/ga-api.js:7:71
      at Object.<anonymous>.__awaiter (dist/lib/google-analytics/ga-api.js:3:12)
      at GoogleAnalyticsApiClient.getGaApiClient (dist/lib/google-analytics/ga-api.js:50:16)
      at Object.<anonymous>.test (src/__tests__/some.test.ts:41:14)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        9.516s
Ran all test suites.
error An unexpected error occurred: "Command failed.
Exit code: 1
Command: sh
Arguments: -c jest
Directory: /Users/carlosbernal/Documents/Grability/DataScience/ga-downloader
Output:
".
info If you think this is a bug, please open a bug report with the information provided in "/Users/carlosbernal/Documents/Grability/DataScience/ga-downloader/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

這是正常的ts-jest還是我錯過了一些額外的配置?

這是正常的ts-jest還是我錯過了一些額外的配置

您應該只將roots設置為/src 這是一個很好的配置

module.exports = {
  "roots": [
    "<rootDir>/src"
  ],
  "transform": {
    "^.+\\.tsx?$": "ts-jest"
  },
  "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "jsx",
    "json",
    "node"
  ],
}

我也只測試.tsx? 文件(沒有.jsx? );)

如果使用IDE,您可能只想禁用那些IDE中發生的.ts.js編譯,以避免生成.js文件,從而解決問題。

對於IntelliJ / Webstorm用戶:請參閱首選項| 語言和框架| TypeScript,在更改時重新編譯(取消選中)

有相同的問題,使用Typescript中的aws cdk來防止它,我將.js添加到jest-config.js中的testPathIgnorePatterns屬性

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  testPathIgnorePatterns: [".d.ts", ".js"]
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM