Skip to content

Instantly share code, notes, and snippets.

@lolmaus
Last active February 28, 2020 15:53
Show Gist options
  • Save lolmaus/74ffd4d1c8bb5368c15d49a1a48a5fc4 to your computer and use it in GitHub Desktop.
Save lolmaus/74ffd4d1c8bb5368c15d49a1a48a5fc4 to your computer and use it in GitHub Desktop.
wait for
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--;
}
};
}
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);
});
}
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