圆顶-功能-摩克
说明
一个函数模拟助手 .
重要性数字 : deepStrictEqual 用于参数比较, === 如果你通过了 strict: true
所需资源
- 节点: 22
安装
穿过 国家预防机制 作为 {\fn黑体\fs22\bord1\shad0\3ahbe\4ah00\fscx67\fscy66\2chffffff\3ch808080}你觉得怎么样。 。 。 .
npm i @chubbyts/[email protected]
使用量
创建功能移动
import { expect, test } from '@jest/globals'; // or 'vitest'
import { useFunctionMock } from '@chubbyts/chubbyts-function-mock/function-mock';
type MyFunction = (string: string, start: number, stop: number) => string;
test('my random test', () => {
const [myFunction, myFunctionMocks] = useFunctionMock<MyFunction>([
{ parameters: ['test', 0, 2], return: 'te' },
{
callback: (string: string, start: number, stop: number): string => {
expect(string).toBe('test');
expect(start).toBe(1);
expect(stop).toBe(2);
return 'es';
}
},
{ parameters: ['test', 0, 2], error: new Error('test') },
]);
expect(myFunction('test', 0, 2)).toBe('te');
expect(myFunction('test', 1, 2)).toBe('es');
try {
expect(myFunction('test', 2, 2)).toBe('st');
throw new Error('Expect fail');
} catch (e) {
expect(e).toMatchInlineSnapshot('[Error: test]');
}
// if you want to be sure, that all mocks are called
expect(myFunctionMocks.length).toBe(0);
});
创建对象雾
重要性: 不使用扩展运算符 ...myObject说吧.
import { expect, test } from '@jest/globals'; // or 'vitest'
import { useObjectMock } from '@chubbyts/chubbyts-function-mock/object-mock';
type MyType = {
substring: (string: string, start: number, stop: number) => string;
uppercase: (string: string) => string;
};
test('my random test', () => {
const [myObject, myObjectMocks] = useObjectMock<MyType>([
{ name: 'substring', parameters: ['test', 0, 2], return: 'te' },
{
name: 'substring',
callback: (string: string, start: number, stop: number): string => {
expect(string).toBe('test');
expect(start).toBe(1);
expect(stop).toBe(2);
return 'es';
}
},
{ name: 'uppercase', parameters: ['test'], error: new Error('test') },
]);
expect(myObject.substring('test', 0, 2)).toBe('te');
expect(myObject.substring('test', 1, 2)).toBe('es');
try {
expect(myObject.uppercase('test')).toBe('st');
throw new Error('Expect fail');
} catch (e) {
expect(e).toMatchInlineSnapshot('[Error: test]');
}
// if you want to be sure, that all mocks are called
expect(myObjectMocks.length).toBe(0);
});
版权
2026 道米尼克·佐格(英语: