这是我的结构:
您看到我对文件shipments.js
进行了100%的测试,以下是这些测试:
import shipmentsPagination, {
setFilters,
removeFilter,
toggleFiltersModal,
setCSVDataAction,
} from '../../actions/shipments';
describe('shipments redux actions', () => {
it('should test setCSVDataAction', () => {
const result = setCSVDataAction({
itemsCount: 0,
shipments: [],
});
expect(result.type).toEqual('SET_CSV_DATA');
expect(result.payload).toEqual({
shipmentsCSV: {
itemsCount: 0,
shipments: [],
},
});
});
it('should test toggleFiltersModal', () => {
const result = toggleFiltersModal(true);
expect(result.type).toEqual('TOGGLE_SHIPMENTS_FILTERS_MODAL');
});
it('should test removeFilter', () => {
const filters = {
shipmentId: '123',
status: 'received',
carrier: 'FedEx',
shippedFrom: 'Dallas',
shippedTo: 'NY',
shippedDate: '10/13/2018',
shipmentType: 'Colocation Hardware',
};
const result = removeFilter(filters);
expect(result.type).toEqual('REMOVE_SHIPMENTS_FILTER');
expect(result.payload).toEqual({ [filters]: '' });
});
it('should test shipmentsPagination', () => {
const result = shipmentsPagination({
page: 2,
pageSize: 25,
});
expect(result.type).toEqual('UPDATE_SHIPMENTS_PAGINATION');
expect(result.payload).toEqual({ page: 2, pageSize: 25 });
});
it('should test setFilters', () => {
const result = setFilters({
shipmentId: '123',
status: 'Received',
carrier: 'FedEx',
shippedFrom: 'Houston - HOU02',
shippedTo: 'Herp',
shippedDate: '1/24/2019',
shipmentType: 'Colocation Hardware',
});
expect(result.type).toEqual('SET_SHIPMENTS_FILTERS');
expect(result.payload).toEqual({
shipmentId: '123',
status: 'Received',
carrier: 'FedEx',
shippedFrom: 'Houston - HOU02',
shippedTo: 'Herp',
shippedDate: '1/24/2019',
shipmentType: 'Colocation Hardware',
});
});
});
您可能会看到,它说index.js
在第1行没有覆盖范围。
index.js
是这个:
import shipments from './shipments';
export default {
shipments,
};
那么我如何才能测试该导入/导出以获得100%的测试覆盖率?
答案 0 :(得分:1)
根据我对您代码的了解,只需在测试中从shipmentsPagination
导入index.js
:
import defaultExport from '../../actions/'; // import the default export from index.js
const { shipments: shipmentsPagination } = defaultExport; // and use it to get shipmentsPagination
import {
setFilters,
removeFilter,
toggleFiltersModal,
setCSVDataAction,
} from '../../actions/shipments'; // everything else can come from shipments.js
从shipmentsPagination
导入index.js
将确保export
中的index.js
行得到测试。
答案 1 :(得分:0)
@mystackrunnethover是一种正确的方法,并不一定需要覆盖该行,但是,以防万一:
我将这些测试用作“健全性测试”,以确保Jest甚至在运行。
一个常见的陷阱是假设如果导入无效,imported_thing.toBeTruth()
将失败。不会-imported_thing
将是一个空对象,这是事实。
这是我们使用的:
import Account from '/account';
//describe block
expect(Account).toBeTruthy();
expect(Object.keys(Account).length > 0).toBe(true);
这当然只有在Account
实际上具有属性(并且应该具有)的情况下才有效。另一种方法是专门检查所需的属性。