我有一个异步函数,我想同时测试:成功和失败。成功时函数返回一个字符串,失败时抛出。我在测试失败方面惨遭失败。这是我的代码:
我通过评论失败的代码并在评论中添加结果来禁用
'use strict';
const path = require('path');
const fs = require('fs');
const getKmlFilename = require('./getKmlFileName.js');
const createGoodFolder = () => {
const folderPath = fs.mkdtempSync('/tmp/test-getKmlFilename-');
const fileDescriptor = fs.openSync(path.join(folderPath, 'doc.kml'), 'w');
fs.closeSync(fileDescriptor);
return folderPath;
};
const createEmptyFolder = () => fs.mkdtempSync('/tmp/test-getKmlFilename-');
describe('/app/lib/getKmlFilename', () => {
// Success tests
test('Should return a KML filename', async () => {
const result = await getKmlFilename(createGoodFolder());
expect(result).toMatch(/\.kml$/);
});
// Failure tests
test('Should throw if no KML files in folder', () => { …Run Code Online (Sandbox Code Playgroud) 给定这样的定义:
const querySchema = joi.object({
one: joi.string().min(1).max(255),
two: joi.string().min(1).max(255),
three: joi.string().min(1).max(255),
});
Run Code Online (Sandbox Code Playgroud)
有没有办法要求至少其中一个字段?我不在乎哪一个。
注意:为这个SO问题提供的解决方案对我不起作用,因为我有7个字段,并且它们可能会增加,因此,进行所有可能的组合都是不可行的。
在Joi API参考中找不到适用于此用例的任何方法。
任何帮助是极大的赞赏。
我在一般的互联网搜索中找不到,也没有通过阅读手册找到为什么无法使用此代码:
let a;
a || throw Error(`'a' is undefined or falsy`);
Run Code Online (Sandbox Code Playgroud)
我得到SyntaxError: Unexpected token throw.
有谁知道为什么不可能用这种方式来编码抛出?