【问题标题】:Jest: Error: Your test suite must contain at least one test笑话:错误:您的测试套件必须包含至少一个测试
【发布时间】:2017-01-01 01:46:05
【问题描述】:

我正在实施 jest 来测试我的 React 应用程序,并已对我拥有的实用程序函数设置了一个简单的测试,但我收到了错误:

错误:您的测试套件必须至少包含一个测试。

检查了我的实现,我认为一切都是正确的 - 有人可以看看我吗?

测试用的文件结构及功能如下

- __tests__
  -- sumObjectValues-test.js
- utils
  -- sumObjectValues.js

sumObjectValues.js如下:

const sumObjectValues = (obj, identifier) => {
    return obj
        .map((el) => { return el[identifier]; })
        .reduce((prev, next) => { return prev += next; }, 0);
}

export default sumObjectValues;

还有sumObjectValues-test.js:

   const obj = [
    {
        "id": 0,
        "item": "Tesla Model S",
        "amount": 85000
    },
    {
        "id": 1,
        "item": "iPhone 6S",
        "amount": 600
    },
    {
        "id": 2,
        "item": "MacBook Pro",
        "amount": 1700
    }
];

const identifier = "amount";


jest.unmock('../client/utils/sumObjectValues'); // unmock to use the actual implementation of `sumObjectValues`

describe('sumObjectValues', () => {
    if('takes an array of objects, each with amount values, & sums the values', () => {
        const sumObjectValues = require('../client/utils/sumObjectValues');
        expect(sumObjectValues(obj, identifier)).toBe(87300);
    });
});

然后我的 package.json 脚本中有"test": "jest",但出现以下错误:

FAIL __tests__/sumObjectValues-test.js (0s) ● 运行时错误 - 错误:您的测试套件必须包含至少一个测试。 在 onTestResult (node_modules/jest-cli/build/TestRunner.js:143:18) 1 测试套件 失败,0 个测试通过(1 个测试套件中总共 0 个,运行时间 13.17 秒)npm 呃!测试失败。有关更多详细信息,请参见上文。

谢谢大家:)

注意it 中有错字,但在修复后我收到了一个新错误:

失败 __tests__/sumObjectValues-test.js (321.763s) ● sumObjectValues › 它需要一个对象数组,每个对象都有数量值,并对 values - TypeError: sumObjectValues 不是函数 目的。 (__tests__/sumObjectValues-test.js:27:10) 1 次测试 失败,0 个测试通过(1 个测试套件中总共 1 个,运行时间 344.008 秒) npm 错误!测试失败。有关更多详细信息,请参见上文。

【问题讨论】:

  • 我很确定Node.js doesn't support ES2015 modules(还)。尝试将export default sumObjectValues 更改为module.exports = sumObjectValues
  • 啊,当然,我可以添加 babel jest 模块来尝试排序,试试吧!谢谢你:)
  • 啊伙计,这很痛苦.. 我想我需要睡觉了。它现在已经运行了 我已经安装了 babel-jest,但是我收到了一个新错误:FAIL __tests__/sumObjectValues-test.js (321.763s) ● sumObjectValues › 它需要一个对象数组,每个对象都有数量值,并对这些值求和- TypeError: sumObjectValues is not a function at Object. (__tests__/sumObjectValues-test.js:27:10) 1 个测试失败,0 个测试通过(1 个测试套件中总共 1 个,运行时间 344.008 秒)npm ERR!测试失败。有关更多详细信息,请参见上文。
  • 我只是吐口水,因为我还没有在 Node 中使用过 ES2015 模块,但是你有没有尝试过使用 import syntax 而不是 require
  • 是的!你美女!谢谢迈克,就是这样,感谢您的帮助:)

标签: javascript unit-testing testing jestjs


【解决方案1】:
if('takes an array of objects, each with amount values, & sums the values', () => {

应该是

it('takes an array of objects, each with amount values, & sums the values', () => {

【讨论】:

    【解决方案2】:

    如果您想稍后再编写测试, 你可以用这个忽略它们

    test.skip('skip', () => {});
    

    【讨论】:

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