小编FLC*_*FLC的帖子

尝试测试异步函数抛出时 Jest 测试失败

我有一个异步函数,我想同时测试:成功和失败。成功时函数返回一个字符串,失败时抛出。我在测试失败方面惨遭失败。这是我的代码:

  • getKmlFileName.test.js

我通过评论失败的代码并在评论中添加结果来禁用

    '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)

javascript async-await jestjs

11
推荐指数
1
解决办法
2万
查看次数

是否可以要求一组定义的字段中至少有一个字段?

给定这样的定义:

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参考中找不到适用于此用例的任何方法。

任何帮助是极大的赞赏。

javascript joi

4
推荐指数
2
解决办法
823
查看次数

不能使用`throw` after或(||)运算符

我在一般的互联网搜索中找不到,也没有通过阅读手册找到为什么无法使用此代码:

let a;
a || throw Error(`'a' is undefined or falsy`);
Run Code Online (Sandbox Code Playgroud)

我得到SyntaxError: Unexpected token throw.

有谁知道为什么不可能用这种方式来编码抛出?

javascript

2
推荐指数
1
解决办法
48
查看次数

标签 统计

javascript ×3

async-await ×1

jestjs ×1

joi ×1