1 | 'use strict';
|
2 |
|
3 | const { arrayOrPush } = require('./util');
|
4 |
|
5 | const caller = require('./lib/caller');
|
6 | const emitter = require('./lib/emitter');
|
7 | const reporter = require('./reporters/default');
|
8 | const testrunner = require('./lib/testrunner');
|
9 | const expect = require('./lib/expect');
|
10 |
|
11 | const symbols = require('./util/symbols');
|
12 |
|
13 | class Gunner {
|
14 |
|
15 | constructor (name) {
|
16 | this.name = name;
|
17 | this.__suite__ = {
|
18 | tests: [],
|
19 | beforeHooks: {
|
20 | [symbols.Start]: [],
|
21 | [symbols.End]: [],
|
22 | '*': [],
|
23 | },
|
24 | afterHooks: {
|
25 | [symbols.Start]: [],
|
26 | [symbols.End]: [],
|
27 | '*': [],
|
28 | }
|
29 | };
|
30 | }
|
31 |
|
32 | test (description, test) {
|
33 | const existing = (
|
34 | this.__suite__.tests
|
35 | .find(x => x.description === description)
|
36 | );
|
37 | if (existing)
|
38 | throw new Error(`Test '${description}' already exists!`);
|
39 |
|
40 | const unit = {
|
41 | description,
|
42 | type: 'test',
|
43 | run: state => caller(test, state),
|
44 | };
|
45 | this.__suite__.tests.push(unit);
|
46 | return this;
|
47 | }
|
48 |
|
49 | before (description, run, label) {
|
50 | const unit = {
|
51 | description,
|
52 | label,
|
53 | type: 'hook',
|
54 | run: state => caller(run, state),
|
55 | };
|
56 | arrayOrPush(this.__suite__.beforeHooks, description, unit);
|
57 | return this;
|
58 | }
|
59 |
|
60 | after (description, run, label) {
|
61 | const unit = {
|
62 | description,
|
63 | label,
|
64 | type: 'hook',
|
65 | run: state => caller(run, state),
|
66 | };
|
67 | arrayOrPush(this.__suite__.afterHooks, description, unit);
|
68 | return this;
|
69 | }
|
70 |
|
71 | run (options = {}) {
|
72 | (options.reporter || reporter)(emitter, options);
|
73 |
|
74 | emitter.emit('start');
|
75 | return testrunner(this, options)
|
76 | .then(results => {
|
77 | results.success = results.filter(r => r.status === 'ok');
|
78 | results.successPercent = Math.floor(
|
79 | results.success.length/results.length * 100
|
80 | );
|
81 |
|
82 | if((results.successPercent !== 100)
|
83 | && typeof process !== 'undefined')
|
84 | process.exitCode = 1;
|
85 | emitter.emit('test end', results);
|
86 | emitter.emit('end', results);
|
87 |
|
88 | return results;
|
89 | });
|
90 | }
|
91 |
|
92 | }
|
93 |
|
94 | module.exports = Gunner;
|
95 | module.exports.Gunner = Gunner;
|
96 | module.exports.expect = expect;
|
97 | module.exports.expectMany = expect.expectMany;
|
98 | module.exports.Start = symbols.Start;
|
99 | module.exports.End = symbols.End;
|