|
| 1 | +import { mockProcessStdout } from 'jest-mock-process'; |
1 | 2 | import main from '@/bin/typescript-demo-lib';
|
2 | 3 |
|
| 4 | +const uuidRegex = new RegExp( |
| 5 | + '^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}', |
| 6 | +); |
| 7 | + |
3 | 8 | describe('main', () => {
|
4 | 9 | test('main takes synthetic parameters', () => {
|
5 | 10 | // jest can also "spy on" the console object
|
6 | 11 | // and then you can test on stdout
|
7 | 12 | expect(main(['1'])).toEqual(0);
|
8 | 13 | });
|
| 14 | + test('no input', () => { |
| 15 | + const mockLog = mockProcessStdout(); |
| 16 | + main(['thing', 'thing']); |
| 17 | + expect(mockLog).toHaveBeenCalledTimes(4); |
| 18 | + expect(mockLog.mock.calls[0][0]).toBe('[]\n'); |
| 19 | + expect(mockLog.mock.calls[1][0]).toBe('new library\n'); |
| 20 | + expect(mockLog.mock.calls[2][0]).toMatch(uuidRegex); |
| 21 | + expect(mockLog.mock.calls[3][0]).toBe('NaN + NaN = NaN\n'); |
| 22 | + mockLog.mockRestore(); |
| 23 | + }); |
| 24 | + test('adds 0 + 0', () => { |
| 25 | + const mockLog = mockProcessStdout(); |
| 26 | + main(['thing', 'thing', '0', '0']); |
| 27 | + expect(mockLog).toHaveBeenCalledTimes(4); |
| 28 | + expect(mockLog.mock.calls[0][0]).toBe('[0,0]\n'); |
| 29 | + expect(mockLog.mock.calls[1][0]).toBe('new library\n'); |
| 30 | + expect(mockLog.mock.calls[2][0]).toMatch(uuidRegex); |
| 31 | + expect(mockLog.mock.calls[3][0]).toBe('0 + 0 = 0\n'); |
| 32 | + mockLog.mockRestore(); |
| 33 | + }); |
| 34 | + test('adds 0 + 1', () => { |
| 35 | + const mockLog = mockProcessStdout(); |
| 36 | + main(['thing', 'thing', '0', '1']); |
| 37 | + expect(mockLog).toHaveBeenCalledTimes(4); |
| 38 | + expect(mockLog.mock.calls[0][0]).toBe('[0,1]\n'); |
| 39 | + expect(mockLog.mock.calls[1][0]).toBe('new library\n'); |
| 40 | + expect(mockLog.mock.calls[2][0]).toMatch(uuidRegex); |
| 41 | + expect(mockLog.mock.calls[3][0]).toBe('0 + 1 = 1\n'); |
| 42 | + mockLog.mockRestore(); |
| 43 | + }); |
| 44 | + test('adds 1 + 0', () => { |
| 45 | + const mockLog = mockProcessStdout(); |
| 46 | + main(['thing', 'thing', '1', '0']); |
| 47 | + expect(mockLog).toHaveBeenCalledTimes(4); |
| 48 | + expect(mockLog.mock.calls[0][0]).toBe('[1,0]\n'); |
| 49 | + expect(mockLog.mock.calls[1][0]).toBe('new library\n'); |
| 50 | + expect(mockLog.mock.calls[2][0]).toMatch(uuidRegex); |
| 51 | + expect(mockLog.mock.calls[3][0]).toBe('1 + 0 = 1\n'); |
| 52 | + mockLog.mockRestore(); |
| 53 | + }); |
| 54 | + test('adds 7657 + 238947', () => { |
| 55 | + const mockLog = mockProcessStdout(); |
| 56 | + main(['thing', 'thing', '7657', '238947']); |
| 57 | + expect(mockLog).toHaveBeenCalledTimes(4); |
| 58 | + expect(mockLog.mock.calls[0][0]).toBe('[7657,238947]\n'); |
| 59 | + expect(mockLog.mock.calls[1][0]).toBe('new library\n'); |
| 60 | + expect(mockLog.mock.calls[2][0]).toMatch(uuidRegex); |
| 61 | + expect(mockLog.mock.calls[3][0]).toBe('7657 + 238947 = 246604\n'); |
| 62 | + mockLog.mockRestore(); |
| 63 | + }); |
9 | 64 | });
|
0 commit comments