admin管理员组文章数量:1323150
So I'm using Jest#test.each
to run some unit tests.
Here's the actual code:
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', () => {
// ...
});
});
The problem is that I'm unable to run this due to typescript errors:
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 inpatible.
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) => {
~~~~~~~~~~~~~~~~~~~~~~~
I also tried to use an array
of objects
instead of 2d array
, like this:
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', () => {
// ...
});
});
...but doing this way, I can't get correct test description for object
values.
So I'm using Jest#test.each
to run some unit tests.
Here's the actual code:
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', () => {
// ...
});
});
The problem is that I'm unable to run this due to typescript errors:
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 inpatible.
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) => {
~~~~~~~~~~~~~~~~~~~~~~~
I also tried to use an array
of objects
instead of 2d array
, like this:
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', () => {
// ...
});
});
...but doing this way, I can't get correct test description for object
values.
2 Answers
Reset to default 6I'm currently not somewhere I can test it, but adding type annotations usually fixes that error.
So maybe try:
type testCaseErrorTypes = null|undefined|boolean
const invalidTestCases: [testCaseErrorTypes, typeof TypeError][] = [
[null, TypeError],
[undefined, TypeError],
[false, TypeError],
[true, TypeError],
];
test.each(invalidTestCases)('some description for (%p, %p)', (actual, expected) => { … }
This should transform invalidTestCases
from a (testCaseErrorTypes|TypeError)[][]
into the correct type [testCaseErrorTypes, TypeError][]
.
Since all the expected
values are equal, you can just use 1d array and pass TypeError
directly to the toThrowError
. It would be something like this:
const invalidTestCases = [
null,
undefined,
false,
true,
];
describe('normalizeNames', () => {
describe('invalid', () => {
test.each(invalidTestCases)('some description for (%p)', actual => {
expect(() => normalizeNames(actual as any)).toThrowError(TypeError);
});
});
describe('valid', () => {
// ...
});
});
本文标签: javascriptIncorrect types for Jest testeachStack Overflow
版权声明:本文标题:javascript - Incorrect types for Jest test.each - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742145047a2422752.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论