qunit.js 2.1 KB

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