Javascript Jest测试的类型不正确。每个
所以我用它来运行一些单元测试 以下是实际代码:Javascript Jest测试的类型不正确。每个,javascript,angular,typescript,jestjs,ts-jest,Javascript,Angular,Typescript,Jestjs,Ts Jest,所以我用它来运行一些单元测试 以下是实际代码: const invalidTestCases = [ [null, TypeError], [undefined, TypeError], [false, TypeError], [true, TypeError], ]; describe('normalizeNames', () => { describe('invalid', () => { test.each(invalidTestCases)('s
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', () => {
// ...
});
});
问题是,由于typescript错误,我无法运行此操作:
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) => {
~~~~~~~~~~~~~~~~~~~~~~~
我还尝试使用
对象的数组
,而不是2d数组
,如下所示:
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', () => {
// ...
});
});
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', () => {
// ...
});
});
…但这样做,我无法获得对象
值的正确测试描述。我目前无法测试它,但添加类型注释通常可以修复该错误
因此,不妨尝试:
type testCaseErrorTypes=null |未定义|布尔值
const invalidTestCases:[testCaseErrorTypes,typeof TypeError][]=[
[null,TypeError],
[未定义,类型错误],
[错误,类型错误],
[正确,类型错误],
];
test.each(invalidTestCases)(“对(%p,%p)的一些描述),(实际的,预期的)=>{…}
这应该将invalidTestCases
从(testCaseErrorTypes | TypeError)[[]
转换为正确的类型[testCaseErrorTypes,TypeError][]
由于所有的期望值都相等,因此您可以使用1d数组并直接将TypeError
传递给tothrower
。如下所示:
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', () => {
// ...
});
});
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', () => {
// ...
});
});
TypeError
不满足typescript类型,但typeof TypeError
满足。此外,由于回调参数是自动推断出来的,您不需要键入这些参数。@developer033是的,我的错。但这能解决您的问题吗?(当然,一旦应用了您对它的修复)@SourceOverflow我是OP:)但你的两个解决方案对我都有效。。。谢谢哦,对了,显然我看不懂,不过很高兴它起作用了。我将用@developer033的更改更新答案