Skip to content

Commit b78c71d

Browse files
Type observables based on how they're consumed by AVA
Detect subscribables and convert to promise. Simplify type description to match what AVA actually requires. Co-authored-by: Mark Wubben <[email protected]>
1 parent a07daeb commit b78c71d

File tree

4 files changed

+13
-29
lines changed

4 files changed

+13
-29
lines changed

index.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/// <reference types="node"/>
22

3-
export interface ObservableLike {
4-
subscribe(observer: (value: unknown) => void): void;
5-
[Symbol.observable](): ObservableLike;
3+
export interface Subscribable {
4+
subscribe(observer: {
5+
error(err: any): void,
6+
complete(): void,
7+
}): void
68
}
79

810
export type Constructor = (new (...args: Array<any>) => any);
@@ -472,7 +474,7 @@ export interface CbExecutionContext<Context = unknown> extends ExecutionContext<
472474
end(error?: any): void;
473475
}
474476

475-
export type ImplementationResult = PromiseLike<void> | ObservableLike | void;
477+
export type ImplementationResult = PromiseLike<void> | Subscribable | void;
476478
export type Implementation<Context = unknown> = (t: ExecutionContext<Context>) => ImplementationResult;
477479
export type CbImplementation<Context = unknown> = (t: CbExecutionContext<Context>) => ImplementationResult;
478480

lib/test.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
22
const concordance = require('concordance');
3-
const observableToPromise = require('observable-to-promise');
43
const isPromise = require('is-promise');
5-
const isObservable = require('is-observable');
64
const plur = require('plur');
75
const assert = require('./assert');
86
const nowAndTimers = require('./now-and-timers');
@@ -577,12 +575,17 @@ class Test {
577575
return this.finishPromised();
578576
}
579577

580-
const returnedObservable = isObservable(result.retval);
578+
const returnedObservable = result.retval !== null && typeof result.retval === 'object' && typeof result.retval.subscribe === 'function';
581579
const returnedPromise = isPromise(result.retval);
582580

583581
let promise;
584582
if (returnedObservable) {
585-
promise = observableToPromise(result.retval);
583+
promise = new Promise((resolve, reject) => {
584+
result.retval.subscribe({
585+
error: reject,
586+
complete: () => resolve()
587+
});
588+
});
586589
} else if (returnedPromise) {
587590
// `retval` can be any thenable, so convert to a proper promise.
588591
promise = Promise.resolve(result.retval);

package-lock.json

Lines changed: 0 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,13 @@
8585
"indent-string": "^4.0.0",
8686
"is-ci": "^2.0.0",
8787
"is-error": "^2.2.2",
88-
"is-observable": "^2.0.0",
8988
"is-plain-object": "^3.0.0",
9089
"is-promise": "^2.1.0",
9190
"lodash": "^4.17.15",
9291
"matcher": "^2.1.0",
9392
"md5-hex": "^3.0.1",
9493
"micromatch": "^4.0.2",
9594
"ms": "^2.1.2",
96-
"observable-to-promise": "^1.0.0",
9795
"ora": "^4.0.3",
9896
"p-map": "^3.0.0",
9997
"pkg-conf": "^3.1.0",

0 commit comments

Comments
 (0)