我想使用jest.spyOn来模拟仅用于the first call的方法的实现。在第二、第三次...nth调用时,要调用它的实际实现。
我在ts中尝试了以下几种方法,但都不起作用:
import handler from '../src/handler';
import * as cRedis from '../src/redis';
jest.spyOn(cRedis, 'rGetAsync');
describe('handle cart quantity validation', () => {
test
我期望当我使用jest.mock()模拟一个模块并将函数实现传递给每个对象时,它们会转到测试中。
import * as services from '_services';
// I thought this would be enough to mock 'load_basket'
jest.mock('_services', () => ({
load_basket: jest.fn(() => Promise.resolve([]))
}));
describe('WB Component', () =
我使用Jest在NodeJS上编写单元测试。有一种方法可以返回一个实体或一个实体数组。当我试图模拟这个方法的返回值时,我只能传递数组,但是需要一个实体。
npm i jest typeorm
const manager = new EntityManager(null);
const sale = new Sale();
jest.spyOn(manager, 'create').mockReturnValue(sale);
最后一个字符串导致错误:Argument of type 'Sale' is not assignable to parameter
在Vue 2.6.12 with Jest:"^27.5.1"和"@vue/test-utils": "^1.3.0",中,我尝试使用jest模拟async方法。当我断言我的方法已被调用1次时,它会返回未被调用的方法。我是不是做错了什么事?
更新电子邮件测试
import {mount} from '@vue/test-utils';
import UpdateEmail from './components/update-email.vue';
//validate form metho
显然,mock.mockRestore()没有恢复使用jest.mock()创建的模拟的原始实现。
// a.js
export default class A {}
// b.js
import A from './a';
export default class B extends A {}
// test.js
import A from './a';
import B from './b';
jest.mock('./a');
jest.mock('./b');
const b = new B(
我试图模拟具有静态getter的日志记录服务。
static get error(): Function {
return console.error.bind(console, 'Error: ');
}
尝试过:jest.spyOn(ConsoleLoggingService, 'error').mockImplementation(() => 'blah');
但我得到了TypeError: Cannot set property info of function ConsoleLoggingService() {}
这是我正在进行的一个测试:
it('dispatches the logout action', () => {
const store = mockStore({});
store.dispatch(logout()); // TODO: logout() has a function in its payload that gives an error
const expectedActions = store.getActions();
expect(expectedActions).toMatchSnapshot();
});
它给了我以
如果我们使用jest和typescript,其中使用了一个交集观察者,那么模拟交集观察者将变得很困难。到目前为止,我在: beforeEach(() => {
// IntersectionObserver isn't available in test environment
const mockIntersectionObserver = class {
observe() {
console.log(this);
}
unobserve() {
console.log(this);
}
disco
我在跟踪这条线索。
functions.js
export const a = (x) => { a very complicated function };
export const b = (x) => exports.a(x+1);
functions.test.js
import * as functions from './functions';
describe('b', () => {
test('calling b calls a with x+1', () => {
functio
背景
因此,我试图测试一个调用自定义钩子的,并根据钩子的返回值,该组件将呈现不同的内容。关于一个例子,见下文:
// App.js
import "./styles.css";
import useCustomHook from "./useHook";
export default function App() {
const [state, handler] = useCustomHook();
return state ? <div>state is true</div> : <div>state is f