Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm starting to write some tests using Jest and typescript, however I'm getting some errors and the problem seems to be that the tests are run twice, one for ts files, and a second time for js files.

The typescript test passes, but the compiled javascript tests don't.

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.

Is this normal in ts-jest or am I missing some extra configuration?

like image 284
BernalCarlos Avatar asked May 02 '18 23:05

BernalCarlos


People also ask

Do Jest tests run concurrently?

To speed-up your tests, Jest can run them in parallel. By default, Jest will parallelise tests that are in different files. IMPORTANT: Paralellising tests mean using different threads to run test-cases simultaneously.

Can Jest run TypeScript tests?

Configure Jest ts-jest is a TypeScript preprocessor for jest , that lets you use jest to test projects written in TypeScript. This will create a file named jest. config. js with a setting for jest to use the preprocessor js-test .

Does Jest need TS node?

When you run jest with a jest. config. ts file it will use ts-node to compile that file, then it will pass it to ts-jest which will compile your tests, then it will pass those . js tests to jest to run it.


2 Answers

Is this normal in ts-jest or am I missing some extra configuration

You should set roots to /src only. Here is a good config:

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

I also only test .tsx? files (no .jsx?) ;)

like image 183
basarat Avatar answered Oct 19 '22 08:10

basarat


Had the same issue, using the aws cdk in Typescript, to prevent it, I added .js to the testPathIgnorePatterns property in jest-config.js

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  testPathIgnorePatterns: [".d.ts", ".js"]
};
like image 10
mlo55 Avatar answered Oct 19 '22 06:10

mlo55