all files / core/ test-loader.ts

100% Statements 52/52
100% Branches 26/26
100% Functions 7/7
100% Lines 50/50
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        28×   92× 91× 91× 91×     91× 16× 16× 15×         75× 75× 75× 74×         91×     91×   91×   91×           91×     91×   91×   91×     91×   91×       89×     305× 305× 12×     12×     305×   305×     305×   305×   305× 299×     305× 305×   305× 138×     167× 686×         89×    
import { ITestFixture, ITest, ITestCase } from "./_interfaces";
import { FileRequirer } from "./_core";
import { TestFixture, METADATA_KEYS } from "./alsatian-core";
 
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 = new TestFixture();
 
      testFixture.ignored = false;
 
      if (Reflect.getMetadata(METADATA_KEYS.IGNORE, testFixtureConstructor)) {
        // fixture should be ignored
        testFixture.ignored = true;
 
        testFixture.ignoreReason = Reflect.getMetadata(METADATA_KEYS.IGNORE_REASON, testFixtureConstructor);
      }
 
      // create an instance of the test fixture
      testFixture.fixture = new testFixtureConstructor();
 
      // find all the tests on this test fixture
      let tests = Reflect.getMetadata(METADATA_KEYS.TESTS, testFixture.fixture);
 
      testFixture.focussed = false;
 
      if (Reflect.getMetadata(METADATA_KEYS.FOCUS, testFixtureConstructor)) {
        testFixture.focussed = true;
      }
 
      testFixture.tests = [];
 
      if (tests === undefined) {
        // no tests on the fixture
        return null;
      }
 
      tests.forEach((test: ITest) => {
 
        // the test is ignored if the fixture is, or if it's specifically ignored
        test.ignored = false;
        if (testFixture.ignored || Reflect.getMetadata(METADATA_KEYS.IGNORE, testFixture.fixture, test.key)) {
          test.ignored = true;
 
          // individual test ignore reasons take precedence over test fixture ignore reasons
          test.ignoreReason = Reflect.getMetadata(METADATA_KEYS.IGNORE_REASON, testFixture.fixture, test.key) || testFixture.ignoreReason;
        }
 
        test.focussed = false;
 
        if (Reflect.getMetadata(METADATA_KEYS.FOCUS, testFixture.fixture, test.key)) {
          test.focussed = true;
        }
 
        test.timeout = Reflect.getMetadata(METADATA_KEYS.TIMEOUT, testFixture.fixture, test.key) || null;
 
        testFixture.addTest(test);
 
        if (!test.description) {
           test.description = test.key;
        }
 
        let testCases = Reflect.getMetadata(METADATA_KEYS.TEST_CASES, testFixture.fixture, test.key);
        test.testCases = [];
 
        if (!testCases) {
          test.testCases.push({ arguments: [] });
        }
        else {
          testCases.forEach((testCase: ITestCase) => {
           test.testCases.push(testCase);
          });
        }
      });
 
      return testFixture;
  }
}