UNPKG

2.1 kBJavaScriptView Raw
1'use strict';
2
3const { arrayOrPush } = require('./util');
4
5const caller = require('./lib/caller');
6const emitter = require('./lib/emitter');
7const reporter = require('./reporters/default');
8const testrunner = require('./lib/testrunner');
9const expect = require('./lib/expect');
10
11const symbols = require('./util/symbols');
12
13class 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
94module.exports = Gunner;
95module.exports.Gunner = Gunner;
96module.exports.expect = expect;
97module.exports.expectMany = expect.expectMany;
98module.exports.Start = symbols.Start;
99module.exports.End = symbols.End;