我有一个容器类A,它实例化了另一个B类:
class A {
constructor(){
this.initClassB()
}
private initClassB() : void{
const options = {id : 0, ... etc}
new B(options)
}
}
在我的测试文件中,我实例化了类A,并模拟了类B:
import { mocked } from 'ts-jest/utils';
import { ClassA} from '../ClassA';
import { ClassB} from '../ClassB';
jest.mock('../ClassB');
const mockedClassB = mocked(ClassB, true);
describe('class B', () => {
it('check if the minimum requirement are ok', () => {
const { text, url } = ClassA.props.options;
const expectedOptions = {
event: ClassA.event,
id: ClassA.getDefaultSiteId(),
loadContent: () => '',
loader: ClassA.loader,
text: text,
url: url,
};
// test fail whereas Expected/Received are identical
expect(mockedClassB ).toHaveBeenCalledWith(expectedOptions)
// fail with error : serialize to the same string
expect(mockedCKTable.mock.calls[0][0]).toMatchObject(expectedOptions);
});
});
这是方法toHaveBeenCalledWith
的错误
这是toMatchObject
方法的错误:
以及依赖关系版本:
{
"@types/jest": "^26.0.24",
"ts-jest": "^27.0.4",
"jest": "^27.0.6",
"typescript": "~4.1.3",
"@types/jest": "^26.0.24",
}
我哪里出问题了?
发布于 2021-12-29 09:18:29
在这种情况下,非对称匹配器不起作用:
但!当我像这样实现时,测试是绿色的:
const expectedOptions = {
id: 0,
// etc ...
};
const mockEntries = Object.entries(mockedCKTable.mock.calls[0][0]);
const expectedEntries = Object.entries(expectedOptions);
expect(JSON.stringify(mockEntries)).toEqual(JSON.stringify(expectedEntries));
https://stackoverflow.com/questions/70507758
复制相似问题