【问题标题】:Run single test of a specific test suite in Jest在 Jest 中运行特定测试套件的单个测试
【发布时间】:2019-11-24 11:28:53
【问题描述】:

我有一个名为“test-file-1”的文件,其中我有一些具有唯一名称的描述(测试套件),其中我有测试,这些测试可能在测试套件中具有相似的名称。

要运行单个测试套件或测试,请输入以下命令。

npm test -- -t "Test Suite or Test"

我试图弄清楚如何运行特定测试套件的单个测试。

【问题讨论】:

  • 那么您是否阅读了 CLI 文档? jestjs.io/docs/en/cli.html
  • 是的,但我没有找到一种方法来运行特定测试套件的单个测试。

标签: javascript node.js npm automated-tests jestjs


【解决方案1】:

给出你要测试的文件的路径:

npm test path/of/your/testfile.js -t "test name"

将只运行此特定文件的名为“测试名称”的测试(或组)。

【讨论】:

  • 这就是问题所在,我有一个包含多个名称相同或相似的测试的文件,唯一不同的是测试套件。
【解决方案2】:

通过"testMatch":["**/path/testSuite/*.js"] 指向 jest 配置文件中的测试套件

现在通过jest testName提供唯一的测试名称来执行

这里你必须每次都更新 testMatch,这是你的 testSuite

【讨论】:

  • 我正在寻找更简单的方法,每次手动更改都不实用。
  • 您的测试套件是动态的,而测试用例是静态的,因此您必须一直更改测试套件,那么为什么不使用 jest -t /path/to/testSuite/testcase.js 运行。每次你只需在这里通过 testsuite 并从配置文件中删除 testMatch
【解决方案3】:

在尝试了很多不同的东西后,我发现它比我想象的要简单。

您只需将测试放在同一字符串中的测试套件之后:

npm test -- -t "<Test Suite> <Test>"

【讨论】:

    【解决方案4】:

    对我来说,它只适用于:

    npm run test -- tests/01_login.js --testcase "Should login into Dashboard"
    
    npm run <script> -- <test suite path> --testcase "<test case>"
    

    我在 package.json 中的脚本:

    "test": "env-cmd -f ./.env nightwatch --retries 2 --env selenium.chrome",
    

    在守夜人版本 1.3.4

    【讨论】:

      【解决方案5】:
      import LoggerService from '../LoggerService ';
      
      describe('Method called****', () => {
        it('00000000', () => {
          const logEvent = jest.spyOn(LoggerService , 'logEvent');
          expect(logEvent).toBeDefined();
        });
      });
      

      运行下面的命令 npm test -- 测试/LoggerService.test.ts -t '00000000'

      【讨论】:

        【解决方案6】:

        如果您使用的是 jest 配置文件,您可以使用 testNamePattern 键来配置它。所以你的配置文件可能看起来像

        jest-e2e.json

        {
          "moduleFileExtensions": ["js", "json", "ts"],
          "rootDir": ".",
          "testEnvironment": "node",
          "testRegex": ".spec.ts$",
          "testNamePattern": "login",
          "transform": {
            "^.+\\.(t|j)s$": "ts-jest"
          },
          "bail": true,
          "verbose": true,
          "testTimeout": 30000
        }
        
        
        

        这只会运行在其中一种测试模式中具有“登录”的测试

        参考: https://jestjs.io/docs/cli#--testnamepatternregex

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-10-21
          • 1970-01-01
          • 2014-03-25
          • 2018-10-14
          • 2022-01-05
          • 2016-11-24
          • 2019-08-22
          • 1970-01-01
          相关资源
          最近更新 更多