dev*_*054 6 javascript typescript jestjs angular ts-jest
所以我用来Jest#test.each运行一些单元测试。
这是实际的代码:
const invalidTestCases = [
[null, TypeError],
[undefined, TypeError],
[false, TypeError],
[true, TypeError],
];
describe('normalizeNames', () => {
describe('invalid', () => {
test.each(invalidTestCases)('some description for (%p, %p)', (actual, expected) => {
expect(() => normalizeNames(actual as any)).toThrowError(expected);
});
});
describe('valid', () => {
// ...
});
});
Run Code Online (Sandbox Code Playgroud)
问题是由于打字稿错误我无法运行它:
Argument of type '(actual: boolean | TypeErrorConstructor | null | undefined, expected: boolean | TypeErrorConstructor | null | undefined) => void' is not assignable to parameter of type '(...args: (TypeErrorConstructor | null)[] | (TypeErrorConstructor | undefined)[] | (boolean | TypeErrorConstructor)[]) => any'.
Types of parameters 'actual' and 'args' are incompatible.
Type '(TypeErrorConstructor | null)[] | (TypeErrorConstructor | undefined)[] | (boolean | TypeErrorConstructor)[]' is not assignable to type '[boolean | TypeErrorConstructor | null | undefined, boolean | TypeErrorConstructor | null | undefined]'.
Type '(TypeErrorConstructor | null)[]' is missing the following properties from type '[boolean | TypeErrorConstructor | null | undefined, boolean | TypeErrorConstructor | null | undefined]': 0, 1
test.each(invalidTestCases)('some description for (%p, %p)', (actual, expected) => {
~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
我还尝试使用arrayofobjects而不是 2d array,如下所示:
const invalidTestCases = [
{ actual: null, expected: TypeError },
{ actual: undefined, expected: TypeError },
{ actual: false, expected: TypeError },
{ actual: true, expected: TypeError },
];
describe('normalizeNames', () => {
describe('invalid', () => {
test.each(invalidTestCases)('some description for (%p, %p)', ({ actual, expected }) => {
expect(() => normalizeNames(actual as any)).toThrowError(expected);
});
});
describe('valid', () => {
// ...
});
});
Run Code Online (Sandbox Code Playgroud)
...但是这样做,我无法获得正确的object值测试描述。
我目前无法对其进行测试,但添加类型注释通常可以修复该错误。
\n\n所以也许可以尝试:
\n\ntype testCaseErrorTypes = null|undefined|boolean\nconst invalidTestCases: [testCaseErrorTypes, typeof TypeError][] = [\n [null, TypeError],\n [undefined, TypeError],\n [false, TypeError],\n [true, TypeError],\n];\ntest.each(invalidTestCases)(\'some description for (%p, %p)\', (actual, expected) => { \xe2\x80\xa6 }\nRun Code Online (Sandbox Code Playgroud)\n\ninvalidTestCases这应该从 a转换(testCaseErrorTypes|TypeError)[][]为正确的 type [testCaseErrorTypes, TypeError][]。
| 归档时间: |
|
| 查看次数: |
4739 次 |
| 最近记录: |