我有这段代码,npm run 测试给我带来了覆盖率问题。 我看到了覆盖 html,并且看到我的解构 url='' 显示为黄色。
我看到了问题,我可以看到这个:
function Team(props) {
const { team } = props;
const {
url = '', //this element appear in yellow, so is not being testing.
} = team;
const context = useAppContext();
const { sportEvent, protocol } = getProperties(context);
const { arcSite, outputType } = context;
const basePath = getBasePath(context);
const infoTeamPicture = translateMessage({ arcSite, id: 'infoCard.infoTeamPicture', defaultMessage: 'Escudo/Bandera equipo' });
const initPath = getInitPath(team, protocol, basePath);
return (
outputType === 'amp' ?
<TeamAmp infoTeamPicture={infoTeamPicture} team={team} initPath={initPath}/>
:
<figure className="ext ext-ic ext-ic--team">
<div className="ext-ic__wr" {...getDTMRegion(ACTIVITY_MAP_NAME)}>
<a href={`${initPath}${url}`} className="ext-ic__img">
<img
src={`${sportEvent.host}${team.image60}`}
alt={infoTeamPicture}
width="80"
height="80"
/>
</a>
<div className="ext-ic__inf">
<a href={`${initPath}${url}`} title={team.shortName} className="ext-ic__tl"><span>{team.shortName}</span></a>
<InfoList team={team} />
</div>
<InfoButtons protocol={protocol} info={team} initPath={initPath}/>
</div>
{team.competition && <Competition competition={team.competition} />}
</figure>
);
}
我尝试创建一个新测试,例如:
it('no render url', () => {
const specificTeams = {
shortName: 'Real Madrid',
country: 'España',
url: '',
urlTag: 'as.com/m/tag/real_madrid/a?omnil=src-app',
image60: '/img/comunes/fotos/fichas/equipos/small/2x/1.png',
image100: '/img/comunes/fotos/fichas/equipos/large/1.png',
elementType: 'team',
competition: {
name: 'LaLiga Santander 2020/2021',
normalized: 'primera',
standing: [],
nextGames: {},
},
};
const instance = shallow(<Team team={specificTeams}/>);
expect(specificTeams.url).toEqual('')
});
但我无法实现覆盖率达到 100%。 有人可以帮助我了解如何测试这类事情吗? 谢谢
最佳答案
您可以使用 JSON.stringify 来实现此目的,因为它将返回 '{}' 字符串。单元测试将是:
expect(JSON.stringify(someObject)).toBe('{}');
关于javascript - 对象中的测试元素是空的,开玩笑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71085591/