javascript - Jest : Error: Your test suite must contain at least one test

标签 javascript unit-testing testing jestjs

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

Error: Your test suite must contain at least one test.

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

测试的文件结构和函数如下

- __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) ● Runtime Error
  - Error: Your test suite must contain at least one test.
       at onTestResult (node_modules/jest-cli/build/TestRunner.js:143:18) 1 test suite
failed, 0 tests passed (0 total in 1 test suite, run time 13.17s) npm
ERR! Test failed.  See above for more details.

谢谢大家:)

N.B.:在 it 中是一个拼写错误,但修复后我收到了一个新错误:

FAIL __tests__/sumObjectValues-test.js (321.763s) ● sumObjectValues ›
it takes an array of objects, each with amount values, & sums the
values - TypeError: sumObjectValues is not a function at
Object. (__tests__/sumObjectValues-test.js:27:10) 1 test
failed, 0 tests passed (1 total in 1 test suite, run time 344.008s)
npm ERR! Test failed. See above for more details.

最佳答案

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', () => {

关于javascript - Jest : Error: Your test suite must contain at least one test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39131521/

相关文章:

javascript - Angular2 表单页面重新加载?

asp.net - Jquery, "$.each(",函数在IE中返回错误。 'Length' 为空或不是对象

javascript - 无法理解在 JavaScript 中删除 vars 的行为

c# - 通过 GetAsync() 传递参数

unit-testing - 无法使用 i686-linux-android 目标运行单元测试

javascript - 将 JavaScript 代码重构为 jQuery 代码

javascript - 如何在不重复父类(super class)测试的情况下在 Mocha 中测试 Backbone 子类?

ruby-on-rails - 应该 helper 不工作

java - 在多个 jvm 实例中运行 java 代码

.net - 我应该如何模拟这个简单的服务层方法?