Skip to content

Commit

Permalink
Improve tests: get error line and position
Browse files Browse the repository at this point in the history
PR-URL: #115
  • Loading branch information
tshemsedinov committed Oct 15, 2023
1 parent 30d533a commit 02efc2d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
9 changes: 9 additions & 0 deletions examples/simpleUndef.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
({
field: 'value',

add(a, b) {
return a.unknown() + b.unknown();
},

sub: (a, b) => a.unknown() - b.unknown(),
});
21 changes: 18 additions & 3 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ test('Reference error', async () => {
}
});

test('Line number', async () => {
test('Line number and position', async () => {
{
const filePath = path.join(examples, 'referenceError.js');
try {
Expand All @@ -178,8 +178,9 @@ test('Line number', async () => {
test.fail();
} catch (err) {
const [, firstLine] = err.stack.split('\n');
const [, lineNumber] = firstLine.split(':');
const [, lineNumber, position] = firstLine.split(':');
assert.strictEqual(parseInt(lineNumber, 10), 2);
assert.strictEqual(parseInt(position, 10), 18);
}
}
{
Expand All @@ -190,8 +191,22 @@ test('Line number', async () => {
test.fail();
} catch (err) {
const [, firstLine] = err.stack.split('\n');
const [, lineNumber] = firstLine.split(':');
const [, lineNumber, position] = firstLine.split(':');
assert.strictEqual(parseInt(lineNumber, 10), 4);
assert.strictEqual(parseInt(position, 10), 18);
}
}
{
const filePath = path.join(examples, 'simpleUndef.js');
try {
const script = await metavm.readScript(filePath);
script.exports.add(5, 2);
test.fail();
} catch (err) {
const [, firstLine] = err.stack.split('\n');
const [, lineNumber, position] = firstLine.split(':');
assert.strictEqual(parseInt(lineNumber, 10), 5);
assert.strictEqual(parseInt(position, 10), 14);
}
}
});
Expand Down

0 comments on commit 02efc2d

Please sign in to comment.