TypeScript-有没有一种方法可以在document.fonts API上模拟/间谍?
是否有办法通过此测试,包括添加/删除任何数量的设置/安装依赖项,而不触发或消除任何错误/警告?TypeScript-有没有一种方法可以在document.fonts API上模拟/间谍?,typescript,unit-testing,jestjs,Typescript,Unit Testing,Jestjs,是否有办法通过此测试,包括添加/删除任何数量的设置/安装依赖项,而不触发或消除任何错误/警告? describe('Input', () => { beforeAll(() => { // Core part of the workaround...it seems the global prefix can also optionally be added global.document.fonts = { ready: { then
describe('Input', () => {
beforeAll(() => {
// Core part of the workaround...it seems the global prefix can also optionally be added
global.document.fonts = {
ready: {
then: jest.fn()
}
};
});
test('should render an Input', () => {
expect(renderInput()).toMatchSnapshot();
});
});
上下文:我有一个使用的生产原因,例如根据 在(出于遗留原因,如果有帮助,可以用TypeScript重写)和类中,可以将其编码为:
export default class Input extends React.Component<any, any> {
constructor(props: any) {
super(props);
(document: any).fonts.ready.then(() => this.setState({
value: props.defaultValue || ''
}));
}
...
不幸的是,这似乎导致了更多错误的兔子洞
尝试2:我的灵感来源于尝试至少看起来合理的东西(当与上面的尝试1结合时): 但是,这会导致测试无法运行,
无法在原语值上监视;未定义的给定值
探索jsdom,例如建议
文档。字体尚未实现。所以我现在很困惑。如果它没有在jsdom
中实现,为什么@ts不忽略它呢?
TS2739: Type '{ then: Mock<any, any>; }' is missing the following properties
from type 'Promise<FontFaceSet>': catch, [Symbol.toStringTag], finally
jest.spyOn(global.document.fonts, 'ready', 'get')