all files / core/ test-runner.ts

96.49% Statements 110/114
88.24% Branches 30/34
92.59% Functions 25/27
96.19% Lines 101/105
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212    70×       69× 69× 69× 69× 69× 69×           69×   64×       69×   69×   69×       1625×   69×     69×     69× 367× 1625× 1625× 348×   69× 19×       50× 50×   50× 50×   50× 50×           50×     5199× 5199×   5199×     5197×   5197× 65× 65×       5197× 5197× 51×   51× 51×   51×   51× 51× 51×             51×             5146× 5144×                     5195×   5195× 5195×   5195×     5197×   5197×   5197× 65× 65×         50×     299× 299× 299× 299×     299× 50×     249×     246×   246× 246×             1352× 1352×   1352× 296×     1056×   1056×           69× 5197×   5197× 1350×     3847×          
import { ITestFixture, ITest } from "./_interfaces";
import { createPromise } from "../promise/create-promise";
import { MatchError, TestSetResults, TestFixtureResults, TestResults, TestSet, TestOutput, METADATA_KEYS, TestTimeoutError } from "./alsatian-core";
import "reflect-metadata";
 
export class TestRunner {
 
   private _testsFocussed: boolean;
   private _testFixtures: Array<ITestFixture>;
   private _currentTestId: number = 0;
   private _currentTestFixtureIndex: number = 0;
   private _currentTestIndex: number = 0;
   private _currentTestCaseIndex: number = 0;
   private _resultPromise: any = createPromise();
   private _testResults = new TestSetResults();
   private _currentTestResults: TestResults;
   private _currentTestFixtureResults: TestFixtureResults;
   private _output: TestOutput;
 
   constructor (output?: TestOutput) {
       // If we were given a TestOutput, use it, otherwise make one
       if (output !== undefined) {
           this._output = output;
       } else {
           this._output = new TestOutput(process.stdout);
       }
   }
 
   private _testTimeout: number = 500;
 
   public run(testSet: TestSet, timeout?: number) {
 
      Iif (timeout) {
         this._testTimeout = timeout;
      }
 
     let anyTestsFocussed = testSet.testFixtures.filter(testFixture => testFixture.focussed || testFixture.getTests().filter(test => test.focussed).length > 0).length > 0;
 
     this._testFixtures = testSet.testFixtures;
 
     // Filter out unfocussed tests if any are focussed
     if (anyTestsFocussed) {
       this._testFixtures = testSet.testFixtures.filter(testFixture => testFixture.focussed || testFixture.getTests().filter(test => test.focussed).length > 0);
     }
 
      let totalTestCount = this._testFixtures
                                   .filter(x => x.getTests().length > 0)
                                   .map(x => x.getTests().map((y: any) => y.testCases.length)
                                   .reduce((a: number, b: number) => a + b, 0))
                                   .reduce((a: number, b: number) => a + b, 0);
 
      if (totalTestCount === 0) {
        throw new Error("no tests to run.");
      }
      else {
 
          this._output.emitVersion();
          this._output.emitPlan(totalTestCount);
 
         this._currentTestFixtureResults = this._testResults.addTestFixtureResult(this._testFixtures[this._currentTestFixtureIndex]);
         this._currentTestResults = this._currentTestFixtureResults.addTestResult(this._testFixtures[this._currentTestFixtureIndex].getTests()[this._currentTestIndex]);
 
         setTimeout(() => {
         this._runTest(this._testFixtures[this._currentTestFixtureIndex],
                 this._testFixtures[this._currentTestFixtureIndex].getTests()[this._currentTestIndex],
                 this._testFixtures[this._currentTestFixtureIndex].getTests()[this._currentTestIndex].testCases[this._currentTestCaseIndex].arguments);
              });
      }
 
      return this._resultPromise;
   }
 
