1 | [![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fstryker-mutator%2Fstryker%2Fmaster%3Fmodule%3Dtypescript)](https://dashboard.stryker-mutator.io/reports/github.com/stryker-mutator/stryker/master?module=typescript)
|
2 | [![Build Status](https://github.com/stryker-mutator/stryker/workflows/CI/badge.svg)](https://github.com/stryker-mutator/stryker/actions?query=workflow%3ACI+branch%3Amaster)
|
3 | [![NPM](https://img.shields.io/npm/dm/@stryker-mutator/typescript.svg)](https://www.npmjs.com/package/@stryker-mutator/typescript)
|
4 | [![Node version](https://img.shields.io/node/v/@stryker-mutator/typescript.svg)](https://img.shields.io/node/v/@stryker-mutator/typescript.svg)
|
5 | [![Slack Chat](https://img.shields.io/badge/slack-chat-brightgreen.svg?logo=slack)](https://join.slack.com/t/stryker-mutator/shared_invite/enQtOTUyMTYyNTg1NDQ0LTU4ODNmZDlmN2I3MmEyMTVhYjZlYmJkOThlNTY3NTM1M2QxYmM5YTM3ODQxYmJjY2YyYzllM2RkMmM1NjNjZjM)
|
6 |
|
7 | ![Stryker](https://github.com/stryker-mutator/stryker/raw/master/stryker-80x80.png)
|
8 |
|
9 | # Stryker Typescript
|
10 |
|
11 | A collection of plugins for native TypeScript support in [Stryker](https://stryker-mutator.io), the ~~JavaScript~~ *TypeScript* Mutation testing framework.
|
12 |
|
13 | ## Quickstart
|
14 |
|
15 | First, install Stryker itself (you can follow the [quickstart on the website](https://stryker-mutator.io/quickstart.html))
|
16 |
|
17 | Next, install this package:
|
18 |
|
19 | ```bash
|
20 | npm install --save-dev @stryker-mutator/typescript
|
21 | ```
|
22 |
|
23 | Now open up your `stryker.conf.js` (or `stryker.conf.json`) file and add the following components:
|
24 |
|
25 | ```javascript
|
26 | coverageAnalysis: 'perTest', // Coverage analysis is supported
|
27 | tsconfigFile: 'tsconfig.json', // Location of your tsconfig.json file
|
28 | mutator: 'typescript', // Specify that you want to mutate typescript code
|
29 | transpilers: [
|
30 | 'typescript' // Specify that your typescript code needs to be transpiled before tests can be run. Not needed if you're using ts-node Just-in-time compilation.
|
31 | ]
|
32 | ```
|
33 |
|
34 | Now give it a go:
|
35 |
|
36 | ```bash
|
37 | $ stryker run
|
38 | ```
|
39 |
|
40 | ## Peer dependencies
|
41 |
|
42 | The `@stryker-mutator/typescript` package is collection a plugins for `stryker` to enable `typescript` support. As such, you should make sure you have the correct versions of its dependencies installed:
|
43 |
|
44 | * `typescript`
|
45 | * `@stryker-mutator/core`
|
46 |
|
47 | For the current versions, see the `peerDependencies` section in the [package.json](https://github.com/stryker-mutator/stryker/blob/master/packages/typescript/package.json).
|
48 |
|
49 | These are marked as `peerDependencies` so you get a warning during installation when the correct versions are not installed.
|
50 |
|
51 | ## Load the plugins
|
52 |
|
53 | In order to use one of the `@stryker-mutator/typescript`'s plugins it must be loaded into Stryker.
|
54 | The easiest way to achieve this, is *not have a `plugins` section* in your config file. That way, all `node_modules` starting with `stryker-` will be loaded.
|
55 |
|
56 | If you do decide to choose specific modules, don't forget to add `'@stryker-mutator/typescript'` to the list of plugins to load.
|
57 |
|
58 | ## 3 Plugins
|
59 |
|
60 | This package contains 3 plugins to support TypeScript
|
61 |
|
62 | 1. [TypescriptOptionsEditor](#typescriptoptionseditor)
|
63 | 1. [TypescriptMutator](#typescriptmutator)
|
64 | 1. [TypescriptTranspiler](#typescripttranspiler)
|
65 |
|
66 | ### TypescriptOptionsEditor
|
67 |
|
68 | The `TypescriptOptionsEditor` is a handy plugin that reads **your** tsconfig.json file and loads it into stryker.conf.js. It will capture all your tsconfig settings to the `tsconfig` in stryker (this property is later used by the `TypescriptMutator` and the `TypescriptTranspiler`)
|
69 |
|
70 | Enable the config editor by pointing the `tsconfigFile` property to your tsconfig location:
|
71 |
|
72 | ```javascript
|
73 | // stryker.conf.js
|
74 | {
|
75 | tsconfigFile: 'tsconfig.json',
|
76 | }
|
77 | ```
|
78 |
|
79 | We always override some properties to enforce these rules (see [issue 391](https://github.com/stryker-mutator/stryker/issues/391) to find out why):
|
80 |
|
81 | ```js
|
82 | allowUnreachableCode: true
|
83 | noUnusedLocals: false
|
84 | noUnusedParameters: false
|
85 | ```
|
86 |
|
87 | ### TypescriptMutator
|
88 |
|
89 | The `TypescriptMutator` is a plugin to mutate typescript code. It builds a Typescript [Abstract Syntax Tree (AST)](https://en.wikipedia.org/wiki/Abstract_syntax_tree) and mutates your code using different kind of mutators.
|
90 |
|
91 | See [test code](https://github.com/stryker-mutator/stryker/tree/master/packages/typescript/test/unit/mutator) to know which mutations are supported.
|
92 |
|
93 | Configure the Typescript mutator in your `stryker.conf.js` (or `stryker.conf.json`) file:
|
94 |
|
95 | ```javascript
|
96 | // stryker.conf.js
|
97 | {
|
98 | mutator: 'typescript'
|
99 | }
|
100 | ```
|
101 |
|
102 | ### TypescriptTranspiler
|
103 |
|
104 | The `TypescriptTranspiler` is a plugin to transpile typescript source code before running tests. If you're using a bundler you might want to configure that instead.
|
105 |
|
106 | Given your Typescript configuration (see **TypescriptOptionsEditor**) it generates the javascript output. This is also used to transpile each mutant to javascript. Internally, it uses the same method as Typescript's watch mode (`tsc -w`), so it can transpile mutants fairly efficiently.
|
107 |
|
108 | Configure the Typescript transpiler in your `stryker.conf.js` (or `stryker.conf.json`) file:
|
109 |
|
110 | ```javascript
|
111 | // stryker.conf.js
|
112 | {
|
113 | transpilers: [
|
114 | 'typescript'
|
115 | // You can specify more transpilers if needed
|
116 | ]
|
117 | }
|
118 | ```
|