qunit.js 2.3 KB

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