Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Update TypeScript example to show use of newer Jest types #10399

Merged
merged 4 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
88 changes: 88 additions & 0 deletions examples/typescript/__tests__/calc.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof sub>;
const mockSum = sum as jest.MockedFunction<typeof sum>;
const MockMemory = Memory as jest.MockedClass<typeof Memory>;

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'),
);
});
});
3 changes: 2 additions & 1 deletion examples/typescript/__tests__/sub-test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
2 changes: 2 additions & 0 deletions examples/typescript/__tests__/sum-test.ts
Original file line number Diff line number Diff line change
@@ -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;
SimenB marked this conversation as resolved.
Show resolved Hide resolved
expect(sum(1, 2)).toBe(3);
});
Expand Down
39 changes: 39 additions & 0 deletions examples/typescript/calc.ts
Original file line number Diff line number Diff line change
@@ -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>): 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');
}
}
};
};
23 changes: 23 additions & 0 deletions examples/typescript/memory.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}