qunit.js 2.0 KB

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