qunit.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict';
  2. /**
  3. * Module dependencies.
  4. */
  5. var Test = require('../test');
  6. /**
  7. * QUnit-style interface:
  8. *
  9. * suite('Array');
  10. *
  11. * test('#length', function() {
  12. * var arr = [1,2,3];
  13. * ok(arr.length == 3);
  14. * });
  15. *
  16. * test('#indexOf()', function() {
  17. * var arr = [1,2,3];
  18. * ok(arr.indexOf(1) == 0);
  19. * ok(arr.indexOf(2) == 1);
  20. * ok(arr.indexOf(3) == 2);
  21. * });
  22. *
  23. * suite('String');
  24. *
  25. * test('#length', function() {
  26. * ok('foo'.length == 3);
  27. * });
  28. *
  29. * @param {Suite} suite Root suite.
  30. */
  31. module.exports = function (suite) {
  32. var suites = [suite];
  33. suite.on('pre-require', function (context, file, mocha) {
  34. var common = require('./common')(suites, context, mocha);
  35. context.before = common.before;
  36. context.after = common.after;
  37. context.beforeEach = common.beforeEach;
  38. context.afterEach = common.afterEach;
  39. context.run = mocha.options.delay && common.runWithSuite(suite);
  40. /**
  41. * Describe a "suite" with the given `title`.
  42. */
  43. context.suite = function (title) {
  44. if (suites.length > 1) {
  45. suites.shift();
  46. }
  47. return common.suite.create({
  48. title: title,
  49. file: file,
  50. fn: false
  51. });
  52. };
  53. /**
  54. * Exclusive Suite.
  55. */
  56. context.suite.only = function (title) {
  57. if (suites.length > 1) {
  58. suites.shift();
  59. }
  60. return common.suite.only({
  61. title: title,
  62. file: file,
  63. fn: false
  64. });
  65. };
  66. /**
  67. * Describe a specification or test-case
  68. * with the given `title` and callback `fn`
  69. * acting as a thunk.
  70. */
  71. context.test = function (title, fn) {
  72. var test = new Test(title, fn);
  73. test.file = file;
  74. suites[0].addTest(test);
  75. return test;
  76. };
  77. /**
  78. * Exclusive test-case.
  79. */
  80. context.test.only = function (title, fn) {
  81. return common.test.only(mocha, context.test(title, fn));
  82. };
  83. context.test.skip = common.test.skip;
  84. context.test.retries = common.test.retries;
  85. });
  86. };