Skip to content

Commit 7eb43cd

Browse files
authored
Merge pull request #24 from MatrixAI/fiddling-with-things
Adding Jest Test for stdout and console.log usage in CLI commands
2 parents a685e67 + cc02777 commit 7eb43cd

File tree

5 files changed

+91
-3
lines changed

5 files changed

+91
-3
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"eslint-config-prettier": "^7.1.0",
3535
"eslint-plugin-prettier": "^3.3.1",
3636
"jest": "^26.6.3",
37+
"jest-mock-process": "^1.4.0",
3738
"pkg": "^5.3.0",
3839
"prettier": "^2.2.1",
3940
"ts-jest": "^26.4.4",

src/bin/typescript-demo-lib.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,28 @@
22

33
import process from 'process';
44
import Library from '../lib/Library';
5+
import NumPair from '../lib/NumPair';
56
import { v4 as uuidv4 } from 'uuid';
67

78
function main(argv = process.argv): number {
8-
console.log(argv.slice(2));
9+
// Print out command-line arguments
10+
const argArray = argv.slice(2);
11+
const args = argArray.toString();
12+
process.stdout.write('[' + args + ']\n');
13+
14+
// Create a new Library with the value someParam = 'new library'
15+
// And print it out
916
const l = new Library('new library');
10-
console.log(l.someParam);
11-
console.log(uuidv4());
17+
process.stdout.write(l.someParam + '\n');
18+
19+
// Generate and print a uuid (universally unique identifier)
20+
process.stdout.write(uuidv4() + '\n');
21+
22+
// Add the first two command-line args and print the result
23+
const nums = new NumPair(parseInt(argv[2]), parseInt(argv[3]));
24+
const sum = nums.num1 + nums.num2;
25+
process.stdout.write(nums.num1 + ' + ' + nums.num2 + ' = ' + sum + '\n');
26+
1227
process.exitCode = 0;
1328
return process.exitCode;
1429
}

src/lib/NumPair.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class NumPair {
2+
num1: number;
3+
num2: number;
4+
5+
constructor(firstNum = 0, secondNum = 0) {
6+
this.num1 = firstNum;
7+
this.num2 = secondNum;
8+
}
9+
}
10+
11+
export default NumPair;

tests/bin/typescript-demo-lib.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,64 @@
1+
import { mockProcessStdout } from 'jest-mock-process';
12
import main from '@/bin/typescript-demo-lib';
23

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+
38
describe('main', () => {
49
test('main takes synthetic parameters', () => {
510
// jest can also "spy on" the console object
611
// and then you can test on stdout
712
expect(main(['1'])).toEqual(0);
813
});
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+
});
964
});

0 commit comments

Comments
 (0)