/
json.test.js
53 lines (47 loc) · 1.91 KB
/
json.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
const { run } = require('../utils/test-utils');
const { stat, readFile } = require('fs');
const { resolve } = require('path');
describe('json flag', () => {
it('should return valid json', () => {
const { stdout } = run(__dirname, ['--json']);
// helper function to check if JSON is valid
const parseJson = () => {
return JSON.parse(stdout);
};
// check the JSON is valid.
expect(JSON.parse(stdout)['hash']).toBeTruthy();
expect(JSON.parse(stdout)['version']).toBeTruthy();
expect(JSON.parse(stdout)['time']).toBeTruthy();
expect(parseJson).not.toThrow();
});
it('should store json to a file', (done) => {
const { stdout } = run(__dirname, ['--json', 'stats.json']);
expect(stdout).toContain('stats are successfully stored as json to stats.json');
stat(resolve(__dirname, './stats.json'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
readFile(resolve(__dirname, 'stats.json'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(JSON.parse(data)['hash']).toBeTruthy();
expect(JSON.parse(data)['version']).toBeTruthy();
expect(JSON.parse(data)['time']).toBeTruthy();
expect(() => JSON.parse(data)).not.toThrow();
done();
});
});
it('should return valid json with -j alias', () => {
const { stdout } = run(__dirname, ['-j']);
// helper function to check if JSON is valid
const parseJson = () => {
return JSON.parse(stdout);
};
// check the JSON is valid.
expect(JSON.parse(stdout)['hash']).toBeTruthy();
expect(JSON.parse(stdout)['version']).toBeTruthy();
expect(JSON.parse(stdout)['time']).toBeTruthy();
expect(parseJson).not.toThrow();
});
});