我用Mocha来测试我的JavaScript东西。我的测试文件包含5个测试。是否可以运行一个特定的测试(或一组测试),而不是文件中的所有测试?


当前回答

只在“describe”,“it”或“context”之前使用。我使用“$npm run test:unit”运行,它只执行带有.only的单元。

describe.only('get success', function() {
 // ...
});

it.only('should return 1', function() {
  // ...
});

其他回答

只在“describe”,“it”或“context”之前使用。我使用“$npm run test:unit”运行,它只执行带有.only的单元。

describe.only('get success', function() {
 // ...
});

it.only('should return 1', function() {
  // ...
});

对于那些希望运行单个文件但又不能使其工作的人来说,对我有用的是,我需要将我的测试用例包装在如下所示的描述套件中,然后使用描述标题,例如。'My Test Description'作为模式。

describe('My Test Description', () => {
  it('test case 1', () => {
    // My test code
  })
  it('test case 2', () => {
  // My test code
  })
})

然后运行

纱线测试-g“我的测试描述”

or

运行我的测试说明

查看文档,我们看到简单地使用:

mocha test/myfile

将工作。你可以省略结尾的'.js'。

如果你使用npm测试(使用包。Json脚本)使用一个额外的——通过mocha传递参数

例如NPM test -- --grep“my second test”

编辑:看起来——grep可能有点麻烦(可能取决于其他参数)。您可以:

修改package.json:

"test:mocha": "mocha --grep \"<DealsList />\" .",

或者使用保释金,这似乎不那么麻烦

npm test -- --bail

不知道为什么grep方法在使用npm测试时不适合我。不过这是可行的。出于某种原因,我还需要指定测试文件夹。

npm test -- test/sometest.js