|
| 1 | +/** @babel */ |
| 2 | + |
| 3 | +export function beforeEach (fn) { |
| 4 | + global.beforeEach(function () { |
| 5 | + const result = fn() |
| 6 | + if (result instanceof Promise) { |
| 7 | + waitsForPromise(() => result) |
| 8 | + } |
| 9 | + }) |
| 10 | +} |
| 11 | + |
| 12 | +export function afterEach (fn) { |
| 13 | + global.afterEach(function () { |
| 14 | + const result = fn() |
| 15 | + if (result instanceof Promise) { |
| 16 | + waitsForPromise(() => result) |
| 17 | + } |
| 18 | + }) |
| 19 | +} |
| 20 | + |
| 21 | +['it', 'fit', 'ffit', 'fffit'].forEach(function (name) { |
| 22 | + module.exports[name] = function (description, fn) { |
| 23 | + if (fn === undefined) { |
| 24 | + global[name](description) |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + global[name](description, function () { |
| 29 | + const result = fn() |
| 30 | + if (result instanceof Promise) { |
| 31 | + waitsForPromise(() => result) |
| 32 | + } |
| 33 | + }) |
| 34 | + } |
| 35 | +}) |
| 36 | + |
| 37 | +export async function conditionPromise (condition, description = 'anonymous condition') { |
| 38 | + const startTime = Date.now() |
| 39 | + |
| 40 | + while (true) { |
| 41 | + await timeoutPromise(100) |
| 42 | + |
| 43 | + if (await condition()) { |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + if (Date.now() - startTime > 5000) { |
| 48 | + throw new Error('Timed out waiting on ' + description) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +export function timeoutPromise (timeout) { |
| 54 | + return new Promise(function (resolve) { |
| 55 | + global.setTimeout(resolve, timeout) |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +function waitsForPromise (fn) { |
| 60 | + const promise = fn() |
| 61 | + global.waitsFor('spec promise to resolve', function (done) { |
| 62 | + promise.then(done, function (error) { |
| 63 | + jasmine.getEnv().currentSpec.fail(error) |
| 64 | + done() |
| 65 | + }) |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +export function emitterEventPromise (emitter, event, timeout = 15000) { |
| 70 | + return new Promise((resolve, reject) => { |
| 71 | + const timeoutHandle = setTimeout(() => { |
| 72 | + reject(new Error(`Timed out waiting for '${event}' event`)) |
| 73 | + }, timeout) |
| 74 | + emitter.once(event, () => { |
| 75 | + clearTimeout(timeoutHandle) |
| 76 | + resolve() |
| 77 | + }) |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +export function promisify (original) { |
| 82 | + return function (...args) { |
| 83 | + return new Promise((resolve, reject) => { |
| 84 | + args.push((err, ...results) => { |
| 85 | + if (err) { |
| 86 | + reject(err) |
| 87 | + } else { |
| 88 | + resolve(...results) |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + return original(...args) |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +export function promisifySome (obj, fnNames) { |
| 98 | + const result = {} |
| 99 | + for (const fnName of fnNames) { |
| 100 | + result[fnName] = promisify(obj[fnName]) |
| 101 | + } |
| 102 | + return result |
| 103 | +} |
0 commit comments