javascript - mocha 中的“it”无法使用 await 运行

标签 javascript node.js testing mocha.js

mocha 中的“it”不能在使用 await 时运行。

我使用函数异步读取文件并根据返回值运行多个循环来处理我的测试。 我还在循环中多次使用相同的文件读取函数。 我需要使用 await 从该函数中获取解析值。 如果我删除 await,则“它”会运行,但我的功能仍未解决。

const subList1 = [
    {name: 'value1', info: 'value1'},
    {name: 'value2', info: 'value2'},
    {name: 'value3', info: 'value3'},
]
const subList2 = [
    {name: 'value1', info: 'value1'},
    {name: 'value2', info: 'value2'},
    {name: 'value3', info: 'value3'},
]
const subList3 = [
    {name: 'value1', info: 'value1'},
    {name: 'value2', info: 'value2'},
    {name: 'value3', info: 'value3'},
]

const mainList = [
    subList1, subList2, subList3, 
    ]

function getList() {
    return mainList;
}

describe('Test', async function () {
    const list = await getList();
    list.forEach(mainListItem => {
        describe('Main Test', function () {
            mainListItem.forEach(subListItem => {
                describe(subListItem.name, function () {
                    let valueInCaps;
                    const name = subListItem.name;
                    console.log(`name: ${JSON.stringify(name)}`);
                    before(function () {
                        valueInCaps = subListItem.info.toUpperCase();
                    })
                    it(name, function () {
                        console.log(valueInCaps);
                    })
                })
            })

        });
    });
})

预期:

name: "value1"
name: "value2"
name: "value3"
name: "value1"
name: "value2"
name: "value3"
name: "value1"
name: "value2"
name: "value3"


  Test
    Main Test
      value1
VALUE1
        √ value1
      value2
VALUE2
        √ value2
      value3
VALUE3
        √ value3
    Main Test
      value1
VALUE1
        √ value1
      value2
VALUE2
        √ value2
      value3
VALUE3
        √ value3
    Main Test
      value1
VALUE1
        √ value1
      value2
VALUE2
        √ value2
      value3
VALUE3
        √ value3

实际:

名称:“值 1” 名称:“值2” 名称:“值3” 名称:“值1” 名称:“值2” 名称:“值3” 名称:“值1” 名称:“值2” 名称:“value3”

最佳答案

如果你进入mocha websites您会找到一个关于如何构建测试的好例子。我相信您的问题是您没有在测试中声明任何内容。您没有测试任何假设。

// require an assertion library
var assert = require('assert');

    describe('Test', async function () {
        const list = await getList();
        list.forEach(mainListItem => {
            describe('Main Test', function () {
                mainListItem.forEach(subListItem => {
                    describe(subListItem.name, function () {
                        let valueInCaps;
                        const name = subListItem.name;
                        console.log(`name: ${JSON.stringify(name)}`);
                        before(function () {
                            valueInCaps = subListItem.info.toUpperCase();
                        })
                        it(name, function () {
                            console.log(valueInCaps);
                            assert.equal(someCondition)
                        })
                    })
                })

            });
        });
    })

关于javascript - mocha 中的“it”无法使用 await 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56231067/

相关文章:

javascript - 我可以在 Node.js 的 javascript 中使用 catch(e if e instanceof SyntaxError) 吗?

javascript - 使用 ng-click 从模式重定向并且数据关闭不起作用

javascript - 类型错误 : Cannot read property 'startsWith' of undefined node. js V12.18.2

node.js - discord.js - 排行榜/顶部命令显示错误顺序

PHPSpec 和 Laravel

java - 何时在 Mockito 中使用 when()

javascript - 在函数内定义函数,如果存在则运行

javascript - 清理充满具有 1 个属性的对象的 JSON 对象

javascript - 数据集返回 JavaScript AngularJS

java - WebDriver 无法使用 Java 通过 xpath 找到元素