1 | # mocha-chrome
|
2 |
|
3 | :coffee: Run Mocha tests using Google Chrome in Node.js
|
4 |
|
5 | [![Build Status](https://travis-ci.org/shellscape/mocha-chrome.svg?branch=master)](https://travis-ci.org/shellscape/mocha-chrome)
|
6 | [![Known Vulnerabilities](https://snyk.io/test/github/shellscape/mocha-chrome/badge.svg)](https://snyk.io/test/github/shellscape/mocha-chrome)
|
7 | [![npm version](https://badge.fury.io/js/mocha-chrome.svg)](https://badge.fury.io/js/mocha-chrome)
|
8 | [![GitHub version](https://badge.fury.io/gh/shellscape%2Fmocha-chrome.svg)](http://badge.fury.io/gh/shellscape%2Fmocha-chrome)
|
9 | [![Open Source Love](https://badges.frapsoft.com/os/mit/mit.svg?v=102)](https://github.com/ellerbrock/open-source-badge/)
|
10 | [![Dependency Status](https://david-dm.org/shellscape/mocha-chrome.svg)](https://david-dm.org/shellscape/mocha-chrome)
|
11 | [![devDependencies Status](https://david-dm.org/shellscape/mocha-chrome/dev-status.svg)](https://david-dm.org/shellscape/mocha-chrome?type=dev)
|
12 |
|
13 | ## Getting Started
|
14 |
|
15 | To begin, you'll need to install this module:
|
16 |
|
17 | ```console
|
18 | $ npm install mocha-chrome --save-dev
|
19 | ```
|
20 |
|
21 | Then you'll need a local npm install of mocha:
|
22 |
|
23 | ```console
|
24 | $ npm install mocha --save-dev
|
25 | ```
|
26 |
|
27 | To run the tests, you'll need an HTML file with some basics:
|
28 |
|
29 | ```html
|
30 | <!doctype>
|
31 | <html>
|
32 | <head>
|
33 | <title>Test</title>
|
34 | <meta charset="utf-8">
|
35 | <link rel="stylesheet" href="../../node_modules/mocha/mocha.css" />
|
36 | <script src="../../node_modules/mocha/mocha.js"></script>
|
37 | <script src="../../node_modules/chai/chai.js"></script>
|
38 | </head>
|
39 | <body>
|
40 | <div id="mocha"></div>
|
41 | <script>
|
42 | expect = chai.expect;
|
43 |
|
44 | // add tests here
|
45 |
|
46 | mocha.run();
|
47 | </script>
|
48 | </body>
|
49 | </html>
|
50 |
|
51 | ```
|
52 |
|
53 | You can then add your tests either through an external script file or
|
54 | inline within a `<script>` tag. Running the tests is easy, either with the CLI
|
55 | binary, or programmatically.
|
56 |
|
57 | ## CLI
|
58 |
|
59 | ```console
|
60 | $ mocha-chrome --help
|
61 |
|
62 | Usage
|
63 | $ mocha-chrome <file.html> [options]
|
64 |
|
65 | Options
|
66 | --mocha A JSON string representing a config object to pass to Mocha
|
67 | --log-level Specify a log level; trace, debug, info, warn, error
|
68 | --no-colors Disable colors in Mocha's output
|
69 | --reporter Specify the Mocha reporter to use
|
70 | --timeout Specify the test startup timeout to use
|
71 |
|
72 | Examples
|
73 | $ mocha-chrome test.html --no-colors
|
74 | $ mocha-chrome test.html --reporter dot
|
75 | $ mocha-chrome test.html --mocha '{"ui":"tdd"}'
|
76 | ```
|
77 |
|
78 | ## Events
|
79 |
|
80 | `mocha-chrome` is technically an event emitter. Due to the asynchronous nature of
|
81 | nearly every interaction with headless Chrome, a simple event bus is used to
|
82 | handle actions from the browser. You have access to those events if running
|
83 | `mocha-chrome` programatically.
|
84 |
|
85 | Example usage can be found in both [test.js](test/test.js) and [bin/mocha-chrome](bin/mocha-chrome).
|
86 |
|
87 | #### `config`
|
88 |
|
89 | Fired to indicate that `mocha-chrome` should configure mocha.
|
90 |
|
91 | #### `ended`
|
92 |
|
93 | Fired when all tests have ended.
|
94 |
|
95 | ##### Parameters
|
96 | `stats` : `object` - A Mocha stats object. eg:
|
97 |
|
98 | ```js
|
99 | {
|
100 | suites: 1,
|
101 | tests: 1,
|
102 | passes: 1,
|
103 | pending: 0,
|
104 | failures: 0,
|
105 | start: '2017-08-03T02:12:02.007Z',
|
106 | end: '2017-08-03T02:12:02.017Z',
|
107 | duration: 10
|
108 | }
|
109 | ```
|
110 |
|
111 | #### `ready`
|
112 |
|
113 | Fired to indicate that the mocha script in the client has been loaded.
|
114 |
|
115 | #### `resourceFailed`
|
116 |
|
117 | Fired when a resource fails to load.
|
118 |
|
119 | ##### Parameters
|
120 | `data` : `object` - An object containing information about the resource. eg:
|
121 |
|
122 | ```js
|
123 | { url, method, reason }
|
124 | ```
|
125 |
|
126 | #### `started`
|
127 |
|
128 | Fired when a resource fails to load.
|
129 |
|
130 | ##### Parameters
|
131 | `tests` : `number` - The number of tests being run.
|
132 |
|
133 | #### `width`
|
134 |
|
135 | Fired to indicate that `mocha-chrome` should inform mocha of the width of
|
136 | the current console/terminal.
|
137 |
|
138 | ## Limitations
|
139 |
|
140 | ### Reporters
|
141 |
|
142 | Reporters are limited to those which don't use `process.stdout.write` to manipulate
|
143 | terminal output. eg. `spec`, `xunit`, etc. Examples of reporters which don't presently
|
144 | produce expected output formatting include `dot` and `nyan`. The cause of this
|
145 | limitation is the lack of a good means to pipe Mocha's built-in `stdout.write`
|
146 | through the Chrome Devtools Protocol to `mocha-chrome`.
|
147 |
|
148 | ### Third-Party Reporters
|
149 |
|
150 | Third party reporters are currently supported, but support is planned. Contributoion
|
151 | on that effort is of course welcome.
|
152 |
|
153 | ## Continuous Integration
|
154 |
|
155 | Please refer to the _"Running it all on Travis CI"_ portion of the guide on [Automated testing with Headless Chrome](https://developers.google.com/web/updates/2017/06/headless-karma-mocha-chai) from
|
156 | Google. Though the article primarily addresses Karma, the setup for Travis CI is
|
157 | identical.
|
158 |
|
159 | ## Testing mocha-chrome
|
160 |
|
161 | ```console
|
162 | $ npm test
|
163 | ```
|
164 |
|
165 | Yep, that's it.
|
166 |
|
167 | ## Contributing
|
168 |
|
169 | We welcome your contributions! Please have a read of [CONTRIBUTING](CONTRIBUTING.md).
|