deno.land / std@0.224.0 / expect / fn.ts
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.// Copyright 2019 Allain Lalonde. All rights reserved. ISC License.// deno-lint-ignore-file no-explicit-any ban-types
import { MOCK_SYMBOL, type MockCall } from "./_mock_util.ts";
export function fn(...stubs: Function[]): Function { const calls: MockCall[] = [];
const f = (...args: any[]) => { const stub = stubs.length === 1 // keep reusing the first ? stubs[0] // pick the exact mock for the current call : stubs[calls.length];
try { const returned = stub ? stub(...args) : undefined; calls.push({ args, returned, timestamp: Date.now(), returns: true, throws: false, }); return returned; } catch (err) { calls.push({ args, timestamp: Date.now(), returns: false, thrown: err, throws: true, }); throw err; } };
Object.defineProperty(f, MOCK_SYMBOL, { value: { calls }, writable: false, });
return f;}
Version Info