同时支持node和浏览器端的JS库如何写浏览器逻辑的测试用例??
decrypt方法 代码如下:
let workbookContent = Workbook.content;
if (!Buffer.isBuffer(workbookContent)) {
workbookContent = Buffer.from(workbookContent);
CFB.utils.prep_blob(workbookContent, 0);
}
output = xls97File.decrypt(cfb, workbookContent, password, input);
// 这里是 workbookContent 在node 是一个buffer , 可以直接给下面的 decrypt 用, 但是在浏览器则不是,所以需要转一下成 buffer
比如这种逻辑要怎么写测试用例来覆盖?
https://github1s.com/zurmokeeper/officecrypto-tool/blob/main/index.js#L19-L27
源码地址
async function decrypt(input, options) {
if (!Buffer.isBuffer(input)) {
// This is an ArrayBuffer in the browser. Convert to a Buffer.
if (ArrayBuffer.isView(input) || input instanceof ArrayBuffer) {
input = Buffer.from(input);
} else {
throw new Error('The input must be a buffer');
}
}
.... 省略一些代码
const Workbook = CFB.find(cfb, 'Workbook');
if (Workbook) {
let workbookContent = Workbook.content; // 这里是 workbookContent 在node 是一个buffer , 可以直接给下面的 decrypt 用, 但是在浏览器则不是,所以需要转一下成 buffer
if (!Buffer.isBuffer(workbookContent)) {
workbookContent = Buffer.from(workbookContent);
CFB.utils.prep_blob(workbookContent, 0);
}
output = xls97File.decrypt(cfb, workbookContent, password, input);
return output;
}
node 端我可以直接这样写: 使用Jest 写完直接运行,因为是node环境所以就可以直接覆盖了
it('agile decrypt', async () => {
try {
const input = await fs.readFile(`${filePath}/agile_pass_test.xlsx`);
const output = await officeCrypto.decrypt(input, {password: '123456'});
await fs.writeFile(`${filePath}/agile_out_success.xlsx`, output);
expect(200).toEqual(200);
} catch (error) {
throw error;
}
});
jest
可以配置测试浏览器环境https://www.jestjs.cn/docs/configuration#testenvironment-string
