我试图模拟具有静态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() {}
我正试图为我的单元测试项目集成jest。
我遵循了的指示。
当我运行时,作为ENOENT: no such file or directory, open 'E:\source-code\packages\core\src\view\ng_module.ts',我面临一个奇怪的错误。
我的文件夹结构没有一个名为package.After的文件夹,我发现packages\core\src\view\ng_module.ts'是由npm包使用的(角:P)
我不知道这个错误是如何被抛出的,当我穿越业力时,这个错误不会发生。
这是我给jest "jest"
在我的Jest测试用例中,我想模拟一个全局变量,但是我得到了Cannot assign to '' because it is an import.。怎么解决这个问题?
我想介绍一个场景,其中我有一个有效的token,然后做一些事情。
试过了,但没有运气:
jest.mock('jwtToken', () => () => ({ jwtToken: 'abc' }));
Service.ts
export let token: string;
@Injectable()
export class MyService {
我正在用jest.fn来模拟两个函数:
let first = jest.fn();
let second = jest.fn();
我如何断言first在second之前调用
我要找的是类似于 .calledBefore断言的东西。
更新我使用了这个简单的“临时”解决方法
it( 'should run all provided function in order', () => {
// we are using this as simple solution
// and asked this question here https://stackover
我正在写一个测试,看起来是这样的:
import { getAllUsers } from "./users";
import { getMockReq, getMockRes } from "@jest-mock/express";
import User from "../../models/User";
jest.mock("../../models/User", () => ({
find: jest.fn(), // I want to change the return value of this mock
显然,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(
基本上,我想确保使用某些属性调用分析方法,但到目前为止,它还不起作用:
Cannot spy the logAppOpen property because it is not a function; undefined given instead
库被成功地模拟,因为我可以看到控制台注销了我的jest.fn():
jest.mock('@react-native-firebase/analytics', () => {
return () => ({
logAppOpen: jest.fn(() => console.log('
出于某种原因,我得到了这个错误:
Cannot spy the handleError property because it is not a function; undefined given instead
我用间谍的方法..。
const spyHandleError = jest.spyOn(handleError, 'handleError');
...to检查是否调用了handleError函数:
expect(spyHandleError).toHaveBeenCalled();
我的handleError函数看起来是这样的:
import { reduxAct
我有一些在函数中包含函数的代码,我希望能够对父函数中的函数进行单元测试。
我希望有测试,单元测试这些,并监视他们(这两个需求是必要的)。
示例:
export default parentFunction = () => {
const innerFunction = () => {
//that does stuff
}
const anotherInnerFunction = () => {
//that does more stuff
}
//and at some point, the functions are called
我正试着在Jest中嘲笑我的一个动作创作者。实际上,我正在测试我的React组件。我需要检查action-creator函数是否在某个事件上被调用。我试图通过jest.mock模拟该函数,但抛出错误说: Do not import `@jest/globals` outside of Jest test environment. 以下是我到目前为止尝试过的方法,但它不起作用: describe('>> SCREEN -- Tests Step1', () => {
describe('• handleSubmit function tests
我需要使用jest和moment运行一些测试。某些导入的函数使用moment current date (moment() )。但我似乎找不到一种方法来总是运行相同的日期来测试或模拟moment构造函数,比如pin moment date at moment('2020-07-05'),即使今天是2020-07-10,所以测试应该总是在第5天运行。 My./UtilsModule文件: import moment from 'moment'
export const getIntervalDates = groupOfDates => {
cons