UNPKG

5.22 kBMarkdownView Raw
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
11A collection of plugins for native TypeScript support in [Stryker](https://stryker-mutator.io), the ~~JavaScript~~ *TypeScript* Mutation testing framework.
12
13## Quickstart
14
15First, install Stryker itself (you can follow the [quickstart on the website](https://stryker-mutator.io/quickstart.html))
16
17Next, install this package:
18
19```bash
20npm install --save-dev @stryker-mutator/typescript
21```
22
23Now open up your `stryker.conf.js` (or `stryker.conf.json`) file and add the following components:
24
25```javascript
26coverageAnalysis: 'perTest', // Coverage analysis is supported
27tsconfigFile: 'tsconfig.json', // Location of your tsconfig.json file
28mutator: 'typescript', // Specify that you want to mutate typescript code
29transpilers: [
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
34Now give it a go:
35
36```bash
37$ stryker run
38```
39
40## Peer dependencies
41
42The `@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
47For the current versions, see the `peerDependencies` section in the [package.json](https://github.com/stryker-mutator/stryker/blob/master/packages/typescript/package.json).
48
49These are marked as `peerDependencies` so you get a warning during installation when the correct versions are not installed.
50
51## Load the plugins
52
53In order to use one of the `@stryker-mutator/typescript`'s plugins it must be loaded into Stryker.
54The 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
56If 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
60This package contains 3 plugins to support TypeScript
61
621. [TypescriptOptionsEditor](#typescriptoptionseditor)
631. [TypescriptMutator](#typescriptmutator)
641. [TypescriptTranspiler](#typescripttranspiler)
65
66### TypescriptOptionsEditor
67
68The `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
70Enable 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
79We 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
82allowUnreachableCode: true
83noUnusedLocals: false
84noUnusedParameters: false
85```
86
87### TypescriptMutator
88
89The `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
91See [test code](https://github.com/stryker-mutator/stryker/tree/master/packages/typescript/test/unit/mutator) to know which mutations are supported.
92
93Configure 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
104The `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
106Given 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
108Configure 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```