【问题标题】:Jest typescript tests runs twice, one for ts files, and one for js filesJest typescript 测试运行两次,一次用于 ts 文件,一次用于 js 文件
【发布时间】:2018-10-13 04:18:26
【问题描述】:

我开始使用 Jest 和 typescript 编写一些测试,但是我遇到了一些错误,问题似乎是测试运行了两次,一次用于 ts 文件,另一次用于 js 文件。

typescript 测试通过,但编译后的 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 中是正常的还是我缺少一些额外的配置?

【问题讨论】:

    标签: typescript jestjs


    【解决方案1】:

    这在 ts-jest 中是正常的还是我缺少一些额外的配置

    您应该只将roots 设置为/srcHere is a good config:

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

    我也只测试.tsx? 文件(没有.jsx?);)

    【讨论】:

    • 这在我的设置中不起作用,因为我使用的是用 TypeScript 编写的 jest 快照序列化程序。 :( 我认为从moduleFileExtensions 中删除/jsx?/ 可以解决此问题,但目前它会触发此错误:github.com/facebook/jest/issues/4025
    • 太棒了,谢谢。 moduleFileExtensions 的行是我需要的。没有它,jest 会更喜欢导入我的 *.js 文件并针对它们运行测试。如果我包含它,jest 会切换到使用所需的行为,它优先考虑具有*.ts 扩展名的文件。
    【解决方案2】:

    有同样的问题,在 Typescript 中使用 aws cdk,为了防止它,我在 jest-config.js 中的 testPathIgnorePatterns 属性中添加了.js

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

    【讨论】:

      【解决方案3】:

      如果使用 IDE,您可能只想禁用这些 IDE 中发生的 .ts.js 编译,以避免生成给您带来问题的 .js 文件。

      对于 IntelliJ/Webstorm 用户:请参阅首选项 |语言和框架 | TypeScript,更改时重新编译(取消选中)

      【讨论】:

        猜你喜欢
        • 2021-09-10
        • 1970-01-01
        • 2016-07-13
        • 1970-01-01
        • 1970-01-01
        • 2015-09-04
        • 1970-01-01
        • 2017-08-28
        • 2021-07-16
        相关资源
        最近更新 更多