exports.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict';
  2. var Suite = require('../suite');
  3. var Test = require('../test');
  4. /**
  5. * Exports-style (as Node.js module) interface:
  6. *
  7. * exports.Array = {
  8. * '#indexOf()': {
  9. * 'should return -1 when the value is not present': function() {
  10. *
  11. * },
  12. *
  13. * 'should return the correct index when the value is present': function() {
  14. *
  15. * }
  16. * }
  17. * };
  18. *
  19. * @param {Suite} suite Root suite.
  20. */
  21. module.exports = function(suite) {
  22. var suites = [suite];
  23. suite.on(Suite.constants.EVENT_FILE_REQUIRE, visit);
  24. function visit(obj, file) {
  25. var suite;
  26. for (var key in obj) {
  27. if (typeof obj[key] === 'function') {
  28. var fn = obj[key];
  29. switch (key) {
  30. case 'before':
  31. suites[0].beforeAll(fn);
  32. break;
  33. case 'after':
  34. suites[0].afterAll(fn);
  35. break;
  36. case 'beforeEach':
  37. suites[0].beforeEach(fn);
  38. break;
  39. case 'afterEach':
  40. suites[0].afterEach(fn);
  41. break;
  42. default:
  43. var test = new Test(key, fn);
  44. test.file = file;
  45. suites[0].addTest(test);
  46. }
  47. } else {
  48. suite = Suite.create(suites[0], key);
  49. suites.unshift(suite);
  50. visit(obj[key], file);
  51. suites.shift();
  52. }
  53. }
  54. }
  55. };
  56. module.exports.description = 'Node.js module ("exports") style';