我想测试一个对象数组。基本上,当我为显示的对象数组运行测试范围时,最后一个对象是link
有条件的部分,而这是未被发现的部分。
export const relatedServicesList: IRelatedServiceItem[] = [
{
label: 'inquiry.title',
link: '/adc/declaration/landing',
},
{
label: 'extendDeposit.title',
link: '/adc/extend-deposit/landing',
},
{
label: 'generalAdminCustomsServices.landing.title',
link:
window.location.host === 'stage'
? '/demo'
: '/test',
},
];
我尝试过的事情
import { relatedServicesList } from './routes';
describe('Routes', () => {
it('when host = stage', () => {
global.window = Object.create(window);
Object.defineProperty(window, 'location', {
value: {
host: 'stage',
},
});
window.location.host = 'stage';
expect(relatedServicesList[relatedServicesList.length - 1]).toEqual(
expect.objectContaining({
label: 'generalAdminCustomsServices.landing.title',
link:
'stage',
}),
);
});
it('when host != stage', () => {
global.window = Object.create(window);
Object.defineProperty(window, 'location', {
value: {
host: 'demo',
},
});
window.location.host = 'demo';
expect(relatedServicesList[relatedServicesList.length - 1]).toEqual(
expect.objectContaining({
label: 'generalAdminCustomsServices.landing.title',
link: '/test',
}),
);
});
});
条件部分未被发现。请注意,仅导出类型为
IRelatedServiceItem
的数组,没有函数等。
答案 0 :(得分:1)
有两个警告:
如果使用es6 import
语法导入relatedServicesList
数组,则评估(window.location.host === 'stage' ? '/demo' : '/test'
)在更改window.location
的值之前。我们可以使用require
处理此问题。
您需要使用jest.resetModules()来重置模块注册表-所有必需模块的缓存。这样每个测试用例都会有一个新的评估。
这是工作示例:
index.ts
:
interface IRelatedServiceItem {
label: string;
link: string;
}
console.log('window.location.host: ', window.location.host);
export const relatedServicesList: IRelatedServiceItem[] = [
{
label: 'inquiry.title',
link: '/adc/declaration/landing',
},
{
label: 'extendDeposit.title',
link: '/adc/extend-deposit/landing',
},
{
label: 'generalAdminCustomsServices.landing.title',
link: window.location.host === 'stage' ? '/demo' : '/test',
},
];
index.spec.ts
:
describe('59698218', () => {
beforeEach(() => {
jest.resetModules();
});
it('when host = stage', () => {
Object.defineProperty(window, 'location', {
value: { host: 'stage' },
writable: true,
});
const { relatedServicesList } = require('./index');
expect(relatedServicesList[relatedServicesList.length - 1]).toEqual({
label: 'generalAdminCustomsServices.landing.title',
link: '/demo',
});
});
it('when host != stage', () => {
Object.defineProperty(window, 'location', {
value: { host: 'demo' },
writable: true,
});
const { relatedServicesList } = require('./index');
expect(relatedServicesList[relatedServicesList.length - 1]).toEqual({
label: 'generalAdminCustomsServices.landing.title',
link: '/test',
});
});
});
单元测试结果覆盖率100%:
PASS src/stackoverflow/59698218/index.spec.ts (8.497s)
59698218
✓ when host = stage (14ms)
✓ when host != stage (2ms)
console.log src/stackoverflow/59698218/index.ts:107
window.location.host: stage
console.log src/stackoverflow/59698218/index.ts:107
window.location.host: demo
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 9.649s
源代码:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59698218