From 1b8f70ae50628106c4712aafed4f36ae45c2180c Mon Sep 17 00:00:00 2001 From: Chris Shepherd Date: Fri, 30 Oct 2020 10:06:32 +0000 Subject: [PATCH] Update TypeScript example to show use of newer Jest types (#10399) --- CHANGELOG.md | 1 + examples/typescript/__tests__/calc.test.ts | 88 ++++++++++++++++++++++ examples/typescript/__tests__/sub-test.ts | 3 +- examples/typescript/__tests__/sum-test.ts | 2 + examples/typescript/calc.ts | 39 ++++++++++ examples/typescript/memory.ts | 23 ++++++ 6 files changed, 155 insertions(+), 1 deletion(-) 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/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 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..2eb4f77dd7c5 100644 --- a/examples/typescript/__tests__/sum-test.ts +++ b/examples/typescript/__tests__/sum-test.ts @@ -1,6 +1,8 @@ // Copyright (c) 2014-present, Facebook, Inc. All rights reserved. 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); }); diff --git a/examples/typescript/calc.ts b/examples/typescript/calc.ts new file mode 100644 index 000000000000..00a934b64559 --- /dev/null +++ b/examples/typescript/calc.ts @@ -0,0 +1,39 @@ +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 [a, b] = input; + const result = sub(a, b); + last = result; + return result; + } + case 'Sum': { + const [a, b] = input; + const result = sum(a, b); + 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; + } +}