swissquote / crafty

The task runner that binds them all
https://swissquote.github.io/crafty/
Apache License 2.0
30 stars 12 forks source link

[crafty-preset-jest] [crafty-preset-typescript] Code coverage doesn't work #21

Closed onigoetz closed 6 years ago

onigoetz commented 6 years ago

The code coverage is not calculated on TypeScript files when running Jest

Elendev commented 6 years ago

This is indeed an issue.

While using Crafty, I get this coverage :

Test Suites: 14 passed, 14 total
Tests:       39 passed, 39 total
Snapshots:   37 passed, 37 total
Time:        6.92s, estimated 8s
Ran all test suites.
----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |        0 |        0 |        0 |                   |
----------|----------|----------|----------|----------|-------------------|

By using Jest directly, I get this coverage :

Test Suites: 14 passed, 14 total
Tests:       39 passed, 39 total
Snapshots:   37 passed, 37 total
Time:        6.037s
Ran all test suites.
-------------------------------|----------|----------|----------|----------|-------------------|
File                           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-------------------------------|----------|----------|----------|----------|-------------------|
All files                      |    72.55 |    64.88 |    72.12 |    78.14 |                   |
 app                           |       80 |      100 |        0 |       80 |                   |
  jestSetup.js                 |       80 |      100 |        0 |       80 |                 5 |
 app/src/js/components/blocks  |      100 |      100 |      100 |      100 |                   |
  HeaderImageBlock.tsx         |      100 |      100 |      100 |      100 |                   |
 app/src/js/components/core    |      100 |      100 |      100 |      100 |                   |
  Loader.tsx                   |      100 |      100 |      100 |      100 |                   |
 app/src/js/components/crm     |      100 |      100 |      100 |      100 |                   |
  Form.tsx                     |      100 |      100 |      100 |      100 |                   |
 app/src/js/components/date    |    69.23 |    68.42 |    66.67 |    72.73 |                   |
  DateLabel.tsx                |    69.23 |    68.42 |    66.67 |    72.73 |      3,4,5,6,8,16 |
 app/src/js/components/events  |    70.27 |    61.54 |    72.62 |    77.27 |                   |
  Event.tsx                    |    86.21 |    66.67 |       75 |       92 |               4,5 |
  EventButton.tsx              |    67.86 |    66.67 |       75 |    66.67 |   3,4,5,6,8,13,16 |
  EventHeader.tsx              |    77.78 |    83.33 |    66.67 |       75 |         3,4,5,6,8 |
  EventMicrodata.tsx           |      100 |      100 |      100 |      100 |                   |
  EventTeaser.tsx              |    73.81 |    59.09 |       60 |    76.67 |4,5,13,14,15,16,18 |
  Events.tsx                   |    62.82 |    57.69 |    63.64 |    70.59 |... 17,118,120,121 |
  EventsApp.tsx                |    64.34 |    55.67 |    79.41 |    76.47 |... 19,120,121,122 |
  Pill.tsx                     |      100 |      100 |      100 |      100 |                   |
 app/src/js/components/speaker |      100 |      100 |      100 |      100 |                   |
  Speaker.tsx                  |      100 |      100 |      100 |      100 |                   |
 app/src/js/schemas            |    53.85 |       50 |       50 |    58.33 |                   |
  Script.tsx                   |    53.85 |       50 |       50 |    58.33 |         3,4,5,6,8 |
 app/src/js/schemas/types      |    55.56 |    57.14 |       60 |       60 |                   |
  Event.ts                     |     62.5 |     62.5 |    66.67 |    66.67 |         3,4,5,6,8 |
  Place.ts                     |    45.45 |       50 |       50 |       50 |         3,4,5,6,8 |
 app/src/js/utils              |      100 |      100 |       75 |      100 |                   |
  SEOUtils.ts                  |      100 |      100 |       75 |      100 |                   |
-------------------------------|----------|----------|----------|----------|-------------------|

The 'direct' Jest configuration (the one working) looks like this :

  "jest": {
    "transform": {
      "^.+\\.(ts|tsx)?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ],
    "setupFiles": [
      "./jestSetup.js"
    ],
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/src/__mocks__/fileMock.js",
      "\\.(css|less|scss)$": "<rootDir>/src/__mocks__/styleMock.js"
    }
  }

The Crafty configuration file (not working) looks like this :

{
  "moduleDirectories": [
    "[...]\/node_modules\/@swissquote\/crafty-preset-react\/node_modules",
    "[...]\/node_modules\/@swissquote\/crafty-preset-jest\/node_modules",
    "[...]\/node_modules",
    [...]\/node_modules\/@swissquote\/crafty-preset-typescript\/node_modules"
  ],
  "moduleFileExtensions": [
    "js",
    "json",
    "ts",
    "tsx"
  ],
  "testPathIgnorePatterns": [
    "\/node_modules\/",
    "[...]\/dist"
  ],
  "moduleNameMapper": {
    "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "\/home\/jonas\/workspace\/cws\/drupal8\/src\/themes\/cws\/app\/node_modules\/@swissquote\/crafty-preset-jest\/src\/file-mock.js",
    "\\.(css|less|sass|scss)$": "[...]\/node_modules\/@swissquote\/crafty-preset-jest\/src\/style-mock.js"
  },
  "bail": true,
  "roots": [
    "[...]"
  ],
  "transform": {
    "^.+\\.(ts|tsx)?$": "[...]\/node_modules\/@swissquote\/crafty-preset-typescript\/node_modules\/ts-jest\/preprocessor.js"
  },
  "globals": {

  },
  "setupTestFrameworkScriptFile": "[...]\/node_modules\/@swissquote\/crafty-preset-react\/testSetup.js",
  "testRegex": "(\/__tests__\/.*|(\\.|\/)(test|spec))\\.(js|ts|tsx)$"
}

(replace [...] by the project root).

I've tried to modify some part of the configuration (like forcing bail to false to be consistent) but with no success so far.

Elendev commented 6 years ago

I've tried to reproduce exactly the same configuration :

  "jest": {
    "transform": {
      "^.+\\.(ts|tsx)?$": "<rootDir>/node_modules/@swissquote/crafty-preset-typescript/node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(js|ts|tsx)$",
    "moduleFileExtensions": [
      "ts",
      "json",
      "tsx",
      "js"
    ],
    "bail": true,
    "moduleDirectories": [
      "<rootDir>/node_modules/@swissquote/crafty-preset-react/node_modules",
      "<rootDir>/node_modules/@swissquote/crafty-preset-jest/node_modules",
      "<rootDir>/node_modules",
      "<rootDir>/node_modules/@swissquote/crafty-preset-typescript/node_modules"
    ],
    "setupTestFrameworkScriptFile": "<rootDir>/node_modules/@swissquote/crafty-preset-react/testSetup.js",
    "testPathIgnorePatterns": [
      "/node_modules/",
      "<rootDir>/dist"
    ],
    "globals": {

    },
    "roots": ["<rootDir>"],
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/node_modules/@swissquote/crafty-preset-jest/src/file-mock.js",
      "\\.(css|less|scss)$": "<rootDir>/node_modules/@swissquote/crafty-preset-jest/src/style-mock.js"
    }
  }

Jest configuration in my package.json

The coverage is still good when I call Jest directly, so the issue doesn't seems to be a configuration issue.

onigoetz commented 6 years ago

As I suspected, the coverage only works if jest's configuration file is at the root of the project to cover. This is fixed here : cc23b7ec3384656839cb0003e04d2164b449cce4 and will be released tomorrow