source: Dev/trunk/node_modules/mocha/lib/interfaces/qunit.js @ 484

Last change on this file since 484 was 484, checked in by hendrikvanantwerpen, 11 years ago

Commit node_modules, to make checkouts and builds more deterministic.

File size: 2.3 KB
Line 
1
2/**
3 * Module dependencies.
4 */
5
6var Suite = require('../suite')
7  , Test = require('../test')
8  , utils = require('../utils');
9
10/**
11 * QUnit-style interface:
12 *
13 *     suite('Array');
14 *
15 *     test('#length', function(){
16 *       var arr = [1,2,3];
17 *       ok(arr.length == 3);
18 *     });
19 *
20 *     test('#indexOf()', function(){
21 *       var arr = [1,2,3];
22 *       ok(arr.indexOf(1) == 0);
23 *       ok(arr.indexOf(2) == 1);
24 *       ok(arr.indexOf(3) == 2);
25 *     });
26 *
27 *     suite('String');
28 *
29 *     test('#length', function(){
30 *       ok('foo'.length == 3);
31 *     });
32 *
33 */
34
35module.exports = function(suite){
36  var suites = [suite];
37
38  suite.on('pre-require', function(context, file, mocha){
39
40    /**
41     * Execute before running tests.
42     */
43
44    context.before = function(fn){
45      suites[0].beforeAll(fn);
46    };
47
48    /**
49     * Execute after running tests.
50     */
51
52    context.after = function(fn){
53      suites[0].afterAll(fn);
54    };
55
56    /**
57     * Execute before each test case.
58     */
59
60    context.beforeEach = function(fn){
61      suites[0].beforeEach(fn);
62    };
63
64    /**
65     * Execute after each test case.
66     */
67
68    context.afterEach = function(fn){
69      suites[0].afterEach(fn);
70    };
71
72    /**
73     * Describe a "suite" with the given `title`.
74     */
75
76    context.suite = function(title){
77      if (suites.length > 1) suites.shift();
78      var suite = Suite.create(suites[0], title);
79      suites.unshift(suite);
80      return suite;
81    };
82
83    /**
84     * Exclusive test-case.
85     */
86
87    context.suite.only = function(title, fn){
88      var suite = context.suite(title, fn);
89      mocha.grep(suite.fullTitle());
90    };
91
92    /**
93     * Describe a specification or test-case
94     * with the given `title` and callback `fn`
95     * acting as a thunk.
96     */
97
98    context.test = function(title, fn){
99      var test = new Test(title, fn);
100      suites[0].addTest(test);
101      return test;
102    };
103
104    /**
105     * Exclusive test-case.
106     */
107
108    context.test.only = function(title, fn){
109      var test = context.test(title, fn);
110      var reString = '^' + utils.escapeRegexp(test.fullTitle()) + '$';
111      mocha.grep(new RegExp(reString));
112    };
113
114    /**
115     * Pending test case.
116     */
117
118    context.test.skip = function(title){
119      context.test(title);
120    };
121  });
122};
Note: See TracBrowser for help on using the repository browser.