Skip to content

Commit 2da9b21

Browse files
fix: types for waterfall hooks (#190)
1 parent 4b099d8 commit 2da9b21

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

lib/__tests__/AsyncSeriesHooks.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,35 @@ describe("AsyncSeriesWaterfallHook", () => {
7171

7272
expect(result).toMatchSnapshot();
7373
});
74+
75+
it("should work with undefined", async () => {
76+
const hook = new AsyncSeriesWaterfallHook(["x"]);
77+
hook.tap("number", () => 42);
78+
hook.tap("undefined", () => undefined);
79+
return expect(hook.promise()).resolves.toBe(42);
80+
});
81+
82+
it("should work with void", async () => {
83+
const hook = new AsyncSeriesWaterfallHook(["x"]);
84+
hook.tap("number", () => 42);
85+
hook.tap("undefined", () => {});
86+
return expect(hook.promise()).resolves.toBe(42);
87+
});
88+
89+
it("should work with undefined and number again", async () => {
90+
const hook = new AsyncSeriesWaterfallHook(["x"]);
91+
hook.tap("number", () => 42);
92+
hook.tap("undefined", () => {});
93+
hook.tap("number-again", () => 43);
94+
return expect(hook.promise()).resolves.toBe(43);
95+
});
96+
97+
it("should work with null", async () => {
98+
const hook = new AsyncSeriesWaterfallHook(["x"]);
99+
hook.tap("number", () => 42);
100+
hook.tap("undefined", () => null);
101+
return expect(hook.promise()).resolves.toBeNull();
102+
});
74103
});
75104

76105
describe("AsyncSeriesLoopHook", () => {

lib/__tests__/SyncWaterfallHook.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,43 @@ describe("SyncWaterfallHook", () => {
2222
);
2323
});
2424

25+
it("should work", () => {
26+
const hook = new SyncWaterfallHook(["x"]);
27+
hook.tap("number", () => 42);
28+
hook.tap("string", () => "str");
29+
hook.tap("false", () => false);
30+
return expect(hook.call()).toBe(false);
31+
});
32+
33+
it("should work with undefined", async () => {
34+
const hook = new SyncWaterfallHook(["x"]);
35+
hook.tap("number", () => 42);
36+
hook.tap("undefined", () => undefined);
37+
return expect(hook.call()).toBe(42);
38+
});
39+
40+
it("should work with void", async () => {
41+
const hook = new SyncWaterfallHook(["x"]);
42+
hook.tap("number", () => 42);
43+
hook.tap("undefined", () => {});
44+
return expect(hook.call()).toBe(42);
45+
});
46+
47+
it("should work with undefined and number again", async () => {
48+
const hook = new SyncWaterfallHook(["x"]);
49+
hook.tap("number", () => 42);
50+
hook.tap("undefined", () => {});
51+
hook.tap("number-again", () => 43);
52+
return expect(hook.call()).toBe(43);
53+
});
54+
55+
it("should work with null", async () => {
56+
const hook = new SyncWaterfallHook(["x"]);
57+
hook.tap("number", () => 42);
58+
hook.tap("undefined", () => null);
59+
return expect(hook.call()).toBeNull();
60+
});
61+
2562
it("should allow to create sync hooks", async () => {
2663
const hook = new SyncWaterfallHook(["arg1", "arg2"]);
2764

tapable.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class SyncLoopHook<
9595
export class SyncWaterfallHook<
9696
T,
9797
AdditionalOptions = UnsetAdditionalOptions
98-
> extends SyncHook<T, AsArray<T>[0], AdditionalOptions> {}
98+
> extends SyncHook<T, AsArray<T>[0] | void, AdditionalOptions> {}
9999

100100
declare class AsyncHook<
101101
T,
@@ -137,7 +137,7 @@ export class AsyncSeriesLoopHook<
137137
export class AsyncSeriesWaterfallHook<
138138
T,
139139
AdditionalOptions = UnsetAdditionalOptions
140-
> extends AsyncHook<T, AsArray<T>[0], AdditionalOptions> {}
140+
> extends AsyncHook<T, AsArray<T>[0] | void, AdditionalOptions> {}
141141

142142
type HookFactory<H> = (key: any, hook?: H) => H;
143143

0 commit comments

Comments
 (0)