在函数(本例中为ngOnInit)中,我订阅了一个observable。当我获得新数据时,我用该数据更新另一个函数: ngOnInit(): void {
this.observable$.subscribe({
next: (data) => {
this.function(data)
},
});
}
function(data): void {
console.log(data)
} 当我需要测试它的时候,问题就来了。我已经尝试了我能找到的所有在线答案的方法,比如In Jest, how can I unit t
我想用jest模拟axios调用,但在使用此测试用例时,仅使用getData就会出现错误:
jest.mock('axios')
const mockAxios = axios as AxiosMock
it('make api call to get data', () => {
// call this first
mockAxios.mockResolvedValue(data)
TestPublications.getData()
expect(mockAxios.get).toHaveBeenCalledWith(
有没有一种方法可以让我在运行时找出一个模块是否被jest模仿了? 由于模拟的模块通常是必需的,因此代码会被执行(如下所示:jest module executed even when mocked 我们需要这样做,因为当没有设置强制环境变量时,我们对每个文件的检查都会提前失败,这会导致即使模块被模拟,我们的测试也会失败。 if (!process.env.SOME_ENV) {
throw new Error(`Mandatory environment variable 'SOME_ENV' not set`)
} 我们正在寻找类似这样的东西: if (!process
这是我要测试的课程:
CommandFactory.ts
import type {ChatInputCommandInteraction} from 'discord.js';
import type CommandInterface from './interfaces/CommandInterface';
import GameCommand from './commands/GameCommand';
import SettingCommand from './commands/SettingCommand';
impor
我的setupTests.ts中有以下内容:
globalThis.Notification = ({
requestPermission: jest.fn(),
permission: "granted",
} as unknown) as jest.Mocked<typeof Notification>
然而,在一个react项目中的jest测试中,我不断地得到这个错误:
TypeError: Notification is not a constructor
21 | export function showNotification(t
我正在尝试检查同一文件中两个函数的实现情况。其中一个函数将调用另一个函数,我想检查它是否真的发生了。 import * as strings from './strings';
const generateUuidSpy = jest.spyOn(strings, 'generateUuid');
describe('getContextId()', () => {
it('it should return a string id if given context and number', () => {