简体   繁体   中英

Jest test does not fail on error while rendering

I'm trying to fix some unit test that passes regardless of what happens in the rendering.

import React from 'react';
import Component from './Component';

describe('Basic component tests', () => {
  it('renders nothing on initial load', () => {
    const component = shallow(<Component/>);

    expect(component.exists()).toBe(false);
  });
});

The problem is when I run this test I get an error in console.error but the test pass regardless. How do I check if the render happened without any error and fail the test if an error did occur?

Output example:

Debugger listening on ws://127.0.0.1:44345/7a8a35fb-7e19-4d92-8dc0-ea274fa1646c
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
  console.error src/components/universal/Component.js:91
    TypeError: Cannot read property 'includes' of undefined
        at includes (/home/frozendragon/code/application/src/components/universal/Component.js:412:45)
        at Array.filter (<anonymous>)
        at Component.filter [as getPackagesByIds] (/home/frozendragon/code/application/src/components/universal/Component.js:412:29)
        at Component.getPackagesByIds (/home/frozendragon/code/application/src/components/universal/Component.js:86:36)
        at tryCatch (/home/frozendragon/code/application/node_modules/@babel/polyfill/node_modules/regenerator-runtime/runtime.js:45:40)
        at Generator.invoke [as _invoke] (/home/frozendragon/code/application/node_modules/@babel/polyfill/node_modules/regenerator-runtime/runtime.js:271:22)
        at Generator.prototype.(anonymous function) [as next] (/home/frozendragon/code/application/node_modules/@babel/polyfill/node_modules/regenerator-runtime/runtime.js:97:21)
        at asyncGeneratorStep (/home/frozendragon/code/application/src/components/universal/Component.js:4176:103)
        at _next (/home/frozendragon/code/application/src/components/universal/Component.js:4178:194)

------------------|----------|----------|----------|----------|-------------------|
File              |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
------------------|----------|----------|----------|----------|-------------------|
All files         |    33.57 |       15 |    35.48 |    34.38 |                   |
 Component.js     |    33.57 |       15 |    35.48 |    34.38 |... 65,566,569,574 |
------------------|----------|----------|----------|----------|-------------------|

最简单的测试是验证您可以查询DOM中的特定元素,例如

expect(component.exists('.some-class')).to.equal(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM