我在 NPM 库中有一个
index.ts
顶级文件,其中只有导出语句。
即:
export * from "./foo"
export * from "./bar"
SonarCloud 将这些行显示为未覆盖,因为预计这些行不应进行测试。我知道我们可以忍受缺少的报道,但这在某种程度上很烦人。
我知道我也可以忽略该文件,但随后我需要对具有类似目的的每个文件执行相同的操作,将组件分组并导出到库中。
我可以使用任何最佳实践或配置来克服这个问题吗?
经过一番研究,我发现了一个与我正在寻找的选项相符的选项。
这受到 MaterialUI 存储库的启发,该存储库使用
mocha
和 chai
:
import { expect } from 'chai';
import * as MyLib from './index';
describe('MyLib', () => {
it('should have exports', () => {
expect(typeof MyLib).to.equal('object');
});
it('should not have undefined exports', () => {
Object.keys(MyLib).forEach((exportKey) =>
expect(Boolean(MyLib[exportKey])).to.equal(true),
);
});
});
来源:https://github.com/mui-org/material-ui/blob/next/packages/material-ui/src/index.test.js
当我们在项目中使用 JEST 时,我们必须对其进行转换:
import * as MyLib from './index';
describe('MyLib', () => {
it('should have exports', () => {
expect(MyLib).toEqual(expect.any(Object));
});
it('should not have undefined exports', () => {
expect(Object.keys(MyLib)).not.toEqual(
expect.arrayContaining([ undefined ])
);
});
});