export function run() { describe("APIFactory test", () => { let factory = new Leaf.APIFactory({ prefix: "/api/", bodyType: "json", errorMapper: (err: Error) => { return err }, mock: true, serializer: Leaf.Serializer.getDefault() }) class APIService extends Leaf.Service { states = {} public name: string = "APIService" register = factory.createAPIFunction<{ username: string email: string password: string }, boolean>({ method: "GET", path: "user" }) echo = factory.createAPIFunction<{ echo: string }, string>({ method: "GET", path: "echo" }) echoError = factory.createAPIFunction<{ message: string detail: string }, null>({ method: "POST", path: "echo/error" }) test = factory.createAPIFunction<{ id: string problem: string }, any>({ method: "POST", path: "test/search/:id" }).mock({ id: "id", problem: "problem" }, null, "yes").mock({ id: "error", problem: "hehe" }, new Error()) } let s = new APIService() it("Mock", (done) => { s.test({ id: "id", problem: "problem" }, (err, result) => { console.log("???", err, result) if (result == "yes") { done() } else { throw new Error("Fail to mock API") } }) }) it("Mock with Error", (done) => { s.test({ id: "error", problem: "hehe" }, (err) => { if (!err) { throw new Error("Fail to mock error") } done() }) }) }) }