From b12e2e61a08bcc49ca2fbce9ccf9c058397f8cc0 Mon Sep 17 00:00:00 2001 From: Geovani de Souza Date: Fri, 29 Dec 2017 14:10:01 -0200 Subject: [PATCH] Add typings for jest-in-case --- types/jest-in-case/index.d.ts | 25 +++++ types/jest-in-case/jest-in-case-tests.ts | 121 +++++++++++++++++++++++ types/jest-in-case/tsconfig.json | 23 +++++ types/jest-in-case/tslint.json | 1 + 4 files changed, 170 insertions(+) create mode 100644 types/jest-in-case/index.d.ts create mode 100644 types/jest-in-case/jest-in-case-tests.ts create mode 100644 types/jest-in-case/tsconfig.json create mode 100644 types/jest-in-case/tslint.json diff --git a/types/jest-in-case/index.d.ts b/types/jest-in-case/index.d.ts new file mode 100644 index 0000000000..02d7d08af9 --- /dev/null +++ b/types/jest-in-case/index.d.ts @@ -0,0 +1,25 @@ +// Type definitions for jest-in-case 1.0 +// Project: https://github.com/thinkmill/jest-in-case#readme +// Definitions by: Geovani de Souza +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +/// +/// + +declare const global: NodeJS.Global; + +interface Config { + name?: string; + only?: boolean; + skip?: boolean; + [key: string]: any; +} + +type Tester = (opts: Opts, done: jest.DoneCallback) => any; + +type TestCases = ReadonlyArray | { [name: string]: Opts }; + +declare function cases(title: string, tester: Tester, testCases: TestCases): void; + +export = cases; diff --git a/types/jest-in-case/jest-in-case-tests.ts b/types/jest-in-case/jest-in-case-tests.ts new file mode 100644 index 0000000000..e0e5de4f89 --- /dev/null +++ b/types/jest-in-case/jest-in-case-tests.ts @@ -0,0 +1,121 @@ +/// +/// + +declare const global: any; + +import cases = require("jest-in-case"); + +function add(augend: number, addend: number) { + return augend + addend; +} + +function subtract(minuend: number, subtrahend: number) { + return minuend - subtrahend; +} + +beforeEach(() => { + jest.spyOn(global, 'describe').mockImplementation((title, fn) => fn()); + jest.spyOn(global, 'test').mockImplementation((name, fn) => fn()); + global.test.skip = jest.fn((name, fn) => fn()); + global.test.only = jest.fn((name, fn) => fn()); +}); + +afterEach(() => { + global.describe.mockRestore(); + global.test.mockRestore(); +}); + +test('array', () => { + const title = 'add(augend, addend)'; + + const tester = jest.fn(opts => { + expect(add(opts.augend, opts.addend)).toBe(opts.total); + }); + + const testCases = [ + { name: '1 + 1 = 2', augend: 1, addend: 1, total: 2 }, + { name: '2 + 1 = 3', augend: 2, addend: 1, total: 3 }, + { name: '3 + 1 = 4', augend: 3, addend: 1, total: 4 }, + ]; + + cases(title, tester, testCases); + + expect(global.describe).toHaveBeenCalledTimes(1); + expect(global.test).toHaveBeenCalledTimes(3); + expect(tester).toHaveBeenCalledTimes(3); + + expect(global.describe.mock.calls[0][0]).toBe(title); + expect(global.test.mock.calls[0][0]).toBe(testCases[0].name); + expect(global.test.mock.calls[1][0]).toBe(testCases[1].name); + expect(global.test.mock.calls[2][0]).toBe(testCases[2].name); + expect(global.test.mock.calls[0][1]).toHaveLength(0); + expect(global.test.mock.calls[1][1]).toHaveLength(0); + expect(global.test.mock.calls[2][1]).toHaveLength(0); + expect(tester).toHaveBeenCalledWith(testCases[0]); + expect(tester).toHaveBeenCalledWith(testCases[1]); + expect(tester).toHaveBeenCalledWith(testCases[2]); +}); + +test('object', () => { + jest.spyOn(global, 'describe').mockImplementation((title, fn) => fn()); + jest.spyOn(global, 'test').mockImplementation((name, fn) => fn()); + + const title = 'add(augend, addend)'; + + const tester = jest.fn(opts => { + expect(subtract(opts.minuend, opts.subtrahend)).toBe(opts.difference); + }); + + const testCases = { + '1 - 1 = 0': { minuend: 1, subtrahend: 1, difference: 0 }, + '2 - 1 = 1': { minuend: 2, subtrahend: 1, difference: 1 }, + '3 - 1 = 2': { minuend: 3, subtrahend: 1, difference: 2 }, + }; + + cases(title, tester, testCases); + + expect(global.describe).toHaveBeenCalledTimes(1); + expect(global.test).toHaveBeenCalledTimes(3); + expect(tester).toHaveBeenCalledTimes(3); + + expect(global.describe.mock.calls[0][0]).toBe(title); + expect(global.test.mock.calls[0][0]).toBe('1 - 1 = 0'); + expect(global.test.mock.calls[1][0]).toBe('2 - 1 = 1'); + expect(global.test.mock.calls[2][0]).toBe('3 - 1 = 2'); + expect(global.test.mock.calls[0][1]).toHaveLength(0); + expect(global.test.mock.calls[1][1]).toHaveLength(0); + expect(global.test.mock.calls[2][1]).toHaveLength(0); + expect(tester.mock.calls[0][0]).toMatchObject(testCases['1 - 1 = 0']); + expect(tester.mock.calls[1][0]).toMatchObject(testCases['2 - 1 = 1']); + expect(tester.mock.calls[2][0]).toMatchObject(testCases['3 - 1 = 2']); +}); + +test('no names', () => { + cases('foo', () => { }, [ + {}, + {}, + ]); + + expect(global.test.mock.calls[0][0]).toBe('case: 1'); + expect(global.test.mock.calls[1][0]).toBe('case: 2'); +}); + +test('only', () => { + cases('foo', () => { }, [ + {}, + { only: true }, + ]); + + expect(global.test.mock.calls[0][0]).toBe('case: 1'); + expect(global.test.only.mock.calls[0][0]).toBe('case: 2'); +}); + +test('skip', () => { + cases('foo', () => { }, [ + {}, + { skip: true }, + ]); + + expect(global.test.mock.calls[0][0]).toBe('case: 1'); + expect(global.test.skip.mock.calls[0][0]).toBe('case: 2'); +}); diff --git a/types/jest-in-case/tsconfig.json b/types/jest-in-case/tsconfig.json new file mode 100644 index 0000000000..a561ae813a --- /dev/null +++ b/types/jest-in-case/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "jest-in-case-tests.ts" + ] +} diff --git a/types/jest-in-case/tslint.json b/types/jest-in-case/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/jest-in-case/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }