【问题标题】:Your test suite must contain at least one test您的测试套件必须包含至少一项测试
【发布时间】:2018-11-23 09:45:14
【问题描述】:

我今天在我的项目中更新了一些依赖项,但进展非常顺利。现在,当我要推动它时,我开始了我的测试。和繁荣。他们都扔了:

Your test suite must contain at least one test.

我的包裹:

"jest": "23.1.0",
"jest-enzyme": "^6.0.1",
"jest-webpack-alias": "^3.3.3",
"jsdom": "^11.2.0",
"jsdom-global": "^3.0.2",
"enzyme": "3.3.0",
"enzyme-adapter-react-16": "^1.1.0",
"enzyme-to-json": "3.3.4",

这就是我的示例测试文件的样子:

/shared/components/App/MyRoute/__tests__/MyRoute.test.js

/* eslint-disable import/no-extraneous-dependencies */
import React from 'react';
import { shallow } from 'enzyme';
import { ContactsRoute } from '../Route';

describe('<ContactsRoute />', () => {
  test('renders', () => {
    const wrapper = shallow(<ContactsRoute t={key => key} />);
    expect(wrapper).toMatchSnapshot();
  });
});

我不知道他们为什么突然停止跑步?

编辑 - 添加我的笑话配置

  "jest": {
"collectCoverageFrom": [
  "shared/**/*.{js,jsx}"
],
"globals": {
  "JWT_SECRET": "local",
  "IS_TEST": "true"
},
"snapshotSerializers": [
  "<rootDir>/node_modules/enzyme-to-json/serializer"
],
"testPathIgnorePatterns": [
  "<rootDir>/(build|internal|node_modules|flow-typed|public|shared/services)/"
],
"moduleNameMapper": {
  "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/_test_config_/mocks/fileMock.js"
},
"testURL": "http://localhost:3005",
"transform": {
  ".": "<rootDir>/_test_config_/preprocessors/webpackAlias.js",
  "^.+\\.css$": "<rootDir>/_test_config_/preprocessors/cssTransform.js",
  "^(?!.*\\.(js|jsx|css|json)$)": "<rootDir>/_test_config_/preprocessors/fileTransform.js"
},
"setupFiles": [
  "<rootDir>/_test_config_/preprocessors/polyfills.js"
],
"setupTestFrameworkScriptFile": "./node_modules/jest-enzyme/lib/index.js"
},

【问题讨论】:

  • test() 不是一个东西。 it() 是。
  • @DanO 这在最近的一些版本中有所改变吗?
  • 使用相同版本的Jesttest() 对我来说很好。
  • @DanO Jest 有 test() 相当于 it()。
  • 呵呵,我从未听说过它,也没有在(快速、非详尽的)文档搜索中看到它。我想每天都能学到新东西。

标签: reactjs jestjs enzyme


【解决方案1】:

引发此错误的另一个问题是当您将一些非测试文件放入您的 __test__ 目录时。

__test__ 目录中的所有文件必须包含一些要运行的测试。

【讨论】:

    【解决方案2】:

    确保您的 describe 函数不是异步的

    // Wrong
    describe('setPackages', async () => {
     // ...
    });
    
    // Correct
    describe('setPackages', () => {
     // ...
    });
    

    【讨论】:

      【解决方案3】:

      另一种选择是使用--passWithNoTests 参数调用Jest,如here 所述。 当您不知道脚本将运行多少测试时,该标志很有用,例如使用 git commit 钩子。

      【讨论】:

        【解决方案4】:

        如果所有其他响应均失败,如果您有任何名为 test.js 的文件,Jest 将期望该文件包含测试。

        我正在使用一个名为 test.js 的文件来手动测试一些东西,而 Jest 抛出了这个错误。我将文件重命名为其他名称(例如:123.js),但我不再收到此错误消息。

        【讨论】:

        • 该死的谢谢这是我的问题
        • 我很高兴它有帮助。不知道为什么 Jest 会这样做,而且日志也根本没有帮助。
        • 除了重命名文件之外的另一个选项是将testMatch配置得更具体。我将其设置为**/?*.test.ts
        • 我的 js 方法被称为 test 并毁了它
        • 没错,可能是你有一个没有测试的 .test 文件。
        【解决方案5】:

        也许你忘了它() 块:

        也许你有一个错误,在某处代码是这样的:

        describe('my test', () => {
            expect(...)...;
        });
        

        替换为:

        describe('my test', () => {
            it('some text', () => {
                expect(...)...;
            })
        });
        

        【讨论】:

          【解决方案6】:

          确保描述/it/expect 变量没有被导入

          在我的例子中,IDE( VSCode ) 会自动从另一个库中导入变量 describe

          【讨论】:

          • 你是怎么解决这个问题的?
          • @xtra 你的意思是自动导入?我只是手动删除了自动添加的导入语句并禁用了 VSCode 自动导入功能。 here's an ref
          • 如果发生这种情况,下一步是stackoverflow.com/questions/57874114/… 让 VSCode 了解开玩笑的全局变量。
          • @J.G.Sebring 很好地添加到解决方案中
          【解决方案7】:

          确保您至少有一个describe / it / expect

          在我的例子中,我只写了一个describe / expect 测试并收到了这个错误消息

          【讨论】:

          • jest/test/expect 也给出了这个错误。用“it”替换“test”修复了它。谢谢
          【解决方案8】:

          所以,我找到了答案。

          here 中的Note 表示,如果我们将babel-jest 与我们自己的预处理器一起使用,则必须在transform 选项中明确定义babel-jest

          transform 配置中添加"^.+\\.jsx?$": "babel-jest" 作为第一个选项很重要。

          【讨论】:

            猜你喜欢
            • 2018-08-01
            • 2017-01-01
            • 2022-12-11
            • 2021-06-26
            • 2019-06-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-08-01
            相关资源
            最近更新 更多