all files / core/ test-runner.js

94.96% Statements 113/119
85.29% Branches 29/34
86.21% Functions 25/29
96.46% Lines 109/113
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165  67× 67× 67× 67× 67× 67× 67× 67× 5045× 5045× 5045×   5045×   67× 5044× 5044× 1282×     3762×       67× 1556× 67×   67×     67× 353× 1556× 1556× 334× 67× 19×     48× 48× 48× 48× 48× 48×     48×   5045× 5045× 5045×   5044× 5044× 66× 66×     5044× 5044× 50× 50× 50× 50× 50× 50× 50×           50×           4994× 4992×                   5042× 5042× 5042× 5042×   5044× 5044× 5044× 66× 66×       48×   285× 285× 285× 285×   285× 48×   237×     234× 234× 234×       1283× 1283× 1283× 282×     1001× 1001×        
"use strict";
var _errors_1 = require("./_errors");
var create_promise_1 = require("../promise/create-promise");
var _results_1 = require("./_results");
require("reflect-metadata");
var TestRunner = (function () {
    function TestRunner() {
        var _this = this;
        this._currentTestId = 0;
        this._currentTestFixtureIndex = 0;
        this._currentTestIndex = 0;
        this._currentTestCaseIndex = 0;
        this._resultPromise = create_promise_1.createPromise();
        this._testResults = new _results_1.TestSetResults();
        this._getTestDescription = function (test, testCaseArguments) {
            var testDescription = _this._currentTestId + " " + (test.ignored ? "# skip " : "") + test.description;
            Eif (testCaseArguments !== undefined) {
                testDescription += " [ " + testCaseArguments.map(function (x) { return JSON.stringify(x) || "undefined"; }).join(", ") + " ]";
            }
            return testDescription;
        };
        this._runNextTestCase = function () {
            _this._currentTestCaseIndex++;
            if (!_this._testFixtures[_this._currentTestFixtureIndex].tests[_this._currentTestIndex].testCases[_this._currentTestCaseIndex]) {
                _this._runNextTest();
            }
            else {
                _this._runTest(_this._testFixtures[_this._currentTestFixtureIndex], _this._testFixtures[_this._currentTestFixtureIndex].tests[_this._currentTestIndex], _this._testFixtures[_this._currentTestFixtureIndex].tests[_this._currentTestIndex].testCases[_this._currentTestCaseIndex].arguments);
            }
        };
    }
    TestRunner.prototype.run = function (testSet) {
        var _this = this;
        var anyTestsFocussed = testSet.testFixtures.filter(function (testFixture) { return testFixture.focussed || testFixture.tests.filter(function (test) { return test.focussed; }).length > 0; }).length > 0;
        this._testFixtures = testSet.testFixtures;
        // Filter out unfocussed tests if any are focussed
        Iif (anyTestsFocussed) {
            this._testFixtures = testSet.testFixtures.filter(function (testFixture) { return testFixture.focussed || testFixture.tests.filter(function (test) { return test.focussed; }).length > 0; });
        }
        var totalTestCount = this._testFixtures
            .filter(function (x) { return x.tests.length > 0; })
            .map(function (x) { return x.tests.map(function (y) { return y.testCases.length; })
            .reduce(function (a, b) { return a + b; }, 0); })
            .reduce(function (a, b) { return a + b; }, 0);
        if (totalTestCount === 0) {
            throw new Error("no tests to run.");
        }
        else {
            process.stdout.write("TAP version 13\n");
            process.stdout.write("1.." + totalTestCount + "\n");
            this._currentTestFixtureResults = this._testResults.addTestFixtureResult(this._testFixtures[this._currentTestFixtureIndex]);
            this._currentTestResults = this._currentTestFixtureResults.addTestResult(this._testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex]);
            setTimeout(function () {
                _this._runTest(_this._testFixtures[_this._currentTestFixtureIndex], _this._testFixtures[_this._currentTestFixtureIndex].tests[_this._currentTestIndex], _this._testFixtures[_this._currentTestFixtureIndex].tests[_this._currentTestIndex].testCases[_this._currentTestCaseIndex].arguments);
            });
        }
        return this._resultPromise;
    };
    TestRunner.prototype._runTest = function (testFixture, test, testCaseArguments) {
        var _this = this;
        this._currentTestId++;
        if (test.ignored) {
            process.stdout.write("ok " + this._getTestDescription(test, testCaseArguments) + "\n");
            this._runNextTest();
            return;
        }
        var setupFunctions = Reflect.getMetadata("alsatian:setup", testFixture.fixture);
        if (setupFunctions) {
            setupFunctions.forEach(function (setupFunction) {
                testFixture.fixture[setupFunction].call(testFixture.fixture);
            });
        }
        try {
            if (test.isAsync) {
                var timeout_1 = false;
                var promise = testFixture.fixture[test.key].apply(testFixture.fixture, testCaseArguments);
                var timeoutCheck_1 = null;
                promise.then(function () {
                    Eif (!timeout_1) {
                        _this._notifySuccess(test, testCaseArguments);
                        clearTimeout(timeoutCheck_1);
                    }
                })
                    .catch(function (error) {
                    _this._handleError(error, test, testCaseArguments);
                });
                timeoutCheck_1 = setTimeout(function () {
                    timeout_1 = true;
                    _this._handleError(new Error("The test exceeded the given timeout."), test, testCaseArguments);
                }, test.timeout | 500);
            }
            else {
                testFixture.fixture[test.key].apply(testFixture, testCaseArguments);
                this._notifySuccess(test, testCaseArguments);
            }
        }
        catch (error) {
            this._handleError(error, test, testCaseArguments);
        }
    };
    TestRunner.prototype._handleError = function (error, test, testCaseArguments) {
        this._teardown();
        this._currentTestResults.addTestCaseResult(testCaseArguments, error);
        process.stdout.write("not ok " + this._getTestDescription(test, testCaseArguments) + "\n");
        if (error instanceof _errors_1.MatchError) {
            process.stdout.write(" ---\n   message: \"" + error.message + "\"\n   severity: fail\n   data:\n     got: " + JSON.stringify(error.actualValue) + "\n     expect: " + JSON.stringify(error.expectedValue) + "\n ...\n");
        }
        else {
            console.log(error);
            process.stdout.write("# Unknown Error\n");
        }
        this._runNextTestCase();
    };
    TestRunner.prototype._notifySuccess = function (test, testCaseArguments) {
        this._teardown();
        process.stdout.write("ok " + this._getTestDescription(test, testCaseArguments) + "\n");
        this._currentTestResults.addTestCaseResult(testCaseArguments);
        this._runNextTestCase();
    };
    TestRunner.prototype._teardown = function () {
        var testFixture = this._testFixtures[this._currentTestFixtureIndex];
        var teardownFunctions = Reflect.getMetadata("alsatian:teardown", testFixture.fixture);
        if (teardownFunctions) {
            teardownFunctions.forEach(function (teardownFunction) {
                testFixture.fixture[teardownFunction].call(testFixture.fixture);
            });
        }
    };
    TestRunner.prototype._exit = function () {
        this._resultPromise.resolve(this._testResults);
    };
    TestRunner.prototype._runNextTestFixture = function () {
        var _this = this;
        this._currentTestFixtureIndex++;
        this._currentTestIndex = 0;
        this._currentTestCaseIndex = 0;
        // no more test fixtures
        if (!this._testFixtures[this._currentTestFixtureIndex]) {
            this._exit();
        }
        else if (!this._testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex]) {
            this._runNextTestFixture();
        }
        else {
            this._currentTestFixtureResults = this._testResults.addTestFixtureResult(this._testFixtures[this._currentTestFixtureIndex]);
            setTimeout(function () {
                _this._runTest(_this._testFixtures[_this._currentTestFixtureIndex], _this._testFixtures[_this._currentTestFixtureIndex].tests[_this._currentTestIndex], _this._testFixtures[_this._currentTestFixtureIndex].tests[_this._currentTestIndex].testCases[_this._currentTestCaseIndex].arguments);
            });
        }
    };
    TestRunner.prototype._runNextTest = function () {
        this._currentTestIndex++;
        this._currentTestCaseIndex = 0;
        if (!this._testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex]) {
            this._runNextTestFixture();
        }
        else {
            this._currentTestResults = this._currentTestFixtureResults.addTestResult(this._testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex]);
            this._runTest(this._testFixtures[this._currentTestFixtureIndex], this._testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex], this._testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex].testCases[this._currentTestCaseIndex].arguments);
        }
    };
    return TestRunner;
}());
exports.TestRunner = TestRunner;
//# sourceMappingURL=test-runner.js.map