all files / core/ test-set.ts

83.93% Statements 47/56
72.73% Branches 16/22
76.92% Functions 10/13
84.62% Lines 44/52
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       3368×                                           43×                 43× 43×     43×   43×         43×     43×     43×   43×         43×   43×   43×   193×       192× 192× 192×   192×   192× 192×     192× 192×   192× 71×     121× 547×         43×      
import * as Glob from "glob";
const path = require("path");
 
export class TestSet {
 
  private _testsFocussed: boolean;
  private _testFixtures: Array<any> = [];
  public get testFixtures(): Array<any> {
    return this._testFixtures;
  }
 
  public constructor (testFileLocation: string)
  public constructor (testFileLocations: Array<string>)
  public constructor(testsFileLocations: string | Array<string>) {
 
    Iif (typeof testsFileLocations === "string") {
      testsFileLocations = [ <string>testsFileLocations ];
    }
 
    this._loadTestFixtures(<Array<string>>testsFileLocations);
 
    // Filter out unfocussed tests if any are focussed
    Iif (this._testsFocussed) {
      this._testFixtures = this._testFixtures.map(x => {
        x.tests = x.tests.filter((y: any) => y.focussed)
        return x;
      }).filter(testFixture => testFixture.tests.length !== 0);
    }
  }
 
  private _loadTestFixtures(testFileLocations: Array<string>) {
     testFileLocations.forEach(testFileLocation => {
 
        testFileLocation = path.join(process.cwd(), testFileLocation);
 
        Eif (Glob.hasMagic(testFileLocation)) {
          let physicalTestFileLocations = Glob.sync(testFileLocation);
 
          physicalTestFileLocations.forEach(physicalTestFileLocation => {
 
              this._loadTest(require(physicalTestFileLocation));
          });
        }
        else {
          this._loadTest(require(testFileLocation));
        }
     });
  }
 
  private _loadTest(Test: any) {
     let testFixtureKeys = Object.keys(Test);
 
     // CALCULATE TESTS TO RUN
     testFixtureKeys.forEach(testFixtureKey => {
 
       Iif (Reflect.getMetadata("alsatian:ignore", Test[testFixtureKey])) {
         // fixture should be ignored
         return;
       }
 
       let testFixture: any = {};
 
       // create an instance of the test fixture
       testFixture.fixture = new Test[testFixtureKey]();
 
       // find all the tests on this test fixture
       let tests = Reflect.getMetadata("alsatian:tests", testFixture.fixture);
 
       Iif (!tests || tests.length === 0) {
         // no tests on the fixture
         return;
       }
 
       let focusFixture = Reflect.getMetadata("alsatian:focus", Test[testFixtureKey]);
 
       testFixture.tests = [];
 
       tests.forEach((test: any) => {
 
         if (Reflect.getMetadata("alsatian:ignore", testFixture.fixture, test.key)) {
           // ignore this test
           return;
         }
 
         let focusTest = Reflect.getMetadata("alsatian:focus", testFixture.fixture, test.key);
         test.focussed = focusFixture || focusTest;
         this._testsFocussed = this._testsFocussed || test.focussed;
 
         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([]);
         }
         else {
           testCases.forEach((testCase: any) => {
             test.testCases.push(testCase);
           });
         }
       });
 
       this._testFixtures.push(testFixture);
     });
  }
}