From dba82e8e0ebe8d65d5feaffbc18d597578c728c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Miko=C5=82ajczyk?= Date: Sat, 16 Jun 2018 14:49:15 +0200 Subject: [PATCH 1/2] Definitions for jest's each function --- types/jest/index.d.ts | 11 +++++ types/jest/jest-tests.ts | 100 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 2afe0eb750..ce0ff1439a 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -15,6 +15,7 @@ // Bradley Ayers // Jeff Lau // Andrew Makarov +// Paweł Mikołajczyk // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -209,6 +210,14 @@ declare namespace jest { readonly name: string; } + interface Each { + (cases: any[]): (name: string, fn: (...args: any[]) => any) => void; + (strings: TemplateStringsArray, ...placeholders: any[]): ( + name: string, + fn: (arg: any) => any + ) => void; + } + /** * Creates a test closure */ @@ -227,6 +236,7 @@ declare namespace jest { only: It; skip: It; concurrent: It; + each: Each; } interface Describe { @@ -234,6 +244,7 @@ declare namespace jest { (name: number | string | Function | FunctionLike, fn: EmptyFunction): void; only: Describe; skip: Describe; + each: Each; } interface MatcherUtils { diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index e154561c86..393890025b 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -760,3 +760,103 @@ describe('toHaveBeenNthCalledWith', () => { expect(fn).toHaveBeenNthCalledWith(3, "foo"); }); + +// https://github.com/DefinitelyTyped/DefinitelyTyped/issues/26368 + +describe.each([[1, 1, 2], [1, 2, 3], [2, 1, 3]])( + ".add(%i, %i)", + (a: number, b: number, expected: number) => { + test(`returns ${expected}`, () => { + expect(a + b).toBe(expected); + }); + } +); + +interface Case { + a: number; + b: number; + expected: number; +} + +describe.each` + a | b | expected + ${1} | ${1} | ${2} + ${1} | ${2} | ${3} + ${2} | ${1} | ${3} +`("$a + $b", ({ a, b, expected }: Case) => { + test(`returns ${expected}`, () => { + expect(a + b).toBe(expected); + }); +}); + +describe.only.each([[1, 1, 2], [1, 2, 3], [2, 1, 3]])( + ".add(%i, %i)", + (a, b, expected) => { + test(`returns ${expected}`, () => { + expect(a + b).toBe(expected); + }); + } +); + +describe.only.each` + a | b | expected + ${1} | ${1} | ${2} + ${1} | ${2} | ${3} + ${2} | ${1} | ${3} +`("$a + $b", ({ a, b, expected }: Case) => { + test(`returns ${expected}`, () => { + expect(a + b).toBe(expected); + }); +}); + +describe.skip.each([[1, 1, 2], [1, 2, 3], [2, 1, 3]])( + ".add(%i, %i)", + (a, b, expected) => { + test(`returns ${expected}`, () => { + expect(a + b).toBe(expected); + }); + } +); + +describe.skip.each` + a | b | expected + ${1} | ${1} | ${2} + ${1} | ${2} | ${3} + ${2} | ${1} | ${3} +`("$a + $b", ({ a, b, expected }: Case) => { + test(`returns ${expected}`, () => { + expect(a + b).toBe(expected); + }); +}); + +test.each([[1, 1, 2], [1, 2, 3], [2, 1, 3]])( + ".add(%i, %i)", + (a, b, expected) => { + expect(a + b).toBe(expected); + } +); + +test.each` + a | b | expected + ${1} | ${1} | ${2} + ${1} | ${2} | ${3} + ${2} | ${1} | ${3} +`("returns $expected when $a is added $b", ({ a, b, expected }: Case) => { + expect(a + b).toBe(expected); +}); + +test.only.each([[1, 1, 2], [1, 2, 3], [2, 1, 3]])( + ".add(%i, %i)", + (a, b, expected) => { + expect(a + b).toBe(expected); + } +); + +test.only.each` + a | b | expected + ${1} | ${1} | ${2} + ${1} | ${2} | ${3} + ${2} | ${1} | ${3} +`("returns $expected when $a is added $b", ({ a, b, expected }: Case) => { + expect(a + b).toBe(expected); +}); From 898ed0479ceda021e9200a275379da0e28e19b69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Miko=C5=82ajczyk?= Date: Sat, 16 Jun 2018 15:38:14 +0200 Subject: [PATCH 2/2] Removed unnecessary line from merge --- types/jest/jest-tests.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index a03c49330b..7d10075262 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -1056,4 +1056,3 @@ test.only.each` `("returns $expected when $a is added $b", ({ a, b, expected }: Case) => { expect(a + b).toBe(expected); }); -