Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
testing how to deal with beforeEach / afterEach
<style>
.tinytest {background-color: black; color:white; font-family:monospace}
.tinytest div {margin-left: 1em}
.tinyTest .pass {color:green;}
.tinyTest .fail {color:red;}
.tinyTest .error {background-color:red; color:black}
</style>
<cfscript>
baseContext = []
describe = (message, callback) => {
var thisContext = {
message = message,
tests = []
}
baseContext.prepend(thisContext)
writeOutput("<div>")
writeOutput("#message#<br>")
callback()
baseContext.shift()
writeOutput("</div>")
}
beforeEach = (callBack) => {
baseContext[1].beforeEach = callback
}
afterEach = (callBack) => {
baseContext[1].afterEach = callback
}
it = (message, callback) => {
writeOutput("<div>")
baseContext.reduce((contexts, context) => contexts.prepend(context), []).each((context) => {
if (context.keyExists("beforeEach")) {
context.beforeEach()
}
})
writeOutput("It #message#: ")
callback()
baseContext.each((context) => {
if (context.keyExists("afterEach")) {
context.afterEach()
}
})
writeOutput("</div>")
}
run = (context) => {
describe("outer", () => {
beforeEach(() => {
writeoutput("beforeEach outer<br>")
})
afterEach(() => {
writeoutput("afterEach outer<br>")
})
it("is the first test", () => {
writeoutput("one<br>")
})
it("is the second test", () => {
writeoutput("two<br>")
})
describe("middle", () => {
beforeEach(() => {
writeoutput("beforeEach middle<br>")
})
afterEach(() => {
writeoutput("afterEach middle<br>")
})
it("is a test before the inner describe", () => {
writeoutput("three<br>")
})
describe("inner", () => {
beforeEach(() => {
writeoutput("beforeEach inner<br>")
})
afterEach(() => {
writeoutput("afterEach inner<br>")
})
it("is the inner first test", () => {
writeoutput("four<br>")
})
it("is the inner second test", () => {
writeoutput("five<br>")
})
})
it("is a test after the inner describe", () => {
writeoutput("six<br>")
})
})
it("is the last test", () => {
writeoutput("seven<br>")
})
})
}
writeOutput('<div class="tinyTest">')
result = run(baseContext)
writeOutput('</div>')
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment