Skip to content

Commit

Permalink
Merge 71dc77f into de1e398
Browse files Browse the repository at this point in the history
  • Loading branch information
jtenner committed Apr 25, 2019
2 parents de1e398 + 71dc77f commit a075a18
Show file tree
Hide file tree
Showing 41 changed files with 5,955 additions and 177 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,2 +1,3 @@
build
node_modules
node_modules
coverage
3 changes: 2 additions & 1 deletion .npmignore
Expand Up @@ -4,9 +4,10 @@ as-pect.config.js
tsconfig.json
.travis.yml
assembly/__tests__
coverage

# used for --init flag
!init/as-pect.config.js

# used for --init and --types flag
!assembly/__tests__/as-pect.d.ts
!assembly/__tests__/as-pect.d.ts
3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -2,3 +2,6 @@ language: node_js
node_js:
- "10"
- "11"
- "12"
after_success:
- npm run coveralls
320 changes: 320 additions & 0 deletions __tests__/TestContext.spec.ts
@@ -0,0 +1,320 @@
import { createModule } from "./setup/createModule";
import { TestContext } from "../src/test/TestContext";
import { TestGroup } from "../src/test/TestGroup";
import { round, sqrt, median, mean, sum } from "mathjs";
import { EmptyReporter } from "../src/reporter/EmptyReporter";

let ctx: TestContext;
let testGroup: TestGroup;
let controlFlowGroup: TestGroup;
let performanceGroup: TestGroup;
let failBeforeAllGroup: TestGroup;
let failBeforeEachGroup: TestGroup;
let failAfterEachGroup: TestGroup;
let failAfterAllGroup: TestGroup;
let failBeforeEachPerformanceGroup: TestGroup;
let failAfterEachPerformanceGroup: TestGroup;
const mock = {
"jest": {
"mockAfterAll": jest.fn(),
"mockAfterEach": jest.fn(),
"mockBeforeAll": jest.fn(),
"mockBeforeEach": jest.fn(),
"mockFailBeforeAll": jest.fn(),
"mockFailBeforeEach": jest.fn(),
"mockFailAfterEach": jest.fn(),
"mockFailAfterAll": jest.fn(),
"mockFailBeforeEachPerformance": jest.fn(),
"mockFailAfterEachPerformance": jest.fn(),
},
};

let start = new Promise<void>((resolve, reject) => {
createModule(mock, (err, result) => {
if (err) {
console.log(err);
reject(err);
}
else {
ctx = result!;
failAfterEachPerformanceGroup = ctx.testGroups[0];
failBeforeEachPerformanceGroup = ctx.testGroups[1];
failAfterAllGroup = ctx.testGroups[2];
failAfterEachGroup = ctx.testGroups[3];
failBeforeEachGroup = ctx.testGroups[4];
failBeforeAllGroup = ctx.testGroups[5];
controlFlowGroup = ctx.testGroups[6];
performanceGroup = ctx.testGroups[7];
testGroup = ctx.testGroups[8];
resolve();
}
});
});

beforeEach(() => start);

