1 | ;
|
2 |
|
3 | var Suite = require('../suite');
|
4 |
|
5 | /**
|
6 | * Functions common to more than one interface.
|
7 | *
|
8 | * @param {Suite[]} suites
|
9 | * @param {Context} context
|
10 | * @param {Mocha} mocha
|
11 | * @return {Object} An object containing common functions.
|
12 | */
|
13 | module.exports = function (suites, context, mocha) {
|
14 | return {
|
15 | /**
|
16 | * This is only present if flag --delay is passed into Mocha. It triggers
|
17 | * root suite execution.
|
18 | *
|
19 | * @param {Suite} suite The root suite.
|
20 | * @return {Function} A function which runs the root suite
|
21 | */
|
22 | runWithSuite: function runWithSuite (suite) {
|
23 | return function run () {
|
24 | suite.run();
|
25 | };
|
26 | },
|
27 |
|
28 | /**
|
29 | * Execute before running tests.
|
30 | *
|
31 | * @param {string} name
|
32 | * @param {Function} fn
|
33 | */
|
34 | before: function (name, fn) {
|
35 | suites[0].beforeAll(name, fn);
|
36 | },
|
37 |
|
38 | /**
|
39 | * Execute after running tests.
|
40 | *
|
41 | * @param {string} name
|
42 | * @param {Function} fn
|
43 | */
|
44 | after: function (name, fn) {
|
45 | suites[0].afterAll(name, fn);
|
46 | },
|
47 |
|
48 | /**
|
49 | * Execute before each test case.
|
50 | *
|
51 | * @param {string} name
|
52 | * @param {Function} fn
|
53 | */
|
54 | beforeEach: function (name, fn) {
|
55 | suites[0].beforeEach(name, fn);
|
56 | },
|
57 |
|
58 | /**
|
59 | * Execute after each test case.
|
60 | *
|
61 | * @param {string} name
|
62 | * @param {Function} fn
|
63 | */
|
64 | afterEach: function (name, fn) {
|
65 | suites[0].afterEach(name, fn);
|
66 | },
|
67 |
|
68 | suite: {
|
69 | /**
|
70 | * Create an exclusive Suite; convenience function
|
71 | * See docstring for create() below.
|
72 | *
|
73 | * @param {Object} opts
|
74 | * @returns {Suite}
|
75 | */
|
76 | only: function only (opts) {
|
77 | mocha.options.hasOnly = true;
|
78 | opts.isOnly = true;
|
79 | return this.create(opts);
|
80 | },
|
81 |
|
82 | /**
|
83 | * Create a Suite, but skip it; convenience function
|
84 | * See docstring for create() below.
|
85 | *
|
86 | * @param {Object} opts
|
87 | * @returns {Suite}
|
88 | */
|
89 | skip: function skip (opts) {
|
90 | opts.pending = true;
|
91 | return this.create(opts);
|
92 | },
|
93 |
|
94 | /**
|
95 | * Creates a suite.
|
96 | * @param {Object} opts Options
|
97 | * @param {string} opts.title Title of Suite
|
98 | * @param {Function} [opts.fn] Suite Function (not always applicable)
|
99 | * @param {boolean} [opts.pending] Is Suite pending?
|
100 | * @param {string} [opts.file] Filepath where this Suite resides
|
101 | * @param {boolean} [opts.isOnly] Is Suite exclusive?
|
102 | * @returns {Suite}
|
103 | */
|
104 | create: function create (opts) {
|
105 | var suite = Suite.create(suites[0], opts.title);
|
106 | suite.pending = Boolean(opts.pending);
|
107 | suite.file = opts.file;
|
108 | suites.unshift(suite);
|
109 | if (opts.isOnly) {
|
110 | suite.parent._onlySuites = suite.parent._onlySuites.concat(suite);
|
111 | mocha.options.hasOnly = true;
|
112 | }
|
113 | if (typeof opts.fn === 'function') {
|
114 | opts.fn.call(suite);
|
115 | suites.shift();
|
116 | } else if (typeof opts.fn === 'undefined' && !suite.pending) {
|
117 | throw new Error('Suite "' + suite.fullTitle() + '" was defined but no callback was supplied. Supply a callback or explicitly skip the suite.');
|
118 | }
|
119 |
|
120 | return suite;
|
121 | }
|
122 | },
|
123 |
|
124 | test: {
|
125 |
|
126 | /**
|
127 | * Exclusive test-case.
|
128 | *
|
129 | * @param {Object} mocha
|
130 | * @param {Function} test
|
131 | * @returns {*}
|
132 | */
|
133 | only: function (mocha, test) {
|
134 | test.parent._onlyTests = test.parent._onlyTests.concat(test);
|
135 | mocha.options.hasOnly = true;
|
136 | return test;
|
137 | },
|
138 |
|
139 | /**
|
140 | * Pending test case.
|
141 | *
|
142 | * @param {string} title
|
143 | */
|
144 | skip: function (title) {
|
145 | context.test(title);
|
146 | },
|
147 |
|
148 | /**
|
149 | * Number of retry attempts
|
150 | *
|
151 | * @param {number} n
|
152 | */
|
153 | retries: function (n) {
|
154 | context.retries(n);
|
155 | }
|
156 | }
|
157 | };
|
158 | };
|