javascript - Felaktiga typer för Jest test.each


Så jag använder Jest#test.each för att köra några enhetstester.

Här är den faktiska koden:

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', () => {
    // ...
  });
});

Problemet är att jag inte kan köra detta på grund av TypeScript-fel:

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) => {
                                                       ~~~~~~~~~~~~~~~~~~~~~~~

Jag försökte också använda en array av objekt istället för 2d array, så här:

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', () => {
    // ...
  });
});

...men genom att göra på det här sättet kan jag inte få korrekt testbeskrivning för object-värden.

Svar #1

Jag är för närvarande inte på en plats där jag kan testa det, men att lägga till typannoteringar brukar fixa det felet.

Så kanske prova:

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) => { … }

Detta bör omvandla invalidTestCases från en (testCaseErrorTypes|TypeError)[][] till rätt typ [testCaseErrorTypes, TypeError][].

Svar #2

Eftersom alla expected-värden är lika kan du bara använda en 1D-array och skicka TypeError direkt till toThrowError. Det skulle vara något i stil med detta:

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', () => {
    // ...
  });
});

Relaterade artiklar