From 517d66f2f22a8a791d9da7a2cefa1418c32e6658 Mon Sep 17 00:00:00 2001 From: Chris Shepherd Date: Wed, 12 Aug 2020 21:33:14 +0100 Subject: [PATCH 1/4] chore: provide example of newer Jest types for TypeScript --- examples/typescript/__tests__/calc.test.ts | 88 ++++++++++++++++++++++ examples/typescript/__tests__/sub-test.ts | 3 +- examples/typescript/__tests__/sum-test.ts | 7 +- examples/typescript/calc.ts | 37 +++++++++ examples/typescript/memory.ts | 23 ++++++ 5 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 examples/typescript/__tests__/calc.test.ts create mode 100644 examples/typescript/calc.ts create mode 100644 examples/typescript/memory.ts diff --git a/examples/typescript/__tests__/calc.test.ts b/examples/typescript/__tests__/calc.test.ts new file mode 100644 index 000000000000..ef5039e6acdd --- /dev/null +++ b/examples/typescript/__tests__/calc.test.ts @@ -0,0 +1,88 @@ +import Memory from '../memory'; +import sub from '../sub'; +import sum from '../sum'; +import makeCalc from '../calc'; + +jest.mock('../memory'); +jest.mock('../sub'); +jest.mock('../sum'); + +const mockSub = sub as jest.MockedFunction; +const mockSum = sum as jest.MockedFunction; +const MockMemory = Memory as jest.MockedClass; + +describe('calc - mocks', () => { + const memory = new MockMemory(); + + it('returns result from subtract', () => { + mockSub.mockReturnValueOnce(0); + + const calc = makeCalc(memory); + const result = calc('Sub', [2, 2]); + + expect(result).toEqual(0); + expect(mockSub).toBeCalledWith(2, 2); + }); + + it('returns result from sum', () => { + mockSum.mockReturnValueOnce(2); + + const calc = makeCalc(memory); + const result = calc('Sum', [1, 1]); + + expect(result).toEqual(2); + expect(mockSum).toBeCalledWith(1, 1); + }); + + it('adds last result to memory', () => { + MockMemory.prototype.add.mockImplementationOnce(x => x); + mockSum.mockReturnValueOnce(2); + + const calc = makeCalc(memory); + const sumResult = calc('Sum', [1, 1]); + const memoryResult = calc('MemoryAdd', []); + + expect(sumResult).toEqual(2); + expect(memoryResult).toEqual(2); + expect(MockMemory.prototype.add).toBeCalledWith(2); + }); + + it('subtracts last result to memory', () => { + MockMemory.prototype.subtract.mockImplementationOnce(x => x); + mockSum.mockReturnValueOnce(2); + + const calc = makeCalc(memory); + const sumResult = calc('Sum', [1, 1]); + const memoryResult = calc('MemorySub', []); + + expect(sumResult).toEqual(2); + expect(memoryResult).toEqual(2); + expect(MockMemory.prototype.subtract).toBeCalledWith(2); + }); + + it('clears the memory', () => { + MockMemory.prototype.add.mockImplementationOnce(x => x); + mockSum.mockReturnValueOnce(2).mockReturnValueOnce(4); + + const calc = makeCalc(memory); + const sumResult = calc('Sum', [1, 1]); + const memoryResult = calc('MemoryAdd', []); + const sumResult2 = calc('Sum', [2, 2]); + const clearResult = calc('MemoryClear', []); + + expect(sumResult).toEqual(2); + expect(memoryResult).toEqual(2); + expect(sumResult2).toEqual(4); + expect(clearResult).toEqual(4); + expect(MockMemory.prototype.reset).toBeCalledTimes(1); + }); + + it('throws an error when invalid Op is passed', () => { + const calc = makeCalc(memory); + + // @ts-expect-error + expect(() => calc('Multiply', [2, 3])).toThrowError( + new Error('Invalid op'), + ); + }); +}); diff --git a/examples/typescript/__tests__/sub-test.ts b/examples/typescript/__tests__/sub-test.ts index acebfc9010f0..98633bcd3ea3 100644 --- a/examples/typescript/__tests__/sub-test.ts +++ b/examples/typescript/__tests__/sub-test.ts @@ -1,6 +1,7 @@ // Copyright (c) 2014-present, Facebook, Inc. All rights reserved. +import sub from '../sub'; + it('subtracts 5 - 1 to equal 4 in TypeScript', () => { - const sub = require('../sub').default; expect(sub(5, 1)).toBe(4); }); diff --git a/examples/typescript/__tests__/sum-test.ts b/examples/typescript/__tests__/sum-test.ts index 9d2e0c3a4c66..4fd093c09f06 100644 --- a/examples/typescript/__tests__/sum-test.ts +++ b/examples/typescript/__tests__/sum-test.ts @@ -1,11 +1,12 @@ // Copyright (c) 2014-present, Facebook, Inc. All rights reserved. +import sum from '../sum'; + it('adds 1 + 2 to equal 3 in TScript', () => { - const sum = require('../sum.ts').default; expect(sum(1, 2)).toBe(3); }); it('adds 1 + 2 to equal 3 in JavaScript', () => { - const sum = require('../sum.js'); - expect(sum(1, 2)).toBe(3); + const sumJs = require('../sum.js'); + expect(sumJs(1, 2)).toBe(3); }); diff --git a/examples/typescript/calc.ts b/examples/typescript/calc.ts new file mode 100644 index 000000000000..f1b90f63ef14 --- /dev/null +++ b/examples/typescript/calc.ts @@ -0,0 +1,37 @@ +import sub from './sub'; +import sum from './sum'; +import Memory from './memory'; + +type Op = 'MemoryAdd' | 'MemoryClear' | 'MemorySub' | 'Sub' | 'Sum'; + +export default (memory: Memory) => { + let last = 0; + + return (op: Op, input: Array): number => { + switch (op) { + case 'MemoryAdd': { + return memory.add(last); + } + case 'MemoryClear': { + memory.reset(); + return last; + } + case 'MemorySub': { + return memory.subtract(last); + } + case 'Sub': { + const result = sub.apply(null, input); + last = result; + return result; + } + case 'Sum': { + const result = sum.apply(null, input); + last = result; + return result; + } + default: { + throw new Error('Invalid op'); + } + } + }; +}; diff --git a/examples/typescript/memory.ts b/examples/typescript/memory.ts new file mode 100644 index 000000000000..c1158a416eaf --- /dev/null +++ b/examples/typescript/memory.ts @@ -0,0 +1,23 @@ +export default class Memory { + current: number; + + constructor() { + this.current = 0; + } + + add(entry: number) { + this.current += entry; + + return this.current; + } + + subtract(entry: number) { + this.current -= entry; + + return this.current; + } + + reset() { + this.current = 0; + } +} From 4d5b84322d40e41c051510182b70091b392bf17a Mon Sep 17 00:00:00 2001 From: Chris Shepherd Date: Wed, 12 Aug 2020 22:06:16 +0100 Subject: [PATCH 2/4] chore: add CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bb053236ea6..7e9903b5bc66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - `[docs]` Add docs for using mocks in TypeScript([#10415](https://github.com/facebook/jest/pull/10415)) - `[jest-cli]` chore: standardize files and folder names ([#10698](https://github.com/facebook/jest/pull/1098)) +- `[examples]` Update TypeScript example to show use of newer Jest types ([#10399](https://github.com/facebook/jest/pull/10399)) ### Performance From 17fda863b94e78f0a9191cf8718426229387bea2 Mon Sep 17 00:00:00 2001 From: Chris Shepherd Date: Wed, 12 Aug 2020 22:22:50 +0100 Subject: [PATCH 3/4] Revert change to TypeScript sum test --- examples/typescript/__tests__/sum-test.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/typescript/__tests__/sum-test.ts b/examples/typescript/__tests__/sum-test.ts index 4fd093c09f06..2eb4f77dd7c5 100644 --- a/examples/typescript/__tests__/sum-test.ts +++ b/examples/typescript/__tests__/sum-test.ts @@ -1,12 +1,13 @@ // Copyright (c) 2014-present, Facebook, Inc. All rights reserved. -import sum from '../sum'; - it('adds 1 + 2 to equal 3 in TScript', () => { + // Generally, `import` should be used for TypeScript + // as using `require` will not return any type information. + const sum = require('../sum.ts').default; expect(sum(1, 2)).toBe(3); }); it('adds 1 + 2 to equal 3 in JavaScript', () => { - const sumJs = require('../sum.js'); - expect(sumJs(1, 2)).toBe(3); + const sum = require('../sum.js'); + expect(sum(1, 2)).toBe(3); }); From ecf094c52c139d0dceec510ff76de07c81c9a843 Mon Sep 17 00:00:00 2001 From: Chris Shepherd Date: Thu, 29 Oct 2020 17:14:45 +0000 Subject: [PATCH 4/4] fix(examples): remove use of apply --- examples/typescript/calc.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/typescript/calc.ts b/examples/typescript/calc.ts index f1b90f63ef14..00a934b64559 100644 --- a/examples/typescript/calc.ts +++ b/examples/typescript/calc.ts @@ -20,12 +20,14 @@ export default (memory: Memory) => { return memory.subtract(last); } case 'Sub': { - const result = sub.apply(null, input); + const [a, b] = input; + const result = sub(a, b); last = result; return result; } case 'Sum': { - const result = sum.apply(null, input); + const [a, b] = input; + const result = sum(a, b); last = result; return result; }