   private _runTest(testFixture: ITestFixture, test: ITest, testCaseArguments: Array<any>) {
     this._currentTestId++;
 
     if (test.ignored) {
         let result = this._currentTestResults.addTestCaseResult(testCaseArguments, undefined);
         this._output.emitResult(this._currentTestId, result);
         this._runNextTest();
         return;
     }
 
     let setupFunctions: Array<string> = Reflect.getMetadata(METADATA_KEYS.SETUP, testFixture.fixture);
 
     if (setupFunctions) {
       setupFunctions.forEach(setupFunction => {
           testFixture.fixture[setupFunction].call(testFixture.fixture);
       });
     }
 
     try {
        if (test.isAsync) {
           let timeout = false;
 
           let promise: any = testFixture.fixture[test.key].apply(testFixture.fixture, testCaseArguments);
           let timeoutCheck: NodeJS.Timer = null;
 
          promise.then(() => {
 
             Eif (!timeout) {
               this._notifySuccess(test, testCaseArguments);
               clearTimeout(timeoutCheck);
             }
           })
           .catch((error: Error) => {
             this._handleError(error, test, testCaseArguments);
           });
 
           timeoutCheck = setTimeout(() => {
 
             timeout = true;
             this._handleError(new TestTimeoutError(test.timeout || this._testTimeout), test, testCaseArguments);
          }, test.timeout || this._testTimeout);
        }
        else {
          testFixture.fixture[test.key].apply(testFixture, testCaseArguments);
          this._notifySuccess(test, testCaseArguments);
        }
     }
     catch (error) {
       this._handleError(error, test, testCaseArguments);
     }
   }
 
   private _handleError(error: Error, test: ITest, testCaseArguments: Array<any>) {
     this._teardown();
 
     let result = this._currentTestResults.addTestCaseResult(testCaseArguments, error);
     this._output.emitResult(this._currentTestId, result);
 
     this._runNextTestCase();
   }
 
   private _notifySuccess(test: ITest, testCaseArguments: Array<any>) {
     this._teardown();
 
     let result = this._currentTestResults.addTestCaseResult(testCaseArguments);
     this._output.emitResult(this._currentTestId, result);
 
     this._runNextTestCase();
   }
 
   private _teardown() {
     let testFixture = this._testFixtures[this._currentTestFixtureIndex];
 
     let teardownFunctions: Array<string> = Reflect.getMetadata(METADATA_KEYS.TEARDOWN, testFixture.fixture);
 
     if (teardownFunctions) {
       teardownFunctions.forEach(teardownFunction => {
           testFixture.fixture[teardownFunction].call(testFixture.fixture);
       });
     }
   }
 
   private _exit() {
     this._resultPromise.resolve(this._testResults);
   }
 
   private _runNextTestFixture() {
     this._currentTestFixtureIndex++;
     this._currentTestIndex = 0;
     this._currentTestCaseIndex = 0;
 
     // no more test fixtures
     if (!this._testFixtures[this._currentTestFixtureIndex]) {
       this._exit();
     }
     // no tests on this fixture
     else if (!this._testFixtures[this._currentTestFixtureIndex].getTests()[this._currentTestIndex]) {
       this._runNextTestFixture();
     }
     else {
        this._currentTestFixtureResults = this._testResults.addTestFixtureResult(this._testFixtures[this._currentTestFixtureIndex]);
 
        setTimeout(() => {
          this._runTest(this._testFixtures[this._currentTestFixtureIndex],
                  this._testFixtures[this._currentTestFixtureIndex].getTests()[this._currentTestIndex],
                  this._testFixtures[this._currentTestFixtureIndex].getTests()[this._currentTestIndex].testCases[this._currentTestCaseIndex].arguments);
        });
     }
   }
 
   private _runNextTest() {
     this._currentTestIndex++;
     this._currentTestCaseIndex = 0;
 
     if (!this._testFixtures[this._currentTestFixtureIndex].getTests()[this._currentTestIndex]) {
       this._runNextTestFixture();
     }
     else {
        this._currentTestResults = this._currentTestFixtureResults.addTestResult(this._testFixtures[this._currentTestFixtureIndex].getTests()[this._currentTestIndex]);
 
       this._runTest(this._testFixtures[this._currentTestFixtureIndex],
               this._testFixtures[this._currentTestFixtureIndex].getTests()[this._currentTestIndex],
               this._testFixtures[this._currentTestFixtureIndex].getTests()[this._currentTestIndex].testCases[this._currentTestCaseIndex].arguments);
     }
   }
 
   private _runNextTestCase = () => {
     this._currentTestCaseIndex++;
 
     if (!this._testFixtures[this._currentTestFixtureIndex].getTests()[this._currentTestIndex].testCases[this._currentTestCaseIndex]) {
       this._runNextTest();
     }
     else {
       this._runTest(this._testFixtures[this._currentTestFixtureIndex],
               this._testFixtures[this._currentTestFixtureIndex].getTests()[this._currentTestIndex],
               this._testFixtures[this._currentTestFixtureIndex].getTests()[this._currentTestIndex].testCases[this._currentTestCaseIndex].arguments);
     }
   }
}