-
-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: avoid unnecessary stringify (#1920)
- Loading branch information
1 parent
d32aeda
commit 5ef1e7b
Showing
8 changed files
with
170 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,3 +65,4 @@ test/**/**/binary/** | |
test/**/dist | ||
test/**/**/dist | ||
test/**/**/**/dist | ||
test/**/stats.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
const { run } = require('../utils/test-utils'); | ||
const { stat, readFile } = require('fs'); | ||
const { resolve } = require('path'); | ||
|
||
describe('errors', () => { | ||
it('should output by default', () => { | ||
const { stdout, exitCode } = run(__dirname); | ||
|
||
expect(stdout).toMatch(/ERROR in/); | ||
expect(stdout).toMatch(/Error: Can't resolve/); | ||
expect(exitCode).toBe(1); | ||
}); | ||
|
||
it('should output JSON with the "json" flag', () => { | ||
const { stdout, exitCode } = run(__dirname, ['--json']); | ||
|
||
expect(() => JSON.parse(stdout)).not.toThrow(); | ||
expect(exitCode).toBe(1); | ||
|
||
const json = JSON.parse(stdout); | ||
|
||
expect(json['hash']).toBeDefined(); | ||
expect(json['errors']).toHaveLength(1); | ||
// `message` for `webpack@5` | ||
expect(json['errors'][0].message ? json['errors'][0].message : json['errors'][0]).toMatch(/Can't resolve/); | ||
}); | ||
|
||
it('should store json to a file', (done) => { | ||
const { stdout, exitCode } = run(__dirname, ['--json', 'stats.json']); | ||
|
||
expect(stdout).toContain('stats are successfully stored as json to stats.json'); | ||
expect(exitCode).toBe(1); | ||
|
||
stat(resolve(__dirname, './stats.json'), (err, stats) => { | ||
expect(err).toBe(null); | ||
expect(stats.isFile()).toBe(true); | ||
|
||
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['errors']).toHaveLength(1); | ||
// `message` for `webpack@5` | ||
expect(json['errors'][0].message ? json['errors'][0].message : json['errors'][0]).toMatch(/Can't resolve/); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import unknown from './unknown.mjs'; | ||
|
||
export default unknown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let obj; | ||
|
||
try { | ||
obj = require('unknown'); | ||
} catch (e) { | ||
// Ignore | ||
} | ||
|
||
export default obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
const { run } = require('../utils/test-utils'); | ||
const { stat, readFile } = require('fs'); | ||
const { resolve } = require('path'); | ||
|
||
describe('warnings', () => { | ||
it('should output by default', () => { | ||
const { stdout, exitCode } = run(__dirname); | ||
|
||
expect(stdout).toMatch(/WARNING in/); | ||
expect(stdout).toMatch(/Error: Can't resolve/); | ||
expect(exitCode).toBe(0); | ||
}); | ||
|
||
it('should output JSON with the "json" flag', () => { | ||
const { stdout, exitCode } = run(__dirname, ['--json']); | ||
|
||
expect(() => JSON.parse(stdout)).not.toThrow(); | ||
expect(exitCode).toBe(0); | ||
|
||
const json = JSON.parse(stdout); | ||
|
||
expect(json['hash']).toBeDefined(); | ||
expect(json['warnings']).toHaveLength(1); | ||
// `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 { stdout, exitCode } = run(__dirname, ['--json', 'stats.json']); | ||
|
||
expect(stdout).toContain('stats are successfully stored as json to stats.json'); | ||
expect(exitCode).toBe(0); | ||
|
||
stat(resolve(__dirname, './stats.json'), (err, stats) => { | ||
expect(err).toBe(null); | ||
expect(stats.isFile()).toBe(true); | ||
|
||
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(1); | ||
// `message` for `webpack@5` | ||
expect(json['warnings'][0].message ? json['warnings'][0].message : json['warnings'][0]).toMatch(/Can't resolve/); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters