UNPKG

2.48 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * Module dependencies.
5 */
6
7var Test = require('../test');
8
9/**
10 * BDD-style interface:
11 *
12 * describe('Array', function() {
13 * describe('#indexOf()', function() {
14 * it('should return -1 when not present', function() {
15 * // ...
16 * });
17 *
18 * it('should return the index when present', function() {
19 * // ...
20 * });
21 * });
22 * });
23 *
24 * @param {Suite} suite Root suite.
25 */
26module.exports = function (suite) {
27 var suites = [suite];
28
29 suite.on('pre-require', function (context, file, mocha) {
30 var common = require('./common')(suites, context, mocha);
31
32 context.before = common.before;
33 context.after = common.after;
34 context.beforeEach = common.beforeEach;
35 context.afterEach = common.afterEach;
36 context.run = mocha.options.delay && common.runWithSuite(suite);
37 /**
38 * Describe a "suite" with the given `title`
39 * and callback `fn` containing nested suites
40 * and/or tests.
41 */
42
43 context.describe = context.context = function (title, fn) {
44 return common.suite.create({
45 title: title,
46 file: file,
47 fn: fn
48 });
49 };
50
51 /**
52 * Pending describe.
53 */
54
55 context.xdescribe = context.xcontext = context.describe.skip = function (title, fn) {
56 return common.suite.skip({
57 title: title,
58 file: file,
59 fn: fn
60 });
61 };
62
63 /**
64 * Exclusive suite.
65 */
66
67 context.describe.only = function (title, fn) {
68 return common.suite.only({
69 title: title,
70 file: file,
71 fn: fn
72 });
73 };
74
75 /**
76 * Describe a specification or test-case
77 * with the given `title` and callback `fn`
78 * acting as a thunk.
79 */
80
81 context.it = context.specify = function (title, fn) {
82 var suite = suites[0];
83 if (suite.isPending()) {
84 fn = null;
85 }
86 var test = new Test(title, fn);
87 test.file = file;
88 suite.addTest(test);
89 return test;
90 };
91
92 /**
93 * Exclusive test-case.
94 */
95
96 context.it.only = function (title, fn) {
97 return common.test.only(mocha, context.it(title, fn));
98 };
99
100 /**
101 * Pending test case.
102 */
103
104 context.xit = context.xspecify = context.it.skip = function (title) {
105 context.it(title);
106 };
107
108 /**
109 * Number of attempts to retry.
110 */
111 context.it.retries = function (n) {
112 context.retries(n);
113 };
114 });
115};