UNPKG

4.64 kBMarkdownView Raw
1# Cookbook
2
3> Recipes for configuring `karma-typescript`
4
5## Hybrid application with coverage
6
7To get code coverage for both plain JavaScript modules and Typescript modules in a hybrid application,
8simply use `allowJs` in the Typescript compiler options and then pipe all `.js` and `.ts` files through
9`karma-typescript`:
10
11```javascript
12module.exports = function(config) {
13 config.set({
14
15 frameworks: ["jasmine", "karma-typescript"],
16
17 files: [
18 { pattern: "src/**/*.+(js|ts)" },
19 ],
20
21 preprocessors: {
22 "src/**/*.+(js|ts)": ["karma-typescript"],
23 },
24
25 reporters: ["progress", "karma-typescript"],
26
27 karmaTypescriptConfig: {
28 compilerOptions: {
29 allowJs: true,
30 },
31 },
32
33 browsers: ["Chrome"]
34 });
35};
36```
37
38## Code and tests in separate directories
39
40There are two ways to configure `karma-typescript` when you keep the application code and its unit tests in
41separate directories and you don't want the tests to get included in the coverage report.
42
431. The setting `karmaTypescriptConfig.coverageOptions.exclude`, which is a `RegExp` object (or an array of
44`RegExp` objects) for filtering which files get excluded from coverage instrumentation.
45
462. You can tell `karma-typescript` not to instrument all code for coverage automatically by adding `karma-coverage`
47to the `preprocessors` array; if the presence of `karma-coverage` is detected no code will be instrumented for
48coverage automatically by `karma-typescript`, giving you full control over which files should get instrumented:
49
50```javascript
51module.exports = function(config) {
52 config.set({
53
54 frameworks: ["jasmine", "karma-typescript"],
55
56 files: [
57 { pattern: "src/**/*.ts" },
58 { pattern: "test/**/*.ts" }
59 ],
60
61 preprocessors: {
62 "src/**/*.ts": ["karma-typescript", "coverage"],
63 "test/**/*.ts": ["karma-typescript"]
64 },
65
66 reporters: ["progress", "coverage", "karma-typescript"],
67
68 browsers: ["Chrome"]
69 });
70};
71```
72
73## Importing ES2015 (aka ES6) modules
74
75Modules written in ES6 syntax can't be run in a web browser directly (yet) and need to be compiled to
76ES5 syntax first. To do this automatically on each test run, you can use the bundler plugin
77[karma-typescript-es6-transform](https://github.com/monounity/karma-typescript-es6-transform):
78
79First, install the ES6 transforms plugin as a dev dependency:
80
81```bash
82npm install --save-dev karma-typescript-es6-transform
83```
84
85And then in the Karma configuration, configure the bundler to use the plugin:
86
87```javascript
88karmaTypescriptConfig: {
89 bundlerOptions: {
90 transforms: [require("karma-typescript-es6-transform")()]
91 }
92}
93```
94
95## PostCSS runner with a plugin
96
97In this recipe we set up `karma-typescript` to run the PostCSS `autoprefixer` plugin on all `.css` files
98with the PostCSS runner [karma-typescript-postcss-transform](https://github.com/monounity/karma-typescript-postcss-transform).
99
100First, install the PostCSS transforms plugin and the `autoprefixer` package as dev dependencies:
101
102```bash
103npm install --save-dev karma-typescript-postcss-transform autoprefixer
104```
105
106And then in the Karma configuration, configure the bundler to use the runner with a plugin and custom options:
107
108```javascript
109karmaTypescriptConfig: {
110 bundlerOptions: {
111 transforms: [
112 require("karma-typescript-postcss-transform")(
113 [require("autoprefixer")], { map: { inline: true } }, /\.css$/
114 )
115 ]
116 }
117}
118```
119
120## Css Modules
121
122When using (for instance) [React CSS Modules](https://github.com/gajus/react-css-modules), style sheets must
123be loaded as JSON objects by the bundler. This can be achieved by using the CSS Modules transforms plugin
124[karma-typescript-cssmodules-transform](https://github.com/monounity/karma-typescript-cssmodules-transform),
125which will transform style sheets to JSON on the fly each test run.
126
127First, install the CSS Modules transforms plugin as a dev dependency:
128
129```bash
130npm install --save-dev karma-typescript-cssmodules-transform
131```
132
133And then in the Karma configuration, configure the bundler to use the transform with custom options:
134
135```javascript
136karmaTypescriptConfig: {
137 bundlerOptions: {
138 transforms: [
139 require("karma-typescript-cssmodules-transform")({}, {}, /\.css$/),
140 ]
141 }
142}
143```
144
145## Emulating webpack's define plugin with bundler constants:
146
147```javascript
148karmaTypescriptConfig: {
149 bundlerOptions: {
150 constants: {
151 __PRODUCTION__: false
152 }
153 }
154}
155```