在 Mocha 输出中显示测试文件名

Display test file name in Mocha output

有没有办法让 mocha 显示名称,或者根据 测试文件 对输出进行分组?

给定两个测试文件,./test/testFoo.js./test/testBar.js 我希望在 运行 mocha test:

时看到这样的结果

* testFoo:

  Doing X
    ✓ should return A
    ✓ even with negative input

  Doing Y
    ✓ should return B
    ✓ should return C when called with Egyptian hieroglyphs

* testBar:

  Doing Z
    ✓ should return D

(这可能是一个 XY 问题。如果有其他方法可以将测试分为两个级别,我也很感兴趣。只是文件已经存在,作为一种自然的第一级组。 )

您应该善于使用 Mocha 的 describe 方法对您的测试进行逻辑分组。对于您想要的输出,您的测试结构如下所示:

/test/testFoo.js

describe('testFoo', () => {
    describe('Doing X', () => {
        it('should return A', () => {
        });

        it('even with negative input', () => {
        });
    });

    describe('Doing Y', () => {
        it('should return B', () => {
        });

        it('should return C when called with Egyptian hieroglyphs', () => {
        });
    });
});

/test/testBar.js

describe('testBar', () => {
    describe('Doing Z', () => {
        it('should return D', () => {
        });
    });
});