我正在尝试编写快照测试,以检查是否返回了正确的图像。当我只使用零转换,结果总是'1‘,这不是很有用。我上了这个网站,把这个添加到我的package.json中
"transform": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/jest/fileTransformer.js"
}
并创建一个名为fileTransformer的文件,如下所示
// fileTransformer.js
const path = require('path');
module.exports = {
process(src, filename, config, options) {
return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';';
},
};
现在,测试产生了有用的快照,这是很棒的,但是我在控制台中没有收到如下所示的警告
console.error node_modules/prop-types/checkPropTypes.js:20
Warning: Failed prop type: Invalid prop `source` supplied to `Image`.
in Image
发布于 2019-11-18 02:59:50
您可以查看moduleNameMapper
配置
"^[./a-zA-Z0-9$_-]+\\.png$": "<rootDir>/RelativeImageStub.js",
在文件中,您可以导出一个虚拟图像路径。
RelativeImageStub.js
export default '/dummy/path/to/dummyImage.png';
您还应该能够通过导入图像来模拟结果:
Component.spec.js
import Component from './Component';
jest.mock('./path/to/the/image.png', () => "dummy/path/image.png");
Component.js
import image from './path/to/the/image.png';
export default function Component() {
return <Image source={image} />;
}
这样,如果指定的路径不存在,您也会得到一个错误。
https://stackoverflow.com/questions/58876218
复制相似问题