UNPKG

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