Skip to content

Instantly share code, notes, and snippets.

@fivetanley
Last active December 9, 2015 21:59
Show Gist options
  • Select an option

  • Save fivetanley/4334633 to your computer and use it in GitHub Desktop.

Select an option

Save fivetanley/4334633 to your computer and use it in GitHub Desktop.
Lol qunit
class Test
constructor: (@description,@body,@context) ->
run: =>
test.call @context, @description, => @body.call @context
class Suite
capitalize = (string) ->
string[0].toUpperCase() + string.slice(1)
each = ( collection, fn ) ->
for item in collection
fn( item )
HOOK_TYPES = [ 'beforeEach', 'beforeAll', 'afterEach', 'afterAll' ]
constructor: (@name)->
for hookType in HOOK_TYPES
@[ "#{hookType}Hooks" ] = []
@tests = []
test: (description, testBody) =>
@tests.push new Test( description,testBody,this )
describe: (description, suiteBody) =>
nestedSuite = new Suite @name+description.toString()
suiteBody.call nestedSuite
@tests.push nestedSuite
context: (description, suiteBody) => @describe( description, suiteBody )
it: ( description, testBody ) =>
@test description, testBody
before: (allOrEach, hookBody ) =>
@["before#{capitalize(allOrEach)}" ] hookBody
after: (allOrEach, hookBody ) =>
@["after#{capitalize(allOrEach)}" ] hookBody
beforeAll: (fn) =>
@beforeAllHooks.push fn
afterAll: (fn) =>
@afterAllHooks.push fn
afterEach: (fn) =>
@afterEachHooks.push fn
beforeEach: (fn) =>
@beforeEachHooks.push fn
run: =>
each @beforeAllHooks, ( hook ) => hook.call( this )
each @tests, ( example ) =>
module @name.toString()
each @beforeEachHooks, (hook) => hook.call( this )
example.run()
each @afterEachHooks, ( hook ) => hook.call this
each @afterEachHooks, ( hook ) => hook.call this
@tests = []
suite = new Suite( 'FullAssignmentView' )
suite.beforeEach ->
@assignment = new Assignment()
suite.describe "#someMethod", ->
@beforeEach -> @assignment = "hi"
@it "does something", ->
ok @assignment
@describe "NESTING WE MUST GO DEEPER", ->
ok 'deeeeeeper'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment