分组运行Jest测试套件 [英] Run Jest test suites in groups

查看:86
本文介绍了分组运行Jest测试套件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Jest和supertest为新的API编写广泛的测试.在运行测试之前,我将建立一个测试数据库并向其填充用户:

I am writing extensive tests for a new API via Jest and supertest. Prior to running the tests, I am setting up a test database and populating it with users:

jest --forceExit --config src/utils/testing/jest.config.js

文件 jest.config.js

module.exports = {
  rootDir: process.cwd(),

  // Sets up testing database with users
  globalSetup: './src/utils/testing/jest.setup.js',

  // Ensures connection to database for all test suites
  setupTestFrameworkScriptFile: './src/utils/testing/jest.db.js',

}

因此,我从一些用户的数据库开始进行测试.问题是这样的:

So I am starting with a database of some users to test on. The problem is this:

我的某些测试依赖于其他测试的成功.在此应用程序中,用户可以上传图像并将其分组.因此,我的分组终结点套件取决于我的图像上传套件的成功,等等.

Some of my tests rely upon the success of other tests. In this application, users can upload images, and group them into packs. So my grouping endpoint suite depends upon the success of my image upload suite, and so on.

我很清楚许多人可能会说这是不好的做法,并且测试不应依赖其他测试.话虽这么说,我真的更希望通过supertest保留所有测试,而不必进行依赖注入等.我不想精心设置测试条件(例如,人工创建一堆用户图像)在运行测试之前),原因是:(1)这仅仅是逻辑上的重复,(2)它增加了发生故障的可能性.

I am well aware many people might say this is bad practice, and that tests should not rely upon other tests. That being said, I would really rather keep all my tests via supertest, and not get into dependency injection, etc. I don't want to have to meticulously set up testing conditions (for example, creating a bunch of user images artificially before running the tests), because: (1) this is just duplication of logic, and (2) it increases the possibility of something breaking.

有什么办法组合笑话套房吗?例如,要按顺序运行套件:

Is there any way to group jest suites? For example, to run suites in order:

jest run creationSuite
jest run modificationSuite

这样,我所有的"creationSuite"测试可以同时运行,并且 all 的成功将触发"modificationSuite"操作.以快速失败的方式运行等.

This way, all my "creationSuite" tests could be run simultaneously, and success of all would then trigger the "modificationSuite" to run, etc., in a fail-fast manner.

或者,在测试套件内部指定对其他测试套件的依赖关系也很好:

Alternatively, specifying inside a test suite dependencies on other test suites would be great:

describe('Grouping endpoint', () => {
    // Somehow define dependencies
    this.dependsOn(uploadSuite)

推荐答案

您可以使用jest-runner-groups分组定义和运行测试.安装并添加到Jest配置后,您可以使用docblock表示法标记测试,如下所示:

You can use jest-runner-groups to define and run tests in groups. Once it's installed and added to the Jest configuration, you can tag your tests using docblock notation, like here:

/**
 * Foo tests
 *
 * @group group1/subgroup1
 * @group unit/foo
 */

describe( 'Foo class', () => {
    ...
} );

/**
 * Bar tests
 *
 * @group group1/subgroup2
 * @group unit/bar
 */

describe( 'Bar class', () => {
    ...
} );

更新您的笑话配置以指定新跑步者:

Update your jest configuration to specify a new runner:

// jest.config.js
module.exports = {
    ...
    runner: "groups"
};

然后要运行特定的组,您需要使用--group=参数:

Then to run a specific group, you need to use --group= argument:

// Using the Jest executable
jest --group=mygroup

// Or npm
npm test -- --group=mygroup

您还可以使用多个--group参数来运行多个组:

You can also use multiple --group arguments to run multiple groups:

// Will execute tests in the unit/bar and unit/foo groups
npm test -- --group=unit/bar --group=unit/foo

// Will execute tests in the unit group (including unit/bar and unit/foo groups)
npm test -- --group=unit

这篇关于分组运行Jest测试套件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