qunit.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Module dependencies.
  3. */
  4. var Suite = require('../suite');
  5. var Test = require('../test');
  6. var escapeRe = require('escape-string-regexp');
  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. * @param {Suite} suite Root suite.
  31. */
  32. module.exports = function(suite) {
  33. var suites = [suite];
  34. suite.on('pre-require', function(context, file, mocha) {
  35. var common = require('./common')(suites, context);
  36. context.before = common.before;
  37. context.after = common.after;
  38. context.beforeEach = common.beforeEach;
  39. context.afterEach = common.afterEach;
  40. context.run = mocha.options.delay && common.runWithSuite(suite);
  41. /**
  42. * Describe a "suite" with the given `title`.
  43. */
  44. context.suite = function(title) {
  45. if (suites.length > 1) {
  46. suites.shift();
  47. }
  48. var suite = Suite.create(suites[0], title);
  49. suite.file = file;
  50. suites.unshift(suite);
  51. return suite;
  52. };
  53. /**
  54. * Exclusive test-case.
  55. */
  56. context.suite.only = function(title, fn) {
  57. var suite = context.suite(title, fn);
  58. mocha.grep(suite.fullTitle());
  59. };
  60. /**
  61. * Describe a specification or test-case
  62. * with the given `title` and callback `fn`
  63. * acting as a thunk.
  64. */
  65. context.test = function(title, fn) {
  66. var test = new Test(title, fn);
  67. test.file = file;
  68. suites[0].addTest(test);
  69. return test;
  70. };
  71. /**
  72. * Exclusive test-case.
  73. */
  74. context.test.only = function(title, fn) {
  75. var test = context.test(title, fn);
  76. var reString = '^' + escapeRe(test.fullTitle()) + '$';
  77. mocha.grep(new RegExp(reString));
  78. };
  79. context.test.skip = common.test.skip;
  80. context.test.retries = common.test.retries;
  81. });
  82. };