UNPKG

6.33 kBMarkdownView Raw
1# karma-browserify
2
3[![CI](https://github.com/nikku/karma-browserify/actions/workflows/CI.yml/badge.svg)](https://github.com/nikku/karma-browserify/actions/workflows/CI.yml)
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
12You will also need to install [browserify](https://www.npmjs.com/package/browserify) and [watchify](https://www.npmjs.com/package/watchify) (for auto-watch only) with it.
13
14```
15npm install --save-dev karma-browserify browserify watchify
16```
17
18
19## Usage
20
21Add `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.
22
23
24```javascript
25module.exports = function(karma) {
26 karma.set({
27
28 frameworks: [ 'browserify', 'jasmine', 'or', 'any', 'other', 'framework' ],
29 files: ['test/**/*.js'],
30 preprocessors: {
31 'test/**/*.js': [ 'browserify' ]
32 },
33
34 browserify: {
35 debug: true,
36 transform: [ 'brfs' ]
37 }
38 });
39}
40```
41
42Look 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.
43
44
45### Browserify Config
46
47Test 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.
48
49For example to generate source maps for easier debugging, specify:
50
51```javascript
52 browserify: {
53 debug: true
54 }
55```
56
57There are three properties that are not passed directly:
58
59* [transform](#transforms)
60* [plugin](#plugins)
61* [configure](#additional-bundle-configuration)
62* bundleDelay
63
64
65#### Transforms
66
67If 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)).
68
69```javascript
70 browserify: {
71 transform: [ 'reactify', 'coffeeify', 'brfs' ]
72
73 // don't forget to register the extensions
74 extensions: ['.js', '.jsx', '.coffee']
75 }
76```
77
78You can also specify options for the transformations:
79
80```javascript
81 browserify: {
82 transform: [ ['reactify', {'es6': true}], 'coffeeify', 'brfs' ]
83 }
84```
85
86#### Plugins
87
88The [browserify plugin](https://github.com/substack/node-browserify#bpluginplugin-opts) option supports the same syntax as `transform`.
89
90```javascript
91 browserify: {
92 plugin: [ 'stringify' ]
93 }
94```
95
96#### Additional Bundle Configuration
97
98You 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):
99
100```javascript
101 browserify: {
102 configure: function(bundle) {
103 bundle.on('prebundle', function() {
104 bundle.external('foobar');
105 });
106 }
107 }
108```
109
110You'll also need to use the `'prebundle'` event for full control over the order of transforms and plugins:
111
112```javascript
113 browserify: {
114 configure: function(bundle) {
115 bundle.once('prebundle', function() {
116 bundle.transform('babelify').plugin('proxyquireify/plugin');
117 });
118 }
119 }
120```
121
122Note that transforms must only be added once.
123
124
125### Watchify Config
126
127You 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`.
128
129```javascript
130 watchify: {
131 poll: true
132 }
133```
134
135
136## How it Works
137
138This 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`.
139
140Before 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.
141
142
143## Detailed Configuration
144
145The following code snippet shows a Karma configuration file with all browserify-related options.
146
147```javascript
148module.exports = function(karma) {
149 karma.set({
150
151 // include browserify first in used frameworks
152 frameworks: [ 'browserify', 'jasmine' ],
153
154 // add all your files here,
155 // including non-commonJS files you need to load before your test cases
156 files: [
157 'some-non-cjs-library.js',
158 'test/**/*.js'
159 ],
160
161 // add preprocessor to the files that should be
162 // processed via browserify
163 preprocessors: {
164 'test/**/*.js': [ 'browserify' ]
165 },
166
167 // see what is going on
168 logLevel: 'LOG_DEBUG',
169
170 // use autoWatch=true for quick and easy test re-execution once files change
171 autoWatch: true,
172
173 // add additional browserify configuration properties here
174 // such as transform and/or debug=true to generate source maps
175 browserify: {
176 debug: true,
177 transform: [ 'brfs' ],
178 configure: function(bundle) {
179 bundle.on('prebundle', function() {
180 bundle.external('foobar');
181 });
182 }
183 }
184 });
185};
186```
187
188
189## Related
190
191Credit 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.
192
193
194
195## Maintainers
196
197* [Ben Drucker](https://github.com/bendrucker)
198* [Nico Rehwaldt](https://github.com/nikku)
199
200
201## License
202
203MIT