admin管理员组

文章数量:1323317

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.

Share edited Apr 12, 2020 at 15:03 dev_054 asked Apr 12, 2020 at 14:51 dev_054dev_054 3,7489 gold badges36 silver badges59 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

I'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