(fn: Function, options: {flush?: boolean} = {})
| 949 | * @experimental |
| 950 | */ |
| 951 | export function fakeAsync(fn: Function, options: {flush?: boolean} = {}): (...args: any[]) => any { |
| 952 | const {flush = true} = options; |
| 953 | // Not using an arrow function to preserve context passed from call site |
| 954 | const fakeAsyncFn: any = function (this: unknown, ...args: any[]) { |
| 955 | const ProxyZoneSpec = getProxyZoneSpec(); |
| 956 | if (!ProxyZoneSpec) { |
| 957 | throwProxyZoneError(); |
| 958 | } |
| 959 | const proxyZoneSpec = ProxyZoneSpec.assertPresent(); |
| 960 | if (Zone.current.get('FakeAsyncTestZoneSpec')) { |
| 961 | throw new Error('fakeAsync() calls can not be nested'); |
| 962 | } |
| 963 | try { |
| 964 | // in case jasmine.clock init a fakeAsyncTestZoneSpec |
| 965 | if (!_fakeAsyncTestZoneSpec) { |
| 966 | const FakeAsyncTestZoneSpec = Zone && (Zone as any)['FakeAsyncTestZoneSpec']; |
| 967 | if (proxyZoneSpec.getDelegate() instanceof FakeAsyncTestZoneSpec) { |
| 968 | throw new Error('fakeAsync() calls can not be nested'); |
| 969 | } |
| 970 | |
| 971 | _fakeAsyncTestZoneSpec = new FakeAsyncTestZoneSpec() as FakeAsyncTestZoneSpec; |
| 972 | } |
| 973 | |
| 974 | let res: any; |
| 975 | const lastProxyZoneSpec = proxyZoneSpec.getDelegate(); |
| 976 | proxyZoneSpec.setDelegate(_fakeAsyncTestZoneSpec); |
| 977 | _fakeAsyncTestZoneSpec.lockDatePatch(); |
| 978 | try { |
| 979 | res = fn.apply(this, args); |
| 980 | if (flush) { |
| 981 | _fakeAsyncTestZoneSpec.flush(20, true); |
| 982 | } else { |
| 983 | flushMicrotasks(); |
| 984 | } |
| 985 | } finally { |
| 986 | proxyZoneSpec.setDelegate(lastProxyZoneSpec); |
| 987 | } |
| 988 | |
| 989 | if (!flush) { |
| 990 | if (_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length > 0) { |
| 991 | throw new Error( |
| 992 | `${_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length} ` + |
| 993 | `periodic timer(s) still in the queue.`, |
| 994 | ); |
| 995 | } |
| 996 | |
| 997 | if (_fakeAsyncTestZoneSpec.pendingTimers.length > 0) { |
| 998 | throw new Error( |
| 999 | `${_fakeAsyncTestZoneSpec.pendingTimers.length} timer(s) still in the queue.`, |
| 1000 | ); |
| 1001 | } |
| 1002 | } |
| 1003 | return res; |
| 1004 | } finally { |
| 1005 | resetFakeAsyncZone(); |
| 1006 | } |
| 1007 | }; |
| 1008 | fakeAsyncFn.isFakeAsync = true; |
no outgoing calls
no test coverage detected
searching dependent graphs…