generated from well-known-components/base-ts-project
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* jest support * add support for jest and mocha * cleanup tests
- Loading branch information
Showing
13 changed files
with
4,351 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
dist | ||
node_modules | ||
temp | ||
tsdoc-metadata.json | ||
tsdoc-metadata.json | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
# test-helpers | ||
# test-helpers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = { | ||
globals: { | ||
'ts-jest': { | ||
tsconfig: 'tsconfig.json' | ||
} | ||
}, | ||
moduleFileExtensions: ['ts', 'js'], | ||
transform: { | ||
'^.+\\.(ts|tsx)$': 'ts-jest' | ||
}, | ||
testMatch: ['**/*.spec.(ts)'], | ||
testEnvironment: 'node' | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import expect from "expect" | ||
import { createRunner } from "../../src" | ||
|
||
type Components = { | ||
componentA: { | ||
functionThatThrows(): void | ||
} | ||
componentB: { | ||
sum(a: number, b: number): number | ||
counter(): number | ||
} | ||
} | ||
|
||
const test = createRunner<Components>({ | ||
async main(program) { | ||
await program.startComponents() | ||
}, | ||
async initComponents() { | ||
let counter = 0 | ||
|
||
const componentA = { | ||
functionThatThrows() { | ||
throw new Error("ABC") | ||
}, | ||
} | ||
const componentB = { | ||
sum(a: number, b: number) { | ||
counter++ | ||
return a + b | ||
}, | ||
counter() { | ||
return counter | ||
}, | ||
} | ||
|
||
return { componentA, componentB } | ||
}, | ||
}) | ||
|
||
if (typeof jest != "undefined") { | ||
test("mocking component A", ({ components }) => { | ||
it("tests without mock", async () => { | ||
const { componentA } = components | ||
expect(() => componentA.functionThatThrows()).toThrow("ABC") | ||
}) | ||
it("tests with mock", async () => { | ||
const { componentA } = components | ||
const spy1 = jest.spyOn(componentA, "functionThatThrows") | ||
spy1.mockImplementation(() => { | ||
throw new Error("XYZ") | ||
}) | ||
expect(() => componentA.functionThatThrows()).toThrow("XYZ") | ||
expect(spy1).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
} | ||
|
||
test("mocking component B", ({ components }) => { | ||
// this should throw because we are at "test declaration stage" and components don't exist yet | ||
expect(() => components.componentB.sum(1, 2)).toThrow( | ||
"Cannot get the components before the test program is initialized" | ||
) | ||
|
||
it("tests without mock", async () => { | ||
const { componentB } = components | ||
expect(componentB.counter()).toEqual(0) | ||
expect(componentB.sum(1, 2)).toEqual(3) | ||
expect(componentB.counter()).toEqual(1) | ||
}) | ||
|
||
it("same components instances should be used inside tests of the same run", async () => { | ||
const { componentB } = components | ||
expect(componentB.counter()).toEqual(1) | ||
}) | ||
if (typeof jest != "undefined") { | ||
it("tests with mock", async () => { | ||
const { componentB } = components | ||
const spy1 = jest.spyOn(componentB, "sum") | ||
spy1.mockImplementation((a, b) => { | ||
return 4 | ||
}) | ||
expect(componentB.sum(1, 2)).toEqual(4) | ||
expect(spy1).toHaveBeenCalledTimes(1) | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import expect from "expect" | ||
import sinon from "sinon" | ||
import { createRunner } from "../../src" | ||
|
||
type Components = { | ||
componentA: { | ||
functionThatThrows(): void | ||
} | ||
componentB: { | ||
sum(a: number, b: number): number | ||
counter(): number | ||
} | ||
} | ||
|
||
const test = createRunner<Components>({ | ||
async main(program) { | ||
await program.startComponents() | ||
}, | ||
async initComponents() { | ||
let counter = 0 | ||
|
||
const componentA = { | ||
functionThatThrows() { | ||
throw new Error("ABC") | ||
}, | ||
} | ||
const componentB = { | ||
sum(a: number, b: number) { | ||
counter++ | ||
return a + b | ||
}, | ||
counter() { | ||
return counter | ||
}, | ||
} | ||
|
||
return { componentA, componentB } | ||
}, | ||
}) | ||
|
||
test("mocking component A", ({ components, stubComponents }) => { | ||
it("tests without mock", async () => { | ||
const { componentA } = components | ||
expect(() => componentA.functionThatThrows()).toThrow("ABC") | ||
}) | ||
it("tests with mock", async () => { | ||
const { componentA } = stubComponents | ||
componentA.functionThatThrows.withArgs().throwsException(new Error("XYZ")) | ||
expect(() => componentA.functionThatThrows()).toThrow("XYZ") | ||
expect(componentA.functionThatThrows.calledOnce).toEqual(true) | ||
}) | ||
}) | ||
|
||
test("mocking component B", ({ components, stubComponents }) => { | ||
// this should throw because we are at "test declaration stage" and components don't exist yet | ||
expect(() => components.componentB.sum(1, 2)).toThrow( | ||
"Cannot get the components before the test program is initialized" | ||
) | ||
|
||
it("tests without mock", async () => { | ||
const { componentB } = components | ||
expect(componentB.counter()).toEqual(0) | ||
expect(componentB.sum(1, 2)).toEqual(3) | ||
expect(componentB.counter()).toEqual(1) | ||
}) | ||
|
||
it("same components instances should be used inside tests of the same run", async () => { | ||
const { componentB } = components | ||
expect(componentB.counter()).toEqual(1) | ||
}) | ||
|
||
it("tests with mock", async () => { | ||
const { componentB } = stubComponents | ||
componentB.sum.withArgs(1, 2).returns(4) | ||
expect(componentB.sum(1, 2)).toEqual(4) | ||
expect(componentB.sum.calledOnce).toEqual(true) | ||
}) | ||
}) | ||
|
||
test("logic tests", ({ components, stubComponents }) => { | ||
it("new components should be created for each test run", async () => { | ||
const { componentB } = components | ||
// the previous tests incremented the counter | ||
expect(componentB.counter()).toEqual(0) | ||
}) | ||
it("stubs could be used many times inside the same 'it'", async () => { | ||
const { componentB } = stubComponents | ||
componentB.counter.returns(12) | ||
expect(componentB.counter()).toEqual(12) | ||
componentB.counter.returns(15) | ||
expect(componentB.counter()).toEqual(15) | ||
// even without deconstructor | ||
stubComponents.componentB.counter.returns(19) | ||
expect(componentB.counter()).toEqual(19) | ||
}) | ||
it("stubs should reset between test cases", async () => { | ||
expect(components.componentB.counter()).toEqual(undefined) | ||
}) | ||
|
||
it("accessing stub and real components should yield the same results", async () => { | ||
stubComponents.componentB.counter.returns(33) | ||
expect(components.componentB.counter()).toEqual(33) | ||
|
||
expect(components.componentB).toEqual(stubComponents.componentB) | ||
}) | ||
|
||
it("stub methods should be restorable", async () => { | ||
stubComponents.componentB.counter.restore() | ||
expect(stubComponents.componentB.counter()).toEqual(0) | ||
}) | ||
it("restored stubs should remain that way between test cases", async () => { | ||
expect(stubComponents.componentB.counter()).toEqual(0) | ||
}) | ||
it("restored stubs should be re-stubabble", async () => { | ||
sinon.stub(stubComponents.componentB, "counter") | ||
|
||
stubComponents.componentB.counter.returns(33) | ||
expect(components.componentB.counter()).toEqual(33) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters