Skip to content

Commit 1b8f70a

Browse files
authored
Update TypeScript example to show use of newer Jest types (#10399)
1 parent 63409db commit 1b8f70a

File tree

6 files changed

+155
-1
lines changed

6 files changed

+155
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
- `[docs]` Add docs for using mocks in TypeScript([#10415](https://github.com/facebook/jest/pull/10415))
1818
- `[jest-cli]` chore: standardize files and folder names ([#10698](https://github.com/facebook/jest/pull/1098))
19+
- `[examples]` Update TypeScript example to show use of newer Jest types ([#10399](https://github.com/facebook/jest/pull/10399))
1920

2021
### Performance
2122

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import Memory from '../memory';
2+
import sub from '../sub';
3+
import sum from '../sum';
4+
import makeCalc from '../calc';
5+
6+
jest.mock('../memory');
7+
jest.mock('../sub');
8+
jest.mock('../sum');
9+
10+
const mockSub = sub as jest.MockedFunction<typeof sub>;
11+
const mockSum = sum as jest.MockedFunction<typeof sum>;
12+
const MockMemory = Memory as jest.MockedClass<typeof Memory>;
13+
14+
describe('calc - mocks', () => {
15+
const memory = new MockMemory();
16+
17+
it('returns result from subtract', () => {
18+
mockSub.mockReturnValueOnce(0);
19+
20+
const calc = makeCalc(memory);
21+
const result = calc('Sub', [2, 2]);
22+
23+
expect(result).toEqual(0);
24+
expect(mockSub).toBeCalledWith(2, 2);
25+
});
26+
27+
it('returns result from sum', () => {
28+
mockSum.mockReturnValueOnce(2);
29+
30+
const calc = makeCalc(memory);
31+
const result = calc('Sum', [1, 1]);
32+
33+
expect(result).toEqual(2);
34+
expect(mockSum).toBeCalledWith(1, 1);
35+
});
36+
37+
it('adds last result to memory', () => {
38+
MockMemory.prototype.add.mockImplementationOnce(x => x);
39+
mockSum.mockReturnValueOnce(2);
40+
41+
const calc = makeCalc(memory);
42+
const sumResult = calc('Sum', [1, 1]);
43+
const memoryResult = calc('MemoryAdd', []);
44+
45+
expect(sumResult).toEqual(2);
46+
expect(memoryResult).toEqual(2);
47+
expect(MockMemory.prototype.add).toBeCalledWith(2);
48+
});
49+
50+
it('subtracts last result to memory', () => {
51+
MockMemory.prototype.subtract.mockImplementationOnce(x => x);
52+
mockSum.mockReturnValueOnce(2);
53+
54+
const calc = makeCalc(memory);
55+
const sumResult = calc('Sum', [1, 1]);
56+
const memoryResult = calc('MemorySub', []);
57+
58+
expect(sumResult).toEqual(2);
59+
expect(memoryResult).toEqual(2);
60+
expect(MockMemory.prototype.subtract).toBeCalledWith(2);
61+
});
62+
63+
it('clears the memory', () => {
64+
MockMemory.prototype.add.mockImplementationOnce(x => x);
65+
mockSum.mockReturnValueOnce(2).mockReturnValueOnce(4);
66+
67+
const calc = makeCalc(memory);
68+
const sumResult = calc('Sum', [1, 1]);
69+
const memoryResult = calc('MemoryAdd', []);
70+
const sumResult2 = calc('Sum', [2, 2]);
71+
const clearResult = calc('MemoryClear', []);
72+
73+
expect(sumResult).toEqual(2);
74+
expect(memoryResult).toEqual(2);
75+
expect(sumResult2).toEqual(4);
76+
expect(clearResult).toEqual(4);
77+
expect(MockMemory.prototype.reset).toBeCalledTimes(1);
78+
});
79+
80+
it('throws an error when invalid Op is passed', () => {
81+
const calc = makeCalc(memory);
82+
83+
// @ts-expect-error
84+
expect(() => calc('Multiply', [2, 3])).toThrowError(
85+
new Error('Invalid op'),
86+
);
87+
});
88+
});
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
22

3+
import sub from '../sub';
4+
35
it('subtracts 5 - 1 to equal 4 in TypeScript', () => {
4-
const sub = require('../sub').default;
56
expect(sub(5, 1)).toBe(4);
67
});

examples/typescript/__tests__/sum-test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
22

33
it('adds 1 + 2 to equal 3 in TScript', () => {
4+
// Generally, `import` should be used for TypeScript
5+
// as using `require` will not return any type information.
46
const sum = require('../sum.ts').default;
57
expect(sum(1, 2)).toBe(3);
68
});

examples/typescript/calc.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import sub from './sub';
2+
import sum from './sum';
3+
import Memory from './memory';
4+
5+
type Op = 'MemoryAdd' | 'MemoryClear' | 'MemorySub' | 'Sub' | 'Sum';
6+
7+
export default (memory: Memory) => {
8+
let last = 0;
9+
10+
return (op: Op, input: Array<number>): number => {
11+
switch (op) {
12+
case 'MemoryAdd': {
13+
return memory.add(last);
14+
}
15+
case 'MemoryClear': {
16+
memory.reset();
17+
return last;
18+
}
19+
case 'MemorySub': {
20+
return memory.subtract(last);
21+
}
22+
case 'Sub': {
23+
const [a, b] = input;
24+
const result = sub(a, b);
25+
last = result;
26+
return result;
27+
}
28+
case 'Sum': {
29+
const [a, b] = input;
30+
const result = sum(a, b);
31+
last = result;
32+
return result;
33+
}
34+
default: {
35+
throw new Error('Invalid op');
36+
}
37+
}
38+
};
39+
};

examples/typescript/memory.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export default class Memory {
2+
current: number;
3+
4+
constructor() {
5+
this.current = 0;
6+
}
7+
8+
add(entry: number) {
9+
this.current += entry;
10+
11+
return this.current;
12+
}
13+
14+
subtract(entry: number) {
15+
this.current -= entry;
16+
17+
return this.current;
18+
}
19+
20+
reset() {
21+
this.current = 0;
22+
}
23+
}

0 commit comments

Comments
 (0)