/
json.test.js
144 lines (117 loc) · 5.78 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';
const stripAnsi = require('strip-ansi');
const { run } = require('../utils/test-utils');
const { existsSync, readFile } = require('fs');
const { resolve } = require('path');
const successMessage = 'stats are successfully stored as json to stats.json';
describe('json', () => {
it('should work and output json stats', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json']);
expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(() => JSON.parse(stdout)).not.toThrow();
expect(JSON.parse(stdout)['hash']).toBeDefined();
});
it('should work and store json to a file', (done) => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json']);
expect(exitCode).toBe(0);
expect(stderr).toContain(successMessage);
expect(stdout).toBeFalsy();
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();
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 work and store json to a file and respect --color flag', (done) => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json', '--color']);
expect(exitCode).toBe(0);
expect(stderr).toContain(`\u001b[32m${successMessage}`);
expect(stdout).toBeFalsy();
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();
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 work and store json to a file and respect --no-color', (done) => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json', '--no-color']);
expect(exitCode).toBe(0);
expect(stderr).not.toContain(`\u001b[32m${successMessage}`);
expect(stderr).toContain(`${successMessage}`);
expect(stdout).toBeFalsy();
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();
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 work using the "-j" option (alias)', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['-j']);
expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(() => JSON.parse(stdout)).not.toThrow();
expect(JSON.parse(stdout)['hash']).toBeDefined();
});
it('should work and output json stats with the "--progress" option', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json', '--progress']);
expect(exitCode).toBe(0);
expect(stderr).toContain('webpack.Progress');
expect(() => JSON.parse(stdout)).not.toThrow();
expect(JSON.parse(stdout)['hash']).toBeDefined();
});
it('should work and store json to a file with the "--progress" option', (done) => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json', '--progress']);
expect(exitCode).toBe(0);
expect(stderr).toContain('webpack.Progress');
expect(stderr).toContain(successMessage);
expect(stdout).toBeFalsy();
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();
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 work and output json stats with cli logs', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json', '--config', 'logging.config.js']);
expect(exitCode).toBe(0);
expect(stderr).toContain('Compiler starting...');
expect(stderr).toContain('Compiler finished');
expect(() => JSON.parse(stdout)).not.toThrow();
expect(JSON.parse(stdout)['hash']).toBeDefined();
});
it('should work and store json to a file with cli logs', (done) => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json', '--config', 'logging.config.js']);
expect(exitCode).toBe(0);
const pureStderr = stripAnsi(stderr);
expect(stderr).toContain('Compiler starting...');
expect(pureStderr).toContain('Compiler finished');
expect(pureStderr).toContain(successMessage);
expect(stdout).toBeFalsy();
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();
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();
});
});
});