describe("TestContext test results", (): void => {
it("should pass the first test", (): void => {
const test = testGroup.tests[0];
expect(test.pass).toBeTruthy();
});

it("should fail the second test", (): void => {
const test = testGroup.tests[1];
expect(test.pass).toBeFalsy();
});

it("should pass the third test", (): void => {
const test = testGroup.tests[2];
expect(test.pass).toBeTruthy();
});

it("should expect each logged value target to be test", (): void => {
const test = testGroup.tests[2];
test.logs.forEach(element => {
expect(element.target).toBe(test);
});
});

it("should have logged a string", (): void => {
const test = testGroup.tests[2];
const value = test.logs[0];
expect(value.message).toBe("Hello world!");
});

it("should have logged a number", (): void => {
const test = testGroup.tests[2];
const value = test.logs[1];
expect(value.value).toBe(42);
});

it("should have logged a null", (): void => {
const test = testGroup.tests[2];
const value = test.logs[2];
expect(value.message).toBe("null");
expect(value.value).toBe(null);
});

it("should have logged a reference", (): void => {
const test = testGroup.tests[2];
const value = test.logs[3];
expect(value.message).toBe("Reference Type");
expect(value.bytes).toHaveLength(24);
});

it("should expect strings", (): void => {
const test = testGroup.tests[3];
expect(test.expected!.message).toBe("one");
expect(test.actual!.message).toBe("two");
});

it("should expect finite values", (): void => {
const test = testGroup.tests[4];
expect(test.actual!.message).toBe("NaN");
expect(test.actual!.value).toBeNaN();
expect(test.expected!.message).toBe("Finite Value");
});

it("should expect falsy values", (): void => {
const test = testGroup.tests[5];
expect(test.actual!.value).toBe(1);
expect(test.expected!.message).toBe("Falsy Value");
});

it("should expect truthy values", (): void => {
const test = testGroup.tests[6];
expect(test.actual!.value).toBe(0);
expect(test.expected!.message).toBe("Truthy Value");
});

it("should expect reference values", (): void => {
const test = testGroup.tests[7];
expect(test.actual!.message).toBe("Reference Value");
expect(test.expected!.message).toBe("Reference Value");
expect(test.actual!.bytes).not.toStrictEqual(test.expected!.bytes);
});

it("should expect number values", (): void => {
const test = testGroup.tests[8];
expect(test.actual!.value).toBe(0);
expect(test.expected!.value).toBe(42);
});

it("should report nulls", (): void => {
const test = testGroup.tests[9];
expect(test.actual!.value).toBe(null);
expect(test.expected!.value).toBe(null);
});

it("should report a negated test", (): void => {
const test = testGroup.tests[10];
expect(test.negated).toBeTruthy();
});

it("should fail when a negated test does not throw", (): void => {
const test = testGroup.tests[11];
expect(test.negated).toBeTruthy();
expect(test.pass).toBeFalsy();
});
});

describe("TestContext performance metrics", (): void => {
it("should have tested performance on the performanceGroup", (): void => {
const test = performanceGroup.tests[0];
expect(test.performance).toBeTruthy();
});

it("should have variance enabled", (): void => {
const test = performanceGroup.tests[1];
expect(test.hasVariance).toBeTruthy();
expect(test.rawVariance).not.toBe(0);
expect(test.variance).toBe(round(test.rawVariance, test.decimalPlaces));
});

it("should have min enabled", (): void => {
const test = performanceGroup.tests[2];
expect(test.hasMin).toBeTruthy();
expect(test.min).toBe(Math.min(...test.times));
});

it("should have min enabled", (): void => {
const test = performanceGroup.tests[3];
expect(test.hasMax).toBeTruthy();
expect(test.max).toBe(Math.max(...test.times));
});

it("should have stdDev enabled", (): void => {
const test = performanceGroup.tests[4];
expect(test.hasStdDev).toBeTruthy();
expect(test.hasVariance).toBeTruthy();
expect(test.stdDev).toBe(round(sqrt(test.rawVariance), test.decimalPlaces));
});

it("should have median enabled", (): void => {
const test = performanceGroup.tests[5];
expect(test.hasMedian).toBeTruthy();
expect(test.median).toBe(round(median(test.times), test.decimalPlaces));
});

it("should have average enabled", (): void => {
const test = performanceGroup.tests[6];
expect(test.hasAverage).toBeTruthy();
expect(test.average).toBe(round(mean(test.times), test.decimalPlaces));
});

it("should have set the rounding decimal places value", (): void => {
const test = performanceGroup.tests[7];
expect(test.decimalPlaces).toBe(42);
});

it("should run approximately less than a second", (): void => {
const test = performanceGroup.tests[8];
const testTimes = sum(test.times);
expect(testTimes).toBeLessThan(30);
expect(test.runTime).toBeLessThan(30);
});

it("should collect 400 samples", (): void => {
const test = performanceGroup.tests[9];
expect(test.times).toHaveLength(400);
});
});


