Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
TTF tests for lifecycle functions
<cfscript>
void function run() {
beforeAll(() => {
tinyTest.debug.setByBeforeAll = true
})
describe("Tests of beforeEach", () => {
describe("Tests without beforeEach (top)", () => {
result = []
it("was called before any usage of beforeEach", () => {
result.append("test before any beforeEach implementation")
expect(result).toBe([
"test before any beforeEach implementation"
])
})
})
describe("Testing first level implementation", () => {
result = []
beforeEach(() => {
result.append("beforeEach")
})
it("was called before the first test in the set", () => {
result.append("first test")
expect(result).toBe([
"beforeEach",
"first test"
])
})
it("was called before the second test in the set", () => {
result.append("second test")
expect(result).toBe([
"beforeEach",
"first test",
"beforeEach",
"second test"
])
})
})
describe("Testing cascade from ancestor", () => {
result = []
beforeEach(() => {
result.append("beforeEach in ancestor")
})
describe("descendant of ancestor", () => {
it("was called even though it is in an ancestor describe block", () => {
result.append("test in descendant")
expect(result).toBe([
"beforeEach in ancestor",
"test in descendant"
])
})
})
})
describe("Multiple sequential handlers", () => {
beforeEach(() => {
result = []
result.append("beforeEach in outer")
})
it("is at the top of the hierarchy before any describe", () => {
result.append("at the top of the hierarchy before any describe")
expect(result).toBe([
"beforeEach in outer",
"at the top of the hierarchy before any describe"
])
})
describe("first descendant of ancestor", () => {
beforeEach(() => {
result.append("beforeEach in middle")
})
it("is a test in the middle of the hierarchy, before the inner describe", () => {
result.append("test before the inner describe")
expect(result).toBe([
"beforeEach in outer",
"beforeEach in middle",
"test before the inner describe"
])
})
describe("inner descendant of ancestor", () => {
it("is a test in the bottom of the hierarchy, before the inner beforeEach", () => {
result.append("in the bottom of the hierarchy, before the inner beforeEach")
expect(result).toBe([
"beforeEach in outer",
"beforeEach in middle",
"in the bottom of the hierarchy, before the inner beforeEach"
])
})
beforeEach(() => {
result.append("beforeEach in inner")
})
it("calls each beforeEach handler in the hierarchy, from outermost to innermost", () => {
result.append("test in innermost descendant")
expect(result).toBe([
"beforeEach in outer",
"beforeEach in middle",
"beforeEach in inner",
"test in innermost descendant"
])
})
it("is another innermost test", () => {
result.append("is another innermost test")
expect(result).toBe([
"beforeEach in outer",
"beforeEach in middle",
"beforeEach in inner",
"is another innermost test"
])
})
})
it("is a test in the middle of the hierarchy, after the inner describe", () => {
result.append("test after the inner describe")
expect(result).toBe([
"beforeEach in outer",
"beforeEach in middle",
"test after the inner describe"
])
})
})
describe("A second describe in the middle tier of the hierarchy", () => {
beforeEach(() => {
result.append("beforeEach second middle describe")
})
it("is a test in the second describe in the middle tier of the hierarchy", () => {
result.append("in the second describe in the middle tier of the hierarchy")
expect(result).toBe([
"beforeEach in outer",
"beforeEach second middle describe",
"in the second describe in the middle tier of the hierarchy"
])
})
})
it("is at the top of the hierarchy after any describe", () => {
result.append("at the top of the hierarchy after any describe")
expect(result).toBe([
"beforeEach in outer",
"at the top of the hierarchy after any describe"
])
})
})
describe("Tests a stack with some describes without a beforeEach (top)", () => {
result = []
describe("Tests a stack with some describes without a beforeEach (middle 1)", () => {
beforeEach(() => {
result.append("beforeEach in second level of hierarchy")
})
describe("Tests a stack with some describes without a beforeEach (middle 2)", () => {
describe("Tests a stack with some describes without a beforeEach (inner)", () => {
it("ran the 'grandparent' beforeEach", () => {
result.append("in the fourth tier of the hierarchy")
expect(result).toBe([
"beforeEach in second level of hierarchy",
"in the fourth tier of the hierarchy"
])
})
})
})
})
})
describe("Tests without beforeEach (bottom)", () => {
result = []
it("was called after all other tests", () => {
result.append("test after any beforeEach implementation")
expect(result).toBe([
"test after any beforeEach implementation"
])
})
})
})
describe("Tests of afterEach", () => {
it("will not break if an afterEach is not specified for a given describe block", () => {
expect(true).toBeTrue()
})
describe("Baseline", () => {
result = []
afterEach(() => {
result.append("set in afterEach")
})
it("runs after a test (setup)", () => {
expect(result).toBe([])
})
it("runs after a test (test)", () => {
expect(result).toBe(["set in afterEach"])
})
})
describe("Works in a hierarchy (top)", () => {
result = []
afterEach(() => {
result.append("set in afterEach in outer describe")
})
describe("Works in a hierarchy (middle)", () => {
afterEach(() => {
result.append("set in afterEach in middle describe")
})
describe("Works in a hierarchy (inner)", () => {
afterEach(() => {
result.append("set in afterEach in inner describe")
})
it("runs all afterEach handlers, from innermost to outermost (setup)", () => {
expect(result).toBe([])
})
it("runs all afterEach handlers, from innermost to outermost (test)", () => {
expect(result).toBe([
"set in afterEach in inner describe",
"set in afterEach in middle describe",
"set in afterEach in outer describe"
])
})
})
})
})
describe("Tests with beforeEach as well", () => {
result = []
afterEach(() => {
result.append("set by afterEach")
})
beforeEach(() => {
result.append("set by beforeEach")
})
it("is the setup test", () => {
expect(true).toBeTrue()
})
it("tests that both beforeEach and afterEach got run", () => {
result.append("testing that the preceding setup test had its afterEach called")
expect(result).toBe([
"set by beforeEach", // setup test
"set by afterEach", // setup test
"set by beforeEach", // this test
"testing that the preceding setup test had its afterEach called"
])
})
})
describe("Tests on a failing test", () => {
result = []
afterEach(() => {
result.append("set by afterEach")
})
it("is the setup test (this is expected to fail, don't worry about it)", () => {
expect(true).toBeFalse()
})
it("tests that afterEach runs even if the test fails", () => {
result.append("testing that the preceding failing setup test had its afterEach called")
expect(result).toBe([
"set by afterEach", // setup test
"testing that the preceding failing setup test had its afterEach called"
])
})
})
describe("Tests on an erroring test", () => {
result = []
afterEach(() => {
result.append("set by afterEach")
})
it("is the setup test (this is expected to error, don't worry about it)", () => {
throw("forcing an error")
})
it("tests that afterEach runs even if the test errors", () => {
result.append("testing that the preceding erroring setup test had its afterEach called")
expect(result).toBe([
"set by afterEach", // setup test
"testing that the preceding erroring setup test had its afterEach called"
])
})
})
})
describe("Tests of aroundEach", () => {
describe("Tests hierarchical sequencing", () => {
result = []
aroundEach((test) => {
result.append("aroundEach top level before test")
test()
result.append("aroundEach top level after test")
})
describe("Tests hierarchical sequencing (second level: no aroundEach in this one)", () => {
describe("Tests hierarchical sequencing (third level)", () => {
aroundEach((test) => {
result.append("aroundEach third level before test")
test()
result.append("aroundEach third level after test")
})
describe("Tests hierarchical sequencing (inner level)", () => {
aroundEach((test) => {
result.append("aroundEach inner before test")
test()
result.append("aroundEach inner after test")
})
it("is the baseline test", ()=> {
expect(true).toBeTrue()
})
it("tests the aroundEach handlers are called in the correct order", ()=> {
result.append("tests the aroundEach handlers are called in the correct order")
expect(result).toBe([
"aroundEach top level before test",
"aroundEach third level before test",
"aroundEach inner before test",
"aroundEach inner after test",
"aroundEach third level after test",
"aroundEach top level after test",
"aroundEach top level before test",
"aroundEach third level before test",
"aroundEach inner before test",
"tests the aroundEach handlers are called in the correct order"
])
})
})
})
})
})
describe("Tests with beforeEach and afterEach", () => {
result = []
afterEach(() => {
result.append("set by afterEach")
})
beforeEach(() => {
result.append("set by beforeEach")
})
aroundEach((test) => {
result.append("set by aroundEach before test")
test()
result.append("set by aroundEach after test")
})
it("is the baseline test", ()=> {
expect(true).toBeTrue()
})
it("tests the aroundEach handlers are called in the correct order", ()=> {
result.append("tests the aroundEach handlers are called in the correct order")
expect(result).toBe([
"set by beforeEach",
"set by aroundEach before test",
"set by aroundEach after test",
"set by afterEach",
"set by beforeEach",
"set by aroundEach before test",
"tests the aroundEach handlers are called in the correct order"
])
})
})
describe("Tests with failing tests", () => {
result = []
aroundEach((test) => {
result.append("set by aroundEach before test")
test()
result.append("set by aroundEach after test")
})
it("It is the setup test (this is expected to fail, don't worry about it)", ()=> {
expect(true).toBefalse()
})
it("tests the 'after' part of the aroundEach handler is still called when a test fails", ()=> {
result.append("tests the 'after' part of the aroundEach handler is still called when a test fails")
expect(result).toBe([
"set by aroundEach before test",
"set by aroundEach after test",
"set by aroundEach before test",
"tests the 'after' part of the aroundEach handler is still called when a test fails"
])
})
})
describe("Tests with erroring tests", () => {
result = []
aroundEach((test) => {
result.append("set by aroundEach before test")
test()
result.append("set by aroundEach after test")
})
it("It is the setup test (this is expected to error, don't worry about it)", ()=> {
throw("forcing an error")
})
it("tests the 'after' part of the aroundEach handler is still called when a test errors", ()=> {
result.append("tests the 'after' part of the aroundEach handler is still called when a test errors")
expect(result).toBe([
"set by aroundEach before test",
"set by aroundEach after test",
"set by aroundEach before test",
"tests the 'after' part of the aroundEach handler is still called when a test errors"
])
})
})
})
describe("Tests of beforeAll", () => {
it("is executed before tests", () => {
expect(tinyTest).toHaveKey("debug")
expect(tinyTest.debug).toHaveKey("setByBeforeAll")
expect(tinyTest.debug.setByBeforeAll).toBe(true)
})
})
describe("Tests for afterAll", () => {
it("is executed after tests", () => {
/* we can't test this directly here as it intrinsically runs *after the tests*
so call another test run that has an afterAll handler. The code for the test
run here is:
function run() {
afterAll(() => {
writeOutput("afterAll ran OK")
})
describe("Tests of afterAll", () => {
it("is a simple test", () => {
expect(true).toBeTrue()
})
})
}
tinyTest.runTests()
*/
cfhttp(method="get", url="https://gist.githubusercontent.com/adamcameron/816ce84fd991c2682df612dbaf1cad11/raw/3d98b189c0062752928a6966a494084ae1af4c0c/tinyTestFramework.cfm", result="local.frameworkCodeResponse");
cfhttp(method="get", url="https://gist.githubusercontent.com/adamcameron/0df8bb82777d92387f0852c8e26feec8/raw/0168fdd943161b2c01161662ef1deff62ffb9be2/afterAllBasicTest.cfm", result="local.testCodeResponse");
var frameworkCode = frameworkCodeResponse.fileContent;
var testCode = testCodeResponse.fileContent;
cfhttp(method="post", url="https://acf14-sbx.trycf.com/getremote.cfm", result="local.testRunResponse") {
cfhttpparam(type="formField", name="setupcode", value=frameworkCode);
cfhttpparam(type="formField", name="code", value=testCode);
cfhttpparam(type="formField", name="asserts", value="");
}
expect(local.testRunResponse.fileContent).toInclude("afterAll ran OK")
})
})
}
tinyTest.runTests()
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment