1 | const chai = require('chai');
|
2 | const deepAssign = require('deep-assign');
|
3 | const fs = require('fs');
|
4 | const path = require('path');
|
5 | const url = require('url');
|
6 |
|
7 | const MochaChrome = require('../index');
|
8 | const expect = chai.expect;
|
9 |
|
10 | function test (options) {
|
11 | const url = 'file://' + path.join(__dirname, '/html', options.file + '.html');
|
12 |
|
13 | options = deepAssign(options = {
|
14 | url,
|
15 | mocha: { useColors: false },
|
16 | ignoreConsole: true,
|
17 | ignoreExceptions: true,
|
18 | ignoreResourceErrors: true
|
19 | }, options);
|
20 |
|
21 | const runner = new MochaChrome(options);
|
22 | const result = new Promise((resolve, reject) => {
|
23 | runner.on('ended', stats => {
|
24 | resolve(stats);
|
25 | });
|
26 |
|
27 | runner.on('failure', message => {
|
28 | reject(message);
|
29 | });
|
30 | });
|
31 |
|
32 | (async function () {
|
33 | await runner.connect();
|
34 | await runner.run();
|
35 | })();
|
36 |
|
37 | return result;
|
38 | }
|
39 |
|
40 | describe('MochaChrome', () => {
|
41 |
|
42 | it('fails if mocha isn\'t loaded', () => {
|
43 | return test({ file: 'no-mocha' }).catch(message => {
|
44 | expect(message).to.equal('mocha was not found in the page within 1000ms of the page loading.');
|
45 | });
|
46 | });
|
47 |
|
48 | it('fails if mocha.run isn\'t called', () => {
|
49 | return test({ file: 'no-run' }).catch(message => {
|
50 | expect(message).to.equal('mocha.run() was not called within 1000ms of the page loading.');
|
51 | });
|
52 | });
|
53 |
|
54 | it('runs a test', () => {
|
55 | return test({ file: 'test' }).then(({passes, failures}) => {
|
56 | expect(passes).to.equal(1);
|
57 | expect(failures).to.equal(0);
|
58 | });
|
59 | });
|
60 |
|
61 | it('reports a failure', () => {
|
62 | return test({ file: 'fail' }).then(({passes, failures}) => {
|
63 | expect(failures).to.equal(1);
|
64 | });
|
65 | });
|
66 |
|
67 | it('allows runner modification', () => {
|
68 | return test({ file: 'runner-mod' }).then(({passes, failures}) => {
|
69 | expect(passes).to.equal(1);
|
70 | expect(failures).to.equal(1);
|
71 | });
|
72 | });
|
73 |
|
74 | it('supports different reporters', () => {
|
75 | return test({ file: 'reporter',
|
76 | mocha: {
|
77 | reporter: 'xunit'
|
78 | }
|
79 | }).then(({passes, failures}) => {
|
80 | expect(passes).to.equal(1);
|
81 | expect(failures).to.equal(0);
|
82 | });
|
83 | });
|
84 |
|
85 | it('supports mixed tests', () => {
|
86 | return test({ file: 'mixed',
|
87 | mocha: {
|
88 | reporter: 'dot'
|
89 | }
|
90 | }).then(({passes, failures}) => {
|
91 | expect(passes).to.equal(6);
|
92 | expect(failures).to.equal(6);
|
93 | });
|
94 | });
|
95 |
|
96 | it('reports async failures', () => {
|
97 | return test({ file: 'fail-async' }).then(({passes, failures}) => {
|
98 | expect(failures).to.equal(3);
|
99 | });
|
100 | });
|
101 |
|
102 | });
|