Last active
February 28, 2020 15:53
-
-
Save lolmaus/74ffd4d1c8bb5368c15d49a1a48a5fc4 to your computer and use it in GitHub Desktop.
wait for
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
import Ember from 'ember'; | |
import { assert } from '@ember/debug'; | |
let pending = 0; | |
export function checkPending(): boolean { | |
return pending === 0; | |
} | |
export default function waitForInTests(_target: unknown, _propertyKey: string, desc: PropertyDescriptor): void { | |
assert('The @waitFor decorator must be applied to functions', desc && typeof desc.value === 'function'); | |
if (!Ember.testing) { | |
return; | |
} | |
const orig = desc.value; | |
desc.value = async function(...args: unknown[]): Promise<unknown> { | |
pending++; | |
try { | |
return await orig.apply(this, args); | |
} finally { | |
pending--; | |
} | |
}; | |
} |
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
import { registerWaiter, unregisterWaiter } from '@ember/test'; | |
import { checkPending } from 'my-app/utils/wait-for'; | |
/** | |
* start the mockserver before each scenario and cleans up after | |
* @param hooks | |
*/ | |
export default function setupWaitFor(hooks: NestedHooks): void { | |
hooks.beforeEach(function(): void { | |
registerWaiter(checkPending); | |
}); | |
hooks.afterEach(function(): void { | |
unregisterWaiter(checkPending); | |
}); | |
} |
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
import { next } from '@ember/runloop'; | |
export default class Foo { | |
@waitFor | |
doSomething () { | |
return next(() => console.log('this should appear before test finishes')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment