MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1const { spawnSync } = require('child_process');
2
3function assertIncludes(haystack, needle, label) {
4 if (!haystack.includes(needle)) {
5 throw new Error(`${label}: expected ${JSON.stringify(haystack)} to include ${JSON.stringify(needle)}`);
6 }
7}
8
9function assertNotIncludes(haystack, needle, label) {
10 if (haystack.includes(needle)) {
11 throw new Error(`${label}: expected ${JSON.stringify(haystack)} not to include ${JSON.stringify(needle)}`);
12 }
13}
14
15function assertMatch(haystack, pattern, label) {
16 if (!pattern.test(haystack)) {
17 throw new Error(`${label}: expected ${JSON.stringify(haystack)} to match ${pattern}`);
18 }
19}
20
21function run(source) {
22 const result = spawnSync(process.execPath, ['-e', source]);
23 if (result.error) throw result.error;
24 if (result.status !== 0) {
25 throw new Error(`child exited ${result.status}\nstdout:\n${result.stdout}\nstderr:\n${result.stderr}`);
26 }
27 return {
28 stdout: String(result.stdout),
29 stderr: String(result.stderr),
30 };
31}
32
33const withDetail = run(`
34 process.emitWarning('Something happened!', {
35 code: 'Custom_Warning',
36 detail: 'Additional information about warning'
37 });
38`);
39
40assertMatch(
41 withDetail.stderr,
42 /\\(ant:\\d+\\) \\[Custom_Warning\\] Warning: Something happened!\\n/,
43 'emitWarning formats code, type, and message'
44);
45assertIncludes(
46 withDetail.stderr,
47 'Additional information about warning\n',
48 'emitWarning prints detail'
49);
50assertNotIncludes(
51 withDetail.stderr,
52 '[Custom_Warning] : Something happened!',
53 'emitWarning avoids the old empty type formatting'
54);
55
56const listenerPayload = run(`
57 process.on('warning', warning => {
58 console.log([warning.name, warning.message, warning.code, warning.detail].join('|'));
59 });
60 process.emitWarning('msg', { code: 'C', detail: 'd' });
61`);
62
63assertIncludes(listenerPayload.stderr, '[C] Warning: msg\n', 'emitWarning still writes default warning output');
64assertIncludes(listenerPayload.stdout, 'Warning|msg|C|d\n', 'emitWarning emits a warning object');
65
66console.log('process.emitWarning formatting ok');