1 | # ts-jest
|
2 |
|
3 | [![NPM downloads](https://img.shields.io/npm/dm/ts-jest.svg?style=flat)](https://npmjs.org/package/ts-jest)
|
4 | [![Build Status for node v7](https://travis-badges.herokuapp.com/repos/kulshekhar/ts-jest/branches/master?job=0)](https://travis-ci.org/kulshekhar/ts-jest)
|
5 | [![Build Status for node v6](https://travis-badges.herokuapp.com/repos/kulshekhar/ts-jest/branches/master?job=1)](https://travis-ci.org/kulshekhar/ts-jest)
|
6 | [![Build Status for node v4](https://travis-badges.herokuapp.com/repos/kulshekhar/ts-jest/branches/master?job=2)](https://travis-ci.org/kulshekhar/ts-jest)
|
7 | [![Build Status for Windows](https://ci.appveyor.com/api/projects/status/g8tt9qd7usv0tolb/branch/master?svg=true)](https://ci.appveyor.com/project/kulshekhar/ts-jest/branch/master)
|
8 | [![npm version](https://badge.fury.io/js/ts-jest.svg)](https://badge.fury.io/js/ts-jest)
|
9 | [![dependencies](https://david-dm.org/kulshekhar/ts-jest.svg)](https://www.npmjs.com/package/ts-jest)
|
10 |
|
11 | ## Table of Contents
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | - [Versioning](#versioning)
|
17 | - [Usage](#usage)
|
18 | - [Options](#options)
|
19 | - [Known limitations for TS compiler options](#known-limitations-for-ts-compiler-options)
|
20 | - [How to Contribute](#how-to-contribute)
|
21 | - [Quickstart to run tests (only if you're working on this package)](#quickstart-to-run-tests-only-if-youre-working-on-this-package)
|
22 | - [License](#license)
|
23 |
|
24 |
|
25 |
|
26 | ## Versioning
|
27 | From version `"jest": "17.0.0"` we are using same MAJOR.MINOR as [`Jest`](https://github.com/facebook/jest).
|
28 | For `"jest": "< 17.0.0"` use `"ts-jest": "0.1.13"`. Docs for it see [here](https://github.com/kulshekhar/ts-jest/blob/e1f95e524ed62091736f70abf63530f1f107ec03/README.md).
|
29 |
|
30 | ## Usage
|
31 |
|
32 | To use this in your project, run:
|
33 | ```sh
|
34 | npm install --save-dev ts-jest
|
35 | ```
|
36 | Modify your project's `package.json` so that the `jest` section looks something like:
|
37 | ```json
|
38 | {
|
39 | "jest": {
|
40 | "transform": {
|
41 | ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
|
42 | },
|
43 | "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
|
44 | "moduleFileExtensions": [
|
45 | "ts",
|
46 | "tsx",
|
47 | "js"
|
48 | ]
|
49 | }
|
50 | }
|
51 | ```
|
52 | This setup should allow you to write Jest tests in Typescript and be able to locate errors without any additional gymnastics.
|
53 |
|
54 | By default `jest` does not provide code coverage remapping for transpiled codes, so if you'd like to have code coverage it needs additional coverage remapping. This can be done via writing custom processing script, or configure `testResultsProcessor` to use built-in coverage remapping in `ts-jest`.
|
55 | ```json
|
56 | {
|
57 | "jest": {
|
58 | "transform": {
|
59 | ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
|
60 | },
|
61 | "testResultsProcessor": "<rootDir>/node_modules/ts-jest/coverageprocessor.js"
|
62 | }
|
63 | }
|
64 | ```
|
65 |
|
66 | > **Note:** If you're experiencing remapping failure with source lookup, it may due to pre-created cache from `jest`. It can be manually deleted, or execute with [`--no-cache`](https://facebook.github.io/jest/docs/troubleshooting.html#caching-issues) to not use those.
|
67 |
|
68 | ### React Native
|
69 |
|
70 | There is a few additional steps if you want to use it with React Native.
|
71 |
|
72 | Install `babel-jest` and `babel-preset-react-native` modules.
|
73 |
|
74 | ```sh
|
75 | npm install -D babel-jest babel-preset-react-native
|
76 | ```
|
77 |
|
78 | Ensure `.babelrc` contains:
|
79 |
|
80 | ```json
|
81 | {
|
82 | "presets": ["react-native"]
|
83 | }
|
84 | ```
|
85 |
|
86 | In `package.json`, inside `jest` section, the `transform` should be like this:
|
87 | ```json
|
88 | "transform": {
|
89 | "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
|
90 | ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
|
91 | }
|
92 | ```
|
93 |
|
94 | ## Options
|
95 | By default this package will try to locate `tsconfig.json` and use its compiler options for your `.ts` and `.tsx` files.
|
96 |
|
97 | But you are able to override this behaviour and provide another path to your config for TypeScript by using `__TS_CONFIG__` option in `globals` for `jest`:
|
98 | ```json
|
99 | {
|
100 | "jest": {
|
101 | "globals": {
|
102 | "__TS_CONFIG__": "my-tsconfig.json"
|
103 | }
|
104 | }
|
105 | }
|
106 | ```
|
107 | Or even declare options for `tsc` instead of using separate config, like this:
|
108 | ```json
|
109 | {
|
110 | "jest": {
|
111 | "globals": {
|
112 | "__TS_CONFIG__": {
|
113 | "module": "commonjs",
|
114 | "jsx": "react"
|
115 | }
|
116 | }
|
117 | }
|
118 | }
|
119 | ```
|
120 | Note that the `module` property will be overwritten to `commonjs` since that is the format Jest expects.
|
121 |
|
122 | For all available options see [TypeScript docs](https://www.typescriptlang.org/docs/handbook/compiler-options.html).
|
123 |
|
124 | ### Known limitations for TS compiler options
|
125 | - You can't use `"target": "ES6"` while using `node v4` in your test environment;
|
126 | - You can't use `"jsx": "preserve"` for now (see [progress of this issue](https://github.com/kulshekhar/ts-jest/issues/63));
|
127 | - If you use `"baseUrl": "<path_to_your_sources>"`, you also have to change `jest config` a little bit:
|
128 | ```json
|
129 | "jest": {
|
130 | "moduleDirectories": ["node_modules", "<path_to_your_sources>"]
|
131 | }
|
132 | ```
|
133 |
|
134 | ## How to Contribute
|
135 | If you have any suggestions/pull requests to turn this into a useful package, just open an issue and I'll be happy to work with you to improve this.
|
136 |
|
137 | ### Quickstart to run tests (only if you're working on this package)
|
138 |
|
139 | ```sh
|
140 | git clone https://github.com/kulshekhar/ts-jest
|
141 | cd ts-jest
|
142 | npm install
|
143 | npm test
|
144 | ```
|
145 |
|
146 | ## License
|
147 |
|
148 | Copyright (c) [Authors](AUTHORS).
|
149 | This source code is licensed under the [MIT license](LICENSE).
|