Skip to content

Instantly share code, notes, and snippets.

@hooptie45
Created May 24, 2016 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hooptie45/04b753fba2083bb9f99731863df0c467 to your computer and use it in GitHub Desktop.
Save hooptie45/04b753fba2083bb9f99731863df0c467 to your computer and use it in GitHub Desktop.
((jasmine)->
root = `(1, eval)('this')`
ARGUMENT_NAMES = /([^\s,]+)/g
getParamNames = (func) ->
fnStr = func.toString()
from = fnStr.indexOf('(') + 1
to = fnStr.indexOf(')')
result = fnStr.slice(from, to).match(ARGUMENT_NAMES)
if result? then result else []
env = jasmine.getEnv()
# Expose angular injected variables to global test environment
expose = (variableNames, values, to = window)->
testEnvironment = _.chain(variableNames).zip(values).object().value()
console.log "EXPOSED: #{ _.keys(testEnvironment)[0..-2].join(', ') }"
angular.extend to, testEnvironment
afterEach ->
delete window[name] for val, name in testEnvironment
injectFunction = (fn, lazy = false)->
injectedVariables = getParamNames(fn)
if injectedVariables.length
injectedVariables.push (injectedVariableValues...)->
expose(injectedVariables, injectedVariableValues)
fn.apply(null, injectedVariableValues)
inject injectedVariables
else fn
env.it = _.wrap env.it, (wit)->
[wrapped, desc, fn, t] = arguments
wit(desc, injectFunction(fn), t)
env.beforeEach = _.wrap env.beforeEach, (wBeforeEach)->
[wrapped, fn] = arguments
wBeforeEach(injectFunction(fn))
root.using = (fn)->
beforeEach fn
) jasmine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment