UNPKG

3.77 kBJavaScriptView Raw
1(function (factory) {
2 if (typeof module === "object" && typeof module.exports === "object") {
3 var v = factory(require, exports);
4 if (v !== undefined) module.exports = v;
5 }
6 else if (typeof define === "function" && define.amd) {
7 define(["require", "exports", "tslib", "@theintern/common", "../Suite", "../Test"], factory);
8 }
9})(function (require, exports) {
10 "use strict";
11 Object.defineProperty(exports, "__esModule", { value: true });
12 exports.getInterface = exports.afterEach = exports.beforeEach = exports.after = exports.before = exports.test = exports.suite = void 0;
13 var tslib_1 = require("tslib");
14 var common_1 = require("@theintern/common");
15 var Suite_1 = tslib_1.__importDefault(require("../Suite"));
16 var Test_1 = tslib_1.__importDefault(require("../Test"));
17 function suite(name, factory) {
18 return _suite(common_1.global.intern, name, factory);
19 }
20 exports.suite = suite;
21 function test(name, test) {
22 if (!currentSuite) {
23 throw new Error('A test must be declared within a suite');
24 }
25 currentSuite.add(new Test_1.default({ name: name, test: test }));
26 }
27 exports.test = test;
28 function before(fn) {
29 if (!currentSuite) {
30 throw new Error('A suite lifecycle method must be declared within a suite');
31 }
32 aspect(currentSuite, 'before', fn);
33 }
34 exports.before = before;
35 function after(fn) {
36 if (!currentSuite) {
37 throw new Error('A suite lifecycle method must be declared within a suite');
38 }
39 aspect(currentSuite, 'after', fn);
40 }
41 exports.after = after;
42 function beforeEach(fn) {
43 if (!currentSuite) {
44 throw new Error('A suite lifecycle method must be declared within a suite');
45 }
46 aspect(currentSuite, 'beforeEach', fn);
47 }
48 exports.beforeEach = beforeEach;
49 function afterEach(fn) {
50 if (!currentSuite) {
51 throw new Error('A suite lifecycle method must be declared within a suite');
52 }
53 aspect(currentSuite, 'afterEach', fn);
54 }
55 exports.afterEach = afterEach;
56 function getInterface(executor) {
57 return {
58 suite: function (name, factory) {
59 return _suite(executor, name, factory);
60 },
61 test: test,
62 before: before,
63 after: after,
64 beforeEach: beforeEach,
65 afterEach: afterEach
66 };
67 }
68 exports.getInterface = getInterface;
69 var currentSuite;
70 function registerSuite(name, factory) {
71 var parent = currentSuite;
72 currentSuite = new Suite_1.default({ name: name, parent: parent });
73 parent.add(currentSuite);
74 factory(currentSuite);
75 currentSuite = parent;
76 }
77 function _suite(executor, name, factory) {
78 if (!currentSuite) {
79 executor.addSuite(function (parent) {
80 currentSuite = parent;
81 registerSuite(name, factory);
82 currentSuite = null;
83 });
84 }
85 else {
86 registerSuite(name, factory);
87 }
88 }
89 function aspect(suite, method, callback) {
90 var originalMethod = suite[method];
91 suite[method] = function () {
92 var args = [];
93 for (var _i = 0; _i < arguments.length; _i++) {
94 args[_i] = arguments[_i];
95 }
96 var originalReturn = originalMethod
97 ? originalMethod.apply(suite, args)
98 : undefined;
99 return Promise.resolve(originalReturn).then(function () {
100 return callback.apply(currentSuite, args);
101 });
102 };
103 }
104});
105//# sourceMappingURL=tdd.js.map
\No newline at end of file