Skip to content

Commit

Permalink
Override global jasmine spec functions
Browse files Browse the repository at this point in the history
Currently, if a spec uses the global `it` function on an async test,
that test will always pass (since the jasmine version checked in Atom
does not natively support tests that return promises). This can be
confusing since the test behaviour is different between the
async-test-helpers methods and the global ones.

By overriding the global functions, we'll also be able to remove all the
imports from async-test-helpers since they won't be needed anymore.

More info: atom#18896 (comment)
  • Loading branch information
rafeca committed Feb 27, 2019
1 parent 042fb45 commit 619d643
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion spec/jasmine-test-runner.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ module.exports = ({logFile, headless, testPaths, buildAtomEnvironment}) ->
window[key] = value for key, value of require '../vendor/jasmine'
require 'jasmine-tagged'

# Rewrite global jasmine functions to have support for async tests.
# This way packages can create async specs without having to import these from the
# async-spec-helpers file.
global.it = asyncifyJasmineFn global.it
global.fit = asyncifyJasmineFn global.fit
global.ffit = asyncifyJasmineFn global.ffit
global.fffit = asyncifyJasmineFn global.fffit
global.beforeEach = asyncifyJasmineFn global.beforeEach
global.afterEach = asyncifyJasmineFn global.afterEach

# Allow document.title to be assigned in specs without screwing up spec window title
documentTitle = null
Object.defineProperty document, 'title',
Expand Down Expand Up @@ -59,6 +69,21 @@ module.exports = ({logFile, headless, testPaths, buildAtomEnvironment}) ->
jasmineEnv.execute()
promise

asyncifyJasmineFn = (fn, posCallback = -1) ->
(args...) ->
if posCallback == -1
posCallback = args.length - 1

if typeof args[posCallback] == 'function'
callback = args[posCallback]

args[posCallback] = (args...) ->
result = callback.apply this, args
if result instanceof Promise
waitsForPromise(() => result)

fn.apply this, args

disableFocusMethods = ->
['fdescribe', 'ffdescribe', 'fffdescribe', 'fit', 'ffit', 'fffit'].forEach (methodName) ->
focusMethod = window[methodName]
Expand Down Expand Up @@ -124,4 +149,4 @@ buildTerminalReporter = (logFile, resolveWithExitCode) ->
new JasmineListReporter(options)
else
{TerminalReporter} = require 'jasmine-tagged'
new TerminalReporter(options)
new TerminalReporter(options)

0 comments on commit 619d643

Please sign in to comment.