describe("TestContext control flow", (): void => {
it("should have a single test", (): void => {
expect(controlFlowGroup.tests).toHaveLength(1);
});
it("should call the beforeEach callback", (): void => {
expect(mock.jest.mockBeforeEach).toBeCalled();
});
it("should call the beforeAll callback", (): void => {
expect(mock.jest.mockBeforeAll).toBeCalled();
});
it("should call the afterEach callback", (): void => {
expect(mock.jest.mockAfterEach).toBeCalled();
});
it("should call the afterAll callback", (): void => {
expect(mock.jest.mockAfterAll).toBeCalled();
});
});

describe("fail beforeAll", (): void => {
it("should call beforeAll", (): void => {
expect(mock.jest.mockFailBeforeAll).toBeCalled();
});

it("should fail the test group", (): void => {
expect(failBeforeAllGroup.pass).toBeFalsy();
expect(failBeforeAllGroup.reason).toBe(`Test suite failBeforeAll failed in beforeAll callback.`)
});
});

describe("fail beforeEach", (): void => {
it("should call beforeEach", (): void => {
expect(mock.jest.mockFailBeforeEach).toBeCalled();
});

it("should fail the test group", (): void => {
expect(failBeforeEachGroup.pass).toBeFalsy();
expect(failBeforeEachGroup.reason).toBe(`Test suite failBeforeEach failed in beforeEach callback.`)
});
});

describe("fail afterEach", (): void => {
it("should call afterEach", (): void => {
expect(mock.jest.mockFailAfterEach).toBeCalled();
});

it("should fail the test group", (): void => {
expect(failAfterEachGroup.pass).toBeFalsy();
expect(failAfterEachGroup.reason).toBe(`Test suite failAfterEach failed in afterEach callback.`)
});
});

describe("fail afterAll", (): void => {
it("should call afterAll", (): void => {
expect(mock.jest.mockFailAfterAll).toBeCalled();
});

it("should fail the test group", (): void => {
expect(failAfterAllGroup.pass).toBeFalsy();
expect(failAfterAllGroup.reason).toBe(`Test suite failAfterAll failed in afterAll callback.`)
});
});

describe("fail beforeEach in performance", (): void => {
it("should call mockFailBeforeEachPerformance", (): void => {
expect(mock.jest.mockFailBeforeEachPerformance).toBeCalled();
});

it("should fail the test group", (): void => {
expect(failBeforeEachPerformanceGroup.pass).toBeFalsy();
expect(failBeforeEachPerformanceGroup.reason).toBe(`Test suite failBeforeEachPerformance failed in beforeEach callback.`)
});
});

describe("fail afterEach in performance", (): void => {
it("should call mockFailAfterEachPerformance", (): void => {
expect(mock.jest.mockFailAfterEachPerformance).toBeCalled();
});

it("should fail the test group", (): void => {
expect(failAfterEachPerformanceGroup.pass).toBeFalsy();
expect(failAfterEachPerformanceGroup.reason).toBe(`Test suite failAfterEachPerformance failed in afterEach callback.`)
});
});

describe("other specs", (): void => {
const t = new TestContext(new EmptyReporter(), "empty.ts", { enabled: false });
it("should return 1 when tryCall is passed -1", (): void => {
// @ts-ignore
expect(t.tryCall(-1)).toBe(1);
});

// this test is just for statement coverage
it("should have default parameters", (): void => {
const t = new TestContext();
expect(t).toBeTruthy();
});
});
24 changes: 24 additions & 0 deletions __tests__/TestResult.spec.ts
@@ -0,0 +1,24 @@
import { TestResult } from "../src/test/TestResult";

let t: TestResult;

describe("TestResult", (): void => {
beforeEach((): void => {
t = new TestResult();
for (let i = 0; i < 1000; i++) {
t.times.push(i);
}
});

it("should caluculate varaince if std deviation is calculated", (): void => {
t.calculateStandardDeviation();
expect(t.hasVariance).toBeTruthy();
expect(t.hasStdDev).toBeTruthy();
});

// this test is purely for test coverage
it("should not calculate variance again if variance is asked for after std deviation", (): void => {
t.calculateStandardDeviation();
t.calculateVariance();
});
});

0 comments on commit a075a18

Please sign in to comment.