UNPKG

3.96 kBJavaScriptView Raw
1'use strict';
2
3const log = require('npmlog');
4const Config = require('./config');
5const EventEmitter = require('events').EventEmitter;
6const fs = require('fs');
7const App = require('./app');
8const Server = require('./server');
9
10/*
11 CLI-level options:
12
13 file: [String] configuration file (testem.json, .testem.json, testem.yml, .testem.yml)
14 host: [String] server host to use (localhost)
15 port: [Number] server port to use (7357)
16 launch: [Array] list of launchers to use for current runs (defaults to current mode)
17 skip: [Array] list of launchers to skip
18 debug: [Boolean] debug mode (false)
19 test_page: [String|Array] path (or list of paths) to the page (or pages) to use to run tests
20 growl: [Boolean] enables growl / native notifications (false)
21
22 Config-level options:
23
24 launchers: [Object] List of custom launchers
25 launch_in_dev: [Array] list of launchers to use for dev runs
26 launch_in_ci: [Array] list of launchers to use for CI runs
27 timeout: [Number] timeout for a browser
28 framework: [String] test framework to use
29 url: [String] url server runs at (http://{host}:{port}/)
30 src_files: [Array] list of files or file patterns to use
31 src_files_ignore: [Array] list of files or file patterns to exclude from usage
32 serve_files: [Array] list of files or file patterns to inject into test playground (defaults to src_files)
33 watch_files: [Array] list of files or file patterns to watch changes of (defaults to src_files)
34 css_files: [Array] additional stylesheets to include
35 cwd: [Path] directory to use as root
36 config_dir: [Path] directory to use as root for resolving configs, if different than cwd
37 query_params: [Object] Object map defining query params to add to the test page. Can be query string also.
38 parallel: [Number] max number of parallel runners (1)
39 routes: [Object] overrides for assets paths
40 fail_on_zero_tests: [Boolean] whether process should exit with error status when no tests found
41 unsafe_file_serving: [Boolean] allow serving directories that are not in your CWD (false)
42
43 Available hooks:
44
45 on_start: Runs on suite startup
46 before_tests: Runs before every run of tests
47 after_tests: Runs after every run of tests
48 on_exit: Runs before suite exits
49*/
50
51class Api {
52 setup(mode, finalizer) {
53 this.config = new Config(mode, this.options);
54 this.config.setDefaultOptions(this.defaultOptions);
55 this.configureLogging();
56 log.info('Test\'em starting..');
57 this.config.read(() => {
58 this.app = new App(this.config, finalizer);
59 this.app.start();
60 });
61 }
62
63 configureLogging() {
64 let debug = this.config.get('debug');
65 if (debug) {
66 log.stream = fs.createWriteStream(debug);
67 } else {
68 let fakeStream = new EventEmitter();
69 fakeStream.write = () => {};
70 log.stream = fakeStream;
71 }
72 }
73
74 startDev(options, finalizer) {
75 this.options = options;
76 this.setup('dev', finalizer);
77 }
78
79 restart() {
80 this.app.triggerRun('Api: restart');
81 }
82
83 startCI(options, finalizer) {
84 this.options = options;
85 this.setup('ci', finalizer);
86 }
87
88 startServer(options) {
89 this.options = options;
90 this.config = new Config('server', this.options);
91 this.config.setDefaultOptions(this.defaultOptions);
92
93 this.config.read(() => {
94 let server = new Server(this.config);
95 server.start();
96 server.on('server-start', () => {
97 console.log('Open ' + this.config.get('url') + ' in a browser to connect.');
98 });
99 });
100 }
101
102 setDefaultOptions(defaultOptions) {
103 this.defaultOptions = defaultOptions;
104 }
105}
106
107module.exports = Api;