Created
May 24, 2016 12:42
-
-
Save hooptie45/04b753fba2083bb9f99731863df0c467 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
((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