Skip to content

Commit ef15cbc

Browse files
committed
Add tests for line numbers in stack traces
PR-URL: #102
1 parent 9a88bff commit ef15cbc

4 files changed

Lines changed: 53 additions & 0 deletions

File tree

test/examples/.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"parserOptions": {
33
"sourceType": "module"
4+
},
5+
"globals": {
6+
"unknownFunction": "readonly"
47
}
58
}

test/examples/referenceError.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
async () => {
2+
const result = unknownFunction();
3+
return result;
4+
};

test/examples/useStrict.cjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
async () => {
4+
const result = unknownFunction();
5+
return result;
6+
};

test/unit.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,46 @@ metatests.test('Syntax error', async (test) => {
168168
test.end();
169169
});
170170

171+
metatests.test('Reference error', async (test) => {
172+
const filePath = path.join(examples, 'referenceError.js');
173+
try {
174+
const script = await metavm.readScript(filePath);
175+
await script.exports();
176+
test.fail();
177+
} catch (err) {
178+
test.strictSame(err.constructor.name, 'ReferenceError');
179+
}
180+
test.end();
181+
});
182+
183+
metatests.test('Line number', async (test) => {
184+
{
185+
const filePath = path.join(examples, 'referenceError.js');
186+
try {
187+
const script = await metavm.readScript(filePath);
188+
await script.exports();
189+
test.fail();
190+
} catch (err) {
191+
const [, firstLine] = err.stack.split('\n');
192+
const [, lineNumber] = firstLine.split(':');
193+
test.strictSame(parseInt(lineNumber, 10), 2);
194+
}
195+
}
196+
{
197+
const filePath = path.join(examples, 'useStrict.cjs');
198+
try {
199+
const script = await metavm.readScript(filePath);
200+
await script.exports();
201+
test.fail();
202+
} catch (err) {
203+
const [, firstLine] = err.stack.split('\n');
204+
const [, lineNumber] = firstLine.split(':');
205+
test.strictSame(parseInt(lineNumber, 10), 4);
206+
}
207+
}
208+
test.end();
209+
});
210+
171211
metatests.test('Create default context', async (test) => {
172212
const context = metavm.createContext();
173213
test.strictSame(Object.keys(context), []);

0 commit comments

Comments
 (0)