React native React本机测试失败:“;TypeError:无法读取属性';财政司司长';“未定义”的定义;
当我运行React native React本机测试失败:“;TypeError:无法读取属性';财政司司长';“未定义”的定义;,react-native,jestjs,React Native,Jestjs,当我运行npm测试时: ➜ mobile git:(develop) ✗ npm test > jest FAIL __tests__/index.js ● Test suite failed to run TypeError: Cannot read property 'fs' of undefined at Object.<anonymous> (node_modules/react-native-cached-image/utils/f
npm测试时
:
➜ mobile git:(develop) ✗ npm test
> jest
FAIL __tests__/index.js
● Test suite failed to run
TypeError: Cannot read property 'fs' of undefined
at Object.<anonymous> (node_modules/react-native-cached-image/utils/fsUtils.js:9:12)
at Object.<anonymous> (node_modules/react-native-cached-image/ImageCacheManager.js:5:13)
at Object.<anonymous> (node_modules/react-native-cached-image/CachedImage.js:13:23)
➜ 移动git:(开发)✗ npm试验
>开玩笑
失败_测试_/index.js
● 测试套件无法运行
TypeError:无法读取未定义的属性“fs”
反对。(node_modules/react native cached image/utils/fsUtils.js:9:12)
反对。(node_modules/react native cached image/ImageCacheManager.js:5:13)
反对。(node_modules/react native cached image/CachedImage.js:13:23)
我发现它与react native cached image
依赖于fs
我一定要嘲笑fs吗?我尝试了两种方法,但都没有成功。使用fs模拟组件 如果可能的话,我建议您不要模拟fs,而是模拟react本机映像缓存,因为该包的一个依赖项使用fs。你可以看到 您可以只模拟本地映像缓存,如下所示:
jest.mock('react-native-img-cache', () => {
return {
DocumentDir: () => {},
ImageCache: {
get: {
clear: () => {}
}
}
}
})
同样,应该可以模拟使用fs并导致错误的react native fetch blob。return语句上的属性取决于映像缓存库使用的方法和属性。也许您不需要下面列出的那么多,但现在您知道可能不止fs
jest.mock('react-native-fetch-blob', () => {
return {
DocumentDir: () => {},
fetch: () => {},
base64: () => {},
android: () => {},
ios: () => {},
config: () => {},
session: () => {},
fs: {
dirs: {
MainBundleDir: () => {},
CacheDir: () => {},
DocumentDir: () => {},
},
},
wrap: () => {},
polyfill: () => {},
JSONStream: () => {}
}
})
模拟fs 如果您想/需要专门模拟fs,可以执行以下操作:
jest.mock('react-native-img-cache', () => {
return {
DocumentDir: () => {},
ImageCache: {
get: {
clear: () => {}
}
}
}
})
fs = require("fs")
fs.readFile = function (filename, cb) {
cb(null, new Buffer("fake contents");
};
// etc
但请记住,fs是在依赖包中的某个地方使用的。在这种情况下,我不确定代码是否正常工作,这就是我将其他选项放在顶部的原因。使用fs模拟组件 如果可能的话,我建议您不要模拟fs,而是模拟react本机映像缓存,因为该包的一个依赖项使用fs。你可以看到 您可以只模拟本地映像缓存,如下所示:
jest.mock('react-native-img-cache', () => {
return {
DocumentDir: () => {},
ImageCache: {
get: {
clear: () => {}
}
}
}
})
同样,应该可以模拟使用fs并导致错误的react native fetch blob。return语句上的属性取决于映像缓存库使用的方法和属性。也许您不需要下面列出的那么多,但现在您知道可能不止fs
jest.mock('react-native-fetch-blob', () => {
return {
DocumentDir: () => {},
fetch: () => {},
base64: () => {},
android: () => {},
ios: () => {},
config: () => {},
session: () => {},
fs: {
dirs: {
MainBundleDir: () => {},
CacheDir: () => {},
DocumentDir: () => {},
},
},
wrap: () => {},
polyfill: () => {},
JSONStream: () => {}
}
})
模拟fs 如果您想/需要专门模拟fs,可以执行以下操作:
jest.mock('react-native-img-cache', () => {
return {
DocumentDir: () => {},
ImageCache: {
get: {
clear: () => {}
}
}
}
})
fs = require("fs")
fs.readFile = function (filename, cb) {
cb(null, new Buffer("fake contents");
};
// etc
但请记住,fs是在依赖包中的某个地方使用的。在这种情况下,我不确定代码是否正常工作,这就是我将其他选项放在顶部的原因