/
warnings.test.js
55 lines (41 loc) · 1.89 KB
/
warnings.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
'use strict';
const { run } = require('../utils/test-utils');
const { existsSync, readFile } = require('fs');
const { resolve } = require('path');
describe('warnings', () => {
it('should output by default', () => {
const { exitCode, stderr, stdout } = run(__dirname);
expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toMatch(/WARNING/);
expect(stdout).toMatch(/Error: Can't resolve/);
});
it('should output JSON with the "json" flag', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json']);
expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(() => JSON.parse(stdout)).not.toThrow();
const json = JSON.parse(stdout);
expect(json['hash']).toBeDefined();
expect(json['warnings']).toHaveLength(2);
// `message` for `webpack@5`
expect(json['warnings'][0].message ? json['warnings'][0].message : json['warnings'][0]).toMatch(/Can't resolve/);
});
it('should store json to a file', (done) => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json']);
expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toContain('stats are successfully stored as json to stats.json');
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();
readFile(resolve(__dirname, 'stats.json'), 'utf-8', (error, data) => {
expect(error).toBe(null);
expect(() => JSON.parse(data)).not.toThrow();
const json = JSON.parse(data);
expect(json['hash']).toBeDefined();
expect(json['warnings']).toHaveLength(2);
// `message` for `webpack@5`
expect(json['warnings'][0].message ? json['warnings'][0].message : json['warnings'][0]).toMatch(/Can't resolve/);
done();
});
});
});