UNPKG

intern

Version:

Intern. A next-generation code testing stack for JavaScript.

309 lines 11.5 kB
(function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "@dojo/core/async/Task", "@dojo/core/lang", "./Deferred", "./common/util"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Task_1 = require("@dojo/core/async/Task"); var lang_1 = require("@dojo/core/lang"); var Deferred_1 = require("./Deferred"); var util_1 = require("./common/util"); var Test = (function () { function Test(options) { var _this = this; this._hasPassed = false; this._isAsync = false; this._usesRemote = false; if (!options.name || !options.test) { throw new Error('A Test requires a name and a test function'); } ['timeElapsed', 'hasPassed'].forEach(function (property) { var name = property; if (options[name] != null) { _this["_" + name] = options[name]; } delete options[name]; }); lang_1.mixin(this, options); } Object.defineProperty(Test.prototype, "executor", { get: function () { return this.parent && this.parent.executor; }, enumerable: true, configurable: true }); Object.defineProperty(Test.prototype, "hasPassed", { get: function () { return this._hasPassed; }, enumerable: true, configurable: true }); Object.defineProperty(Test.prototype, "id", { get: function () { var name = []; var suiteOrTest = this; do { suiteOrTest.name != null && name.unshift(suiteOrTest.name); } while ((suiteOrTest = suiteOrTest.parent)); return name.join(' - '); }, enumerable: true, configurable: true }); Object.defineProperty(Test.prototype, "isAsync", { get: function () { return this._isAsync; }, enumerable: true, configurable: true }); Object.defineProperty(Test.prototype, "parentId", { get: function () { return this.parent.id; }, enumerable: true, configurable: true }); Object.defineProperty(Test.prototype, "remote", { get: function () { this._usesRemote = true; return this.parent.remote; }, enumerable: true, configurable: true }); Object.defineProperty(Test.prototype, "sessionId", { get: function () { return this.parent.sessionId; }, enumerable: true, configurable: true }); Object.defineProperty(Test.prototype, "timeElapsed", { get: function () { return this._timeElapsed; }, enumerable: true, configurable: true }); Object.defineProperty(Test.prototype, "timeout", { get: function () { if (this._timeout != null) { return this._timeout; } if (this.parent && this.parent.timeout != null) { return this.parent.timeout; } return 30000; }, set: function (value) { this._timeout = value; }, enumerable: true, configurable: true }); Test.prototype.async = function (timeout, numCallsUntilResolution) { this._isAsync = true; if (timeout != null) { this.timeout = timeout; } var remainingCalls = numCallsUntilResolution || 1; var dfd = new Deferred_1.default(); var oldResolve = dfd.resolve; dfd.resolve = function () { --remainingCalls; if (remainingCalls === 0) { oldResolve.apply(this, arguments); } else if (remainingCalls < 0) { throw new Error('resolve called too many times'); } }; this.async = function () { return dfd; }; return dfd; }; Test.prototype.restartTimeout = function (timeout) { var _this = this; if (timeout != null) { this.timeout = timeout; } if (this._runTask) { if (this._timer) { clearTimeout(this._timer); } this._timer = setTimeout(function () { _this._timer = undefined; if (_this._runTask) { var error = new Error("Timeout reached on " + _this.id + "#"); error.name = 'TimeoutError'; _this.error = error; _this._runTask.cancel(); } }, this.timeout); } }; Test.prototype.run = function () { var _this = this; var startTime; if (this._runTask && this._runTask.state === 1) { this._runTask.cancel(); this._runTask = undefined; } if (this._timer) { clearTimeout(this._timer); this._timer = undefined; } this._usesRemote = false; this._hasPassed = false; this._isAsync = false; this._timeElapsed = 0; this._runTask = undefined; this.async = Object.getPrototypeOf(this).async; this.error = undefined; this.skipped = undefined; return this.executor .emit('testStart', this) .then(function () { startTime = Date.now(); }) .then(function () { var result = _this.test(_this); if (_this.isAsync) { if (!Task_1.isThenable(result)) { result = _this.async().promise; } else { result = Task_1.default.race([_this.async().promise, result]); } } if (Task_1.isThenable(result)) { _this._isAsync = true; return new Task_1.default(function (resolve, reject) { _this._runTask = new Task_1.default(function (resolve, reject) { if (Task_1.isThenable(result)) { result.then(function () { resolve(); }, reject); } if (util_1.isTask(result)) { var resultTask_1 = result; resultTask_1 .finally(function () { if (resultTask_1.state === 3) { _this.skipped = 'Canceled'; reject(exports.SKIP); } }) .catch(function (_error) { }); } }, function () { if (util_1.isTask(result)) { result.cancel(); } if (_this.error) { reject(_this.error); } }).then(function () { resolve(); }, reject); _this.restartTimeout(); }); } }) .finally(function () { if (_this._runTask && _this._runTask.state === 1) { _this._runTask.cancel(); } _this._runTask = undefined; _this._timeElapsed = Date.now() - startTime; if (_this._timer) { clearTimeout(_this._timer); _this._timer = undefined; } }) .then(function () { if (_this._usesRemote && !_this.isAsync) { throw new Error('Remote used in synchronous test! Tests using this.remote must ' + 'return a promise or resolve a this.async deferred.'); } _this._hasPassed = true; }) .catch(function (error) { if (error !== exports.SKIP) { _this.error = error; throw error; } }) .finally(function () { return _this.executor.emit('testEnd', _this); }); }; Test.prototype.skip = function (message) { if (message === void 0) { message = 'skipped'; } this.skipped = message; throw exports.SKIP; }; Test.prototype.toJSON = function () { var _this = this; var json = {}; var properties = [ 'id', 'parentId', 'name', 'sessionId', 'timeElapsed', 'timeout', 'hasPassed', 'skipped' ]; properties.forEach(function (key) { var value = _this[key]; if (typeof value !== 'undefined') { json[key] = value; } }); if (this.error) { json.error = { name: this.error.name, message: this.error.message, stack: this.error.stack, showDiff: Boolean(this.error.showDiff) }; if (this.error.showDiff) { json.error.actual = this.error.actual; json.error.expected = this.error.expected; } } return json; }; return Test; }()); exports.default = Test; function isTest(value) { return (value != null && typeof value.test === 'function' && typeof value.hasPassed === 'boolean'); } exports.isTest = isTest; function isTestOptions(value) { return (value != null && !(value instanceof Test) && value.name != null && value.test != null); } exports.isTestOptions = isTestOptions; function isTestFunction(value) { return typeof value === 'function'; } exports.isTestFunction = isTestFunction; exports.SKIP = {}; }); //# sourceMappingURL=Test.js.map