UNPKG

6.11 kBMarkdownView Raw
1# karma-browserify
2
3[![Build Status](https://travis-ci.org/nikku/karma-browserify.svg?branch=master)](https://travis-ci.org/nikku/karma-browserify)
4
5[karma-browserify](https://github.com/nikku/karma-browserify) is a fast [browserify](http://browserify.org) integration for [Karma](https://karma-runner.github.io) that handles large projects with ease.
6
7
8## Installation
9
10Get the plug-in via [npm](https://www.npmjs.org/)
11
12```
13npm install --save-dev karma-browserify
14```
15
16
17## Usage
18
19Add `browserify` as a framework to your Karma configuration file. For each file that should be processed and bundled by Karma, configure the `browserify` preprocessor. Optionally use the `browserify` config entry to configure how the bundle gets created.
20
21
22```javascript
23module.exports = function(karma) {
24 karma.set({
25
26 frameworks: [ 'browserify', 'jasmine', 'or', 'any', 'other', 'framework' ],
27 files: ['test/**/*.js'],
28 preprocessors: {
29 'test/**/*.js': [ 'browserify' ]
30 },
31
32 browserify: {
33 debug: true,
34 transform: [ 'brfs' ]
35 }
36 });
37}
38```
39
40Look at the [example directory](https://github.com/nikku/karma-browserify/tree/master/example) for a simple [browserify](http://browserify.org) + [jasmine](http://jasmine.github.io) project that uses this plug-in.
41
42
43### Browserify Config
44
45Test bundles can be configured through the `browserify` Karma configuration property. [Configuration options](https://github.com/substack/node-browserify#var-b--browserifyfiles-or-opts) are passed directly to browserify.
46
47For example to generate source maps for easier debugging, specify:
48
49```javascript
50 browserify: {
51 debug: true
52 }
53```
54
55There are three properties that are not passed directly:
56
57* [transform](#transforms)
58* [plugin](#plugins)
59* [configure](#additional-bundle-configuration)
60* bundleDelay
61
62
63#### Transforms
64
65If you use CoffeeScript, JSX or other tools that need to transform the source file before bundling, specify a [browserify transform](https://github.com/substack/node-browserify#btransformtr-opts) (Karma preprocessors are [not supported](https://github.com/nikku/karma-browserify/issues/36)).
66
67```javascript
68 browserify: {
69 transform: [ 'reactify', 'coffeeify', 'brfs' ]
70
71 // don't forget to register the extensions
72 extensions: ['.js', '.jsx', '.coffee']
73 }
74```
75
76You can also specify options for the transformations:
77
78```javascript
79 browserify: {
80 transform: [ ['reactify', {'es6': true}], 'coffeeify', 'brfs' ]
81 }
82```
83
84#### Plugins
85
86The [browserify plugin](https://github.com/substack/node-browserify#bpluginplugin-opts) option supports the same syntax as `transform`.
87
88```javascript
89 browserify: {
90 plugin: [ 'stringify' ]
91 }
92```
93
94#### Additional Bundle Configuration
95
96You may perform additional configuration in a function passed as the `configure` option and that receives the browserify instance as an argument. A custom `prebundle` event is emitted on the bundle right before a bundling operation takes place. This is useful when setting up things like [externals](https://github.com/substack/node-browserify#external-requires):
97
98```javascript
99 browserify: {
100 configure: function(bundle) {
101 bundle.on('prebundle', function() {
102 bundle.external('foobar');
103 });
104 }
105 }
106```
107
108You'll also need to use the `'prebundle'` event for full control over the order of transforms and plugins:
109
110```javascript
111 browserify: {
112 configure: function(bundle) {
113 bundle.once('prebundle', function() {
114 bundle.transform('babelify').plugin('proxyquireify/plugin');
115 });
116 }
117 }
118```
119
120Note that transforms must only be added once.
121
122
123### Watchify Config
124
125You can configure the underlying [watchify](https://github.com/substack/watchify) instance via `config.watchify`. This is helpful if you need to fine tune the change detection used during `autoWatch=true`.
126
127```javascript
128 watchify: {
129 poll: true
130 }
131```
132
133
134## How it Works
135
136This project is a preprocessor for Karma that combines test files and dependencies into a browserified bundle. It relies on [watchify](https://github.com/substack/watchify) to generate the bundle and to keep it updated during `autoWatch=true`.
137
138Before the initial test run we build one browserify bundle for all test cases and dependencies. Once any of the files change, it incrementally updates the bundle. Each file included in Karma is required from the file bundle via a stub. Thereby it ensures tests are only executed once per test run.
139
140
141## Detailed Configuration
142
143The following code snippet shows a Karma configuration file with all Bro related options.
144
145```javascript
146module.exports = function(karma) {
147 karma.set({
148
149 // include browserify first in used frameworks
150 frameworks: [ 'browserify', 'jasmine' ],
151
152 // add all your files here,
153 // including non-commonJS files you need to load before your test cases
154 files: [
155 'some-non-cjs-library.js',
156 'test/**/*.js'
157 ],
158
159 // add preprocessor to the files that should be
160 // processed via browserify
161 preprocessors: {
162 'test/**/*.js': [ 'browserify' ]
163 },
164
165 // see what is going on
166 logLevel: 'LOG_DEBUG',
167
168 // use autoWatch=true for quick and easy test re-execution once files change
169 autoWatch: true,
170
171 // add additional browserify configuration properties here
172 // such as transform and/or debug=true to generate source maps
173 browserify: {
174 debug: true,
175 transform: [ 'brfs' ],
176 configure: function(bundle) {
177 bundle.on('prebundle', function() {
178 bundle.external('foobar');
179 });
180 }
181 }
182 });
183};
184```
185
186
187## Related
188
189Credit goes to to the original [karma-browserify](https://github.com/xdissent/karma-browserify) and [karma-browserifast](https://github.com/cjohansen/karma-browserifast). This library builds on the lessons learned in these projects and offers improved configurability, speed and/or the ability to handle large projects.
190
191
192
193## Maintainers
194
195* [Ben Drucker](https://github.com/bendrucker)
196* [Nico Rehwaldt](https://github.com/nikku)
197
198
199## License
200
201MIT