Skip to content

Commit 88f9a39

Browse files
TrottMylesBorins
authored andcommitted
test: isolate unusual assert test in its own file
test-assert.js contains a test that writes to the source tree, requires an internal module, and depends on modules not needed by the plethora of other test cases in the file. Move it to its own file so that there are not side effects in test-assert.js and so that it can be refactored to not write to the source tree. PR-URL: #20861 Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 460a502 commit 88f9a39

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Flags: --expose-internals
2+
3+
'use strict';
4+
5+
require('../common');
6+
7+
const assert = require('assert');
8+
const EventEmitter = require('events');
9+
const { errorCache } = require('internal/assert');
10+
const { writeFileSync, unlinkSync } = require('fs');
11+
12+
// Do not read filesystem for error messages in builtin modules.
13+
{
14+
const e = new EventEmitter();
15+
16+
e.on('hello', assert);
17+
18+
let threw = false;
19+
try {
20+
e.emit('hello', false);
21+
} catch (err) {
22+
const frames = err.stack.split('\n');
23+
const [, filename, line, column] = frames[1].match(/\((.+):(\d+):(\d+)\)/);
24+
// Reset the cache to check again
25+
const size = errorCache.size;
26+
errorCache.delete(`${filename}${line - 1}${column - 1}`);
27+
assert.strictEqual(errorCache.size, size - 1);
28+
const data = `${'\n'.repeat(line - 1)}${' '.repeat(column - 1)}` +
29+
'ok(failed(badly));';
30+
try {
31+
writeFileSync(filename, data);
32+
assert.throws(
33+
() => e.emit('hello', false),
34+
{
35+
message: 'false == true'
36+
}
37+
);
38+
threw = true;
39+
} finally {
40+
unlinkSync(filename);
41+
}
42+
}
43+
assert(threw);
44+
}

test/parallel/test-assert.js

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,12 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
// Flags: --expose-internals
23-
2422
'use strict';
2523

2624
/* eslint-disable node-core/prefer-common-expectserror */
2725

2826
const common = require('../common');
2927
const assert = require('assert');
30-
const EventEmitter = require('events');
31-
const { errorCache } = require('internal/assert');
32-
const { writeFileSync, unlinkSync } = require('fs');
3328
const { inspect } = require('util');
3429
const a = assert;
3530

@@ -793,40 +788,6 @@ common.expectsError(
793788
}
794789
);
795790

796-
// Do not try to check Node.js modules.
797-
{
798-
const e = new EventEmitter();
799-
800-
e.on('hello', assert);
801-
802-
let threw = false;
803-
try {
804-
e.emit('hello', false);
805-
} catch (err) {
806-
const frames = err.stack.split('\n');
807-
const [, filename, line, column] = frames[1].match(/\((.+):(\d+):(\d+)\)/);
808-
// Reset the cache to check again
809-
const size = errorCache.size;
810-
errorCache.delete(`${filename}${line - 1}${column - 1}`);
811-
assert.strictEqual(errorCache.size, size - 1);
812-
const data = `${'\n'.repeat(line - 1)}${' '.repeat(column - 1)}` +
813-
'ok(failed(badly));';
814-
try {
815-
writeFileSync(filename, data);
816-
assert.throws(
817-
() => e.emit('hello', false),
818-
{
819-
message: 'false == true'
820-
}
821-
);
822-
threw = true;
823-
} finally {
824-
unlinkSync(filename);
825-
}
826-
}
827-
assert(threw);
828-
}
829-
830791
common.expectsError(
831792
// eslint-disable-next-line no-restricted-syntax
832793
() => assert.throws(() => {}, 'Error message', 'message'),

0 commit comments

Comments
 (0)