qunit.js 2.2 KB

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