From 9d1baf07ec33d450bea8d87a61400898393fd7dc Mon Sep 17 00:00:00 2001 From: chensheng Date: Mon, 16 Mar 2020 10:11:22 +0800 Subject: [PATCH] check invalid before names --- lib/Hook.js | 3 +++ lib/__tests__/Hook.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/Hook.js b/lib/Hook.js index 55149f8..5eb6fa3 100644 --- a/lib/Hook.js +++ b/lib/Hook.js @@ -157,6 +157,9 @@ class Hook { continue; } if (before.size > 0) { + if (i === 0) { + throw new Error(`Invalid before names [${[...before]}]`); + } continue; } } diff --git a/lib/__tests__/Hook.js b/lib/__tests__/Hook.js index 71a73d5..0cb92d0 100644 --- a/lib/__tests__/Hook.js +++ b/lib/__tests__/Hook.js @@ -67,4 +67,21 @@ describe("Hook", () => { hook.call(); expect(calls).toEqual(["E", "F", "C", "D", "B", "A"]); }); + + it("should throw error when pass invalid before names", () => { + const hook = new SyncHook(); + + const calls = []; + hook.tap("A", () => calls.push("A")); + + expect(() => { + hook.tap( + { + name: "B", + before: "C" + }, + () => calls.push("B") + ); + }).toThrowError("Invalid before names [C]"); + }); });