有什么方法可以对 IPFS 的基本功能进行单元测试,特别是IPFS.create()
?我正在使用 TypeScript 和 Jest。我总是遇到两个错误。首先我得到:Error: digest should be a Uint8Array
在<project-dri>/node_modules/multihashing-async/node_modules/multihashes/src/index.js:132:11
at <project-dir>/node_modules/multihashing-async/src/sha.js:54:22)
。如果我在已使用此错误调用它之后运行任何使用的测试,IPFS.create()
则会出现另一个错误:LockExistsError: Lock already being held for file: ~/.jsipfs/repo.lock at Object.<anonymous>.exports.lock (<project-dir>/node_modules/ipfs-repo/src/lock.js:37:13) { code: 'ERR_LOCK_EXISTS' }
.
const ipfsCreateMemo: IpfsCreateMemo = {};
export async function useNode(
callback = async (node: Object) => { },
errorCallback = async (err: Error) => { },
props = {}
) {
const memo = String(Object.entries(props)) || 'useNode';
try {
if (memo in ipfsCreateMemo === false) {
// There is no way to get to the next line of code
// The following line triggers every error
ipfsCreateMemo[memo] = await IPFS.create();
}
await callback(ipfsCreateMemo[memo]);
ipfsCreateMemo[memo].close();
} catch (e) {
console.log(e);
errorCallback(e);
}
}
export async function getLatest() {
return await useNode(async (node) => {
return node.refs.local();
});
}
// the test
test("Read the latest", async () => {
const mockGetLatest = jest.fn(async () => await getLatest());
await mockGetLatest();
expect(mockGetLatest).toHaveReturned();
});
我已经能够从我的网络应用程序中毫无错误地调用这些方法,但前提是我在调用它们之前等待几秒钟。我了解 IPFS 可能会受到其类似 alpha 的条件的限制。有什么方法可以进行工作单元测试吗?尽管如此?