all files / core/ test-loader.ts

100% Statements 49/49
95.45% Branches 21/22
100% Functions 7/7
100% Lines 47/47
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            20×   72× 71× 71× 71×     71× 13× 13× 12×         58× 58× 58× 57×         71×     71×   71×   71×         71×     71×   71×   71×     71×   71×       69×   230× 230×     230×   230×     230×   230×   230× 230×     230× 230×   230× 105×     125× 559×         69×    
import { ITestFixture } from "./_interfaces/test-fixture.i";
import { ITest } from "./_interfaces/test.i";
import { ITestCase } from "./_interfaces/test-case.i";
import { FileRequirer } from "./file-requirer";
 
export class TestLoader {
 
   public constructor(private _fileRequirer: FileRequirer) { }
 
  loadTestFixture(filePath: string): Array<ITestFixture> {
    let Test = this._fileRequirer.require(filePath);
    let testFixtureKeys = Object.keys(Test);
    let testFixtures: Array<ITestFixture> = [];
 
    // if the default export is class constructor
    if (typeof Test === "function") {
      let testFixture = this._loadTestFixture(Test);
      if (testFixture !== null) {
        testFixtures.push(testFixture);
      }
    }
    // otherwise there are multiple exports and we must handle all of them
    else {
      testFixtureKeys.forEach(testFixtureKey => {
        let testFixture = this._loadTestFixture(Test[testFixtureKey]);
        if (testFixture !== null) {
          testFixtures.push(testFixture);
        }
       });
     }
 
     return testFixtures;
   }
 
  private _loadTestFixture(testFixtureConstructor: any): ITestFixture {
      let testFixture = <ITestFixture>{};
 
      testFixture.ignored = false;
 
      if (Reflect.getMetadata("alsatian:ignore", testFixtureConstructor)) {
        // fixture should be ignored
        testFixture.ignored = true;
      }
 
      // create an instance of the test fixture
      testFixture.fixture = new testFixtureConstructor();
 
      // find all the tests on this test fixture
      let tests = Reflect.getMetadata("alsatian:tests", testFixture.fixture);
 
      testFixture.focussed = false;
 
      if (Reflect.getMetadata("alsatian:focus", testFixtureConstructor)) {
        testFixture.focussed = true;
      }
 
      testFixture.tests = [];
 
      if (tests === undefined) {
        // no tests on the fixture
        return null;
      }
 
      tests.forEach((test: ITest) => {
 
        test.ignored = false;
        if (Reflect.getMetadata("alsatian:ignore", testFixture.fixture, test.key)) {
          test.ignored = true;
        }
 
        test.focussed = false;
 
        if (Reflect.getMetadata("alsatian:focus", testFixture.fixture, test.key)) {
          test.focussed = true;
        }
 
        test.timeout = Reflect.getMetadata("alsatian:timeout", testFixture.fixture, test.key) || null;
 
        testFixture.tests.push(test);
 
        Eif (!test.description) {
           test.description = test.key;
        }
 
        let testCases = Reflect.getMetadata("alsatian:testcases", testFixture.fixture, test.key);
        test.testCases = [];
 
        if (!testCases) {
          test.testCases.push({ arguments: [] });
        }
        else {
          testCases.forEach((testCase: ITestCase) => {
           test.testCases.push(testCase);
          });
        }
      });
 
      return testFixture;
  }
}