这是indexloc提供的服务,不要输入任何密码
Skip to content

Commit cbdb405

Browse files
authored
Merge b5ddd20 into d3ff91a
2 parents d3ff91a + b5ddd20 commit cbdb405

File tree

9 files changed

+966
-114
lines changed

9 files changed

+966
-114
lines changed

docs/dev/04-public-api.md

Lines changed: 258 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@ You can, however, call Karma programmatically from your node module. Here is the
44

55
## karma.Server(options, [callback=process.exit])
66

7-
### Constructor
7+
### `constructor`
8+
9+
- **Returns:** `Server` instance.
10+
11+
#### Usage
12+
13+
Notice the capital 'S' on `require('karma').Server`.
14+
15+
##### Deprecated Behavior
16+
17+
The following still works, but the way it behaves is deprecated and will be
18+
changed in a future major version.
819

920
```javascript
1021
var Server = require('karma').Server
@@ -15,25 +26,45 @@ var server = new Server(karmaConfig, function(exitCode) {
1526
})
1627
```
1728

18-
Notice the capital 'S' on `require('karma').Server`.
29+
##### New Behavior
30+
31+
```javascript
32+
const karma = require('karma')
33+
const parseConfig = karma.config.parseConfig
34+
const Server = karma.Server
35+
36+
parseConfig(
37+
null,
38+
{ port: 9876 },
39+
{ promiseConfig: true, throwErrors: true }
40+
).then(
41+
(karmaConfig) => {
42+
const server = new Server(karmaConfig, function doneCallback(exitCode) {
43+
console.log('Karma has exited with ' + exitCode)
44+
process.exit(exitCode)
45+
})
46+
},
47+
(rejectReason) => { /* respond to the rejection reason error */ }
48+
);
49+
```
1950

20-
### **server.start()**
51+
### `server.start()`
2152

2253
Equivalent of `karma start`.
2354

2455
```javascript
2556
server.start()
2657
```
2758

28-
### **server.refreshFiles()**
59+
### `server.refreshFiles()`
2960

3061
Trigger a file list refresh. Returns a promise.
3162

3263
```javascript
3364
server.refreshFiles()
3465
```
3566

36-
### **server.refreshFile(path)**
67+
### `server.refreshFile(path)`
3768

3869
Trigger a file refresh. Returns a promise.
3970

@@ -117,10 +148,19 @@ This event gets triggered whenever all the browsers, which belong to a test run,
117148

118149
## karma.runner
119150

120-
### **runner.run(options, [callback=process.exit])**
151+
### `runner.run(options, [callback=process.exit])`
152+
153+
- **Returns:** `EventEmitter`
121154

122155
The equivalent of `karma run`.
123156

157+
#### Usage
158+
159+
##### Deprecated Behavior
160+
161+
The following still works, but the way it behaves is deprecated and will be
162+
changed in a future major version.
163+
124164
```javascript
125165
var runner = require('karma').runner
126166
runner.run({port: 9876}, function(exitCode) {
@@ -129,6 +169,35 @@ runner.run({port: 9876}, function(exitCode) {
129169
})
130170
```
131171

172+
##### New Behavior
173+
174+
```javascript
175+
const karma = require('karma')
176+
177+
karma.config.parseConfig(
178+
null,
179+
{ port: 9876 },
180+
{ promiseConfig: true, throwErrors: true }
181+
).then(
182+
(karmaConfig) => {
183+
karma.runner.run(karmaConfig, function doneCallback(exitCode, possibleErrorCode) {
184+
console.log('Karma has exited with ' + exitCode)
185+
process.exit(exitCode)
186+
})
187+
},
188+
(rejectReason) => { /* respond to the rejection reason error */ }
189+
);
190+
```
191+
192+
#### `callback` argument
193+
194+
The callback receives the exit code as the first argument.
195+
196+
If there is an error, the error code will be provided as the second parameter to
197+
the error callback.
198+
199+
#### runner Events
200+
132201
`runner.run()` returns an `EventEmitter` which emits a `progress` event passing
133202
the reporter output as a `Buffer` object.
134203

@@ -142,9 +211,17 @@ runner.run({port: 9876}).on('progress', function(data) {
142211

143212
## karma.stopper
144213

145-
### **stopper.stop(options, [callback=process.exit])**
214+
### `stopper.stop(options, [callback=process.exit])`
215+
216+
This function will signal a running server to stop. The equivalent of
217+
`karma stop`.
218+
219+
#### Usage
220+
221+
##### Deprecated Behavior
146222

147-
This function will signal a running server to stop. The equivalent of `karma stop`.
223+
The following still works, but the way it behaves is deprecated and will be
224+
changed in a future major version.
148225

149226
```javascript
150227
var stopper = require('karma').stopper
@@ -156,78 +233,228 @@ stopper.stop({port: 9876}, function(exitCode) {
156233
})
157234
```
158235

159-
## karma.config.parseConfig([configFilePath], [cliOptions])
236+
##### New Behavior
237+
238+
```javascript
239+
const karma = require('karma')
240+
241+
karma.config.parseConfig(
242+
null,
243+
{ port: 9876 },
244+
{ promiseConfig: true, throwErrors: true }
245+
).then(
246+
(karmaConfig) => {
247+
karma.stopper.stop(karmaConfig, function doneCallback(exitCode, possibleErrorCode) {
248+
if (exitCode === 0) {
249+
console.log('Server stop as initiated')
250+
}
251+
process.exit(exitCode)
252+
})
253+
},
254+
(rejectReason) => { /* respond to the rejection reason error */ }
255+
);
256+
```
257+
258+
#### `callback` argument
259+
260+
The callback receives the exit code as the first argument.
261+
262+
If there is an error, the error code will be provided as the second parameter to
263+
the error callback.
264+
265+
## karma.config
266+
267+
### `config.parseConfig([configFilePath], [cliOptions], [parseOptions])`
160268

161269
This function will load given config file and returns a filled config object.
162270
This can be useful if you want to integrate karma into another tool and want to load
163-
the karma config while honoring the karma defaults. For example, the [stryker-karma-runner](https://github.com/stryker-mutator/stryker-karma-runner)
164-
uses this to load your karma configuration and use that in the stryker configuration.
271+
the karma config while honoring the karma defaults.
272+
273+
#### Usage
274+
275+
##### Deprecated Behavior
276+
277+
The following still works, but the way it behaves is deprecated and will be
278+
changed in a future major version.
279+
280+
```javascript
281+
const cfg = require('karma').config;
282+
const path = require('path');
283+
// Read karma.conf.js, but override port with 1337
284+
const karmaConfig = cfg.parseConfig(
285+
path.resolve('./karma.conf.js'),
286+
{ port: 1337 }
287+
);
288+
```
289+
290+
The new behavior in the future will involve throwing exceptions instead of
291+
exiting the process and aynchronous config files will be supported through the
292+
use of promises.
293+
294+
##### New Behavior
165295

166296
```javascript
167297
const cfg = require('karma').config;
168298
const path = require('path');
169299
// Read karma.conf.js, but override port with 1337
170-
const karmaConfig = cfg.parseConfig(path.resolve('./karma.conf.js'), { port: 1337 } );
300+
cfg.parseConfig(
301+
path.resolve('./karma.conf.js'),
302+
{ port: 1337 },
303+
{ promiseConfig: true, throwErrors: true }
304+
).then(
305+
(karmaConfig) => { /* use the config with the public API */ },
306+
(rejectReason) => { /* respond to the rejection reason error */ }
307+
);
171308
```
172309

173-
## karma.constants
174310

175-
### **constants.VERSION**
311+
#### `configFilePath` argument
312+
313+
- **Type:** String | `null` | `undefined`
314+
- **Default Value:** `undefined`
315+
316+
A string representing a file system path pointing to the config file whose
317+
default export is a function that will be used to set Karma configuration
318+
options. This function will be passed an instance of the `Config` class as its
319+
first argument. If this option is not provided, then only the options provided
320+
by the `cliOptions` argument will be set.
321+
322+
- JavaScript must use CommonJS modules.
323+
- ECMAScript modules are not currently supported by Karma when using
324+
JavaScript.
325+
- Other formats, such as TypeScript, may support ECMAScript modules.
326+
327+
328+
#### `cliOptions` argument
329+
330+
- **Type:** Object | `null` | `undefined`
331+
- **Default Value:** `undefined`
332+
333+
An object whose values will take priority over options set in the config file.
334+
The config object passed to function exported by the config file will already
335+
have these options applied. Any changes the config file makes to these options
336+
will effectively be ignored in the final configuration.
337+
338+
Supports all the same options as the config file and is applied using the same
339+
`config.set()` method.
340+
341+
The expected source of this argument is parsed command line options, but
342+
programatic users may construct this object or leave it out entirely.
343+
344+
345+
#### `parseOptions` argument
346+
347+
- **Type:** Object | `null` | `undefined`
348+
- **Default Value:** `undefined`
349+
350+
`parseOptions` is an object whose properties are configuration options that
351+
allow additional control over parsing and opt-in access to new behaviors or
352+
features.
353+
354+
These options are only related to parsing configuration files and object and are
355+
not related to the configuration of Karma itself.
356+
357+
358+
##### `parseOptions.promiseConfig` option
359+
360+
- **Type:** Boolean
361+
- **Default Value:** `false`
362+
363+
When `parseOptions.promiseConfig === true`, then `parseConfig` will return a
364+
promise instead of a configuration object.
365+
366+
When this option is `true`, then the function exported by the config file may
367+
return a promise. The resolution of that promise indicates that all asynchronous
368+
activity has been completed. Internally, the resolved/fulfilled value is
369+
ignored. As with synchronous usage, all changes to the config object must be
370+
done with the `config.set()` method.
371+
372+
If the function exported by the config file does not return a promise, then
373+
parsing is completed and an immediately fulfilled promise is returned.
374+
375+
Whether the function exported by the config file returns a promise or not, the
376+
promise returned by `parseConfig()` will resolve with a parsed configuration
377+
object, an instance of the `Config` class, as the value.
378+
379+
_**In most cases, `parseOptions.throwErrors = true` should also be set. This
380+
disables process exiting and allows errors to result in rejected promises.**_
381+
382+
383+
##### `parseOptions.throwErrors` option
384+
385+
- **Type:** Boolean
386+
- **Default Value:** `false`
387+
388+
In the past, `parseConfig()` would call `process.exit(exitCode)` when it
389+
encountered a critical failure. This meant that your own code had no way of
390+
responding to failures before the Node.js process exited.
391+
392+
By passing `parseOptions.throwErrors = true`, `parseConfig()` will disable
393+
process exiting.
394+
395+
For synchronous usage, it will throw an exception instead of exiting the
396+
process. Your code can then catch the exception and respond how ever it needs
397+
to.
398+
399+
If the asynchronous API (`parseOptions.promiseConfig = true`) is being used,
400+
then `parseOptions.throwErrors = true` allows the promise to be rejected
401+
instead of exiting the process.
402+
403+
404+
## `karma.constants`
405+
406+
### `constants.VERSION`
176407

177408
The current version of karma
178409

179-
### **constants.DEFAULT_PORT**
410+
### `constants.DEFAULT_PORT`
180411

181412
The default port used for the karma server
182413

183-
### **constants.DEFAULT_HOSTNAME**
414+
### `constants.DEFAULT_HOSTNAME`
184415

185416
The default hostname used for the karma server
186417

187-
### **constants.DEFAULT_LISTEN_ADDR**
418+
### `constants.DEFAULT_LISTEN_ADDR`
188419

189420
The default address use for the karma server to listen on
190421

191-
### **constants.LOG_DISABLE**
422+
### `constants.LOG_DISABLE`
192423

193424
The value for disabling logs
194425

195-
### **constants.LOG_ERROR**
426+
### `constants.LOG_ERROR`
196427

197428
The value for the log `error` level
198429

199-
### **constants.LOG_WARN**
430+
### `constants.LOG_WARN`
200431

201432
The value for the log `warn` level
202433

203-
### **constants.LOG_INFO**
434+
### `constants.LOG_INFO`
204435

205436
The value for the log `info` level
206437

207-
### **constants.LOG_DEBUG**
438+
### `constants.LOG_DEBUG`
208439

209440
The value for the log `debug` level
210441

211-
### **constants.LOG_PRIORITIES**
442+
### `constants.LOG_PRIORITIES`
212443

213444
An array of log levels in descending order, i.e. `LOG_DISABLE`, `LOG_ERROR`, `LOG_WARN`, `LOG_INFO`, and `LOG_DEBUG`
214445

215-
### **constants.COLOR_PATTERN**
446+
### `constants.COLOR_PATTERN`
216447

217448
The default color pattern for log output
218449

219-
### **constants.NO_COLOR_PATTERN**
450+
### `constants.NO_COLOR_PATTERN`
220451

221452
The default pattern for log output without color
222453

223-
### **constants.CONSOLE_APPENDER**
454+
### `constants.CONSOLE_APPENDER`
224455

225456
The default console appender
226457

227-
### **constants.EXIT_CODE**
458+
### `constants.EXIT_CODE`
228459

229460
The exit code
230-
231-
## Callback function notes
232-
233-
- If there is an error, the error code will be provided as the second parameter to the error callback.

0 commit comments

Comments
 (0)