mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2024-11-10 10:17:11 +03:00
89f1710cfe
Signed-off-by: Nathan Sobo <nathan@github.com>
75 lines
1.6 KiB
JavaScript
75 lines
1.6 KiB
JavaScript
/** @babel */
|
|
|
|
export function beforeEach (fn) {
|
|
global.beforeEach(function () {
|
|
const result = fn()
|
|
if (result instanceof Promise) {
|
|
waitsForPromise(() => result)
|
|
}
|
|
})
|
|
}
|
|
|
|
export function afterEach (fn) {
|
|
global.afterEach(function () {
|
|
const result = fn()
|
|
if (result instanceof Promise) {
|
|
waitsForPromise(() => result)
|
|
}
|
|
})
|
|
}
|
|
|
|
['it', 'fit', 'ffit', 'fffit'].forEach(function (name) {
|
|
module.exports[name] = function (description, fn) {
|
|
global[name](description, function () {
|
|
const result = fn()
|
|
if (result instanceof Promise) {
|
|
waitsForPromise(() => result)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
export async function conditionPromise (condition) {
|
|
const startTime = Date.now()
|
|
|
|
while (true) {
|
|
await timeoutPromise(100)
|
|
|
|
if (await condition()) {
|
|
return
|
|
}
|
|
|
|
if (Date.now() - startTime > 5000) {
|
|
throw new Error("Timed out waiting on condition")
|
|
}
|
|
}
|
|
}
|
|
|
|
export function timeoutPromise (timeout) {
|
|
return new Promise(function (resolve) {
|
|
global.setTimeout(resolve, timeout)
|
|
})
|
|
}
|
|
|
|
function waitsForPromise (fn) {
|
|
const promise = fn()
|
|
global.waitsFor('spec promise to resolve', function (done) {
|
|
promise.then(done, function (error) {
|
|
jasmine.getEnv().currentSpec.fail(error)
|
|
done()
|
|
})
|
|
})
|
|
}
|
|
|
|
export function emitterEventPromise (emitter, event, timeout = 15000) {
|
|
return new Promise((resolve, reject) => {
|
|
const timeoutHandle = setTimeout(() => {
|
|
reject(new Error(`Timed out waiting for '${event}' event`))
|
|
}, timeout)
|
|
emitter.once(event, () => {
|
|
clearTimeout(timeoutHandle)
|
|
resolve()
|
|
})
|
|
})
|
|
}
|