Skip to content

Commit 3429958

Browse files
whxaxesteppeis
authored andcommitted
fix: correct the error stacks (#55)
1 parent d476175 commit 3429958

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/node_modules
22
test/test-outdir/**/*.js
3+
yarn.lock
4+
*.log

index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@
55
const path = require('path');
66
const espowerSource = require('espower-source');
77
const minimatch = require('minimatch');
8+
const sourceMapSupport = require('source-map-support');
89
const tsNodeRegister = require('ts-node').register;
10+
const sourceCache = {};
911

1012
function espowerTypeScript(options, tsNodeOptions) {
1113
tsNodeRegister(tsNodeOptions);
14+
15+
// install source-map-support again to correct the source-map
16+
sourceMapSupport.install({
17+
environment: 'node',
18+
retrieveFile: path => sourceCache[path],
19+
});
20+
1221
const {extensions = ['ts', 'tsx']} = options;
1322
extensions.forEach(ext => {
1423
espowerTsRegister(`.${ext}`, options);
@@ -26,7 +35,9 @@ function espowerTsRegister(ext, options) {
2635
}
2736
const originalCompile = module._compile;
2837
module._compile = function(code, filepath) {
29-
return originalCompile.call(this, espowerSource(code, filepath, options), filepath);
38+
const newSource = espowerSource(code, filepath, options);
39+
sourceCache[filepath] = newSource;
40+
return originalCompile.call(this, newSource, filepath);
3041
};
3142
return originalExtension(module, filepath);
3243
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"dependencies": {
2323
"espower-source": "^2.3.0",
2424
"minimatch": "^3.0.3",
25+
"source-map-support": "^0.5.9",
2526
"ts-node": "^7.0.0"
2627
},
2728
"devDependencies": {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import assert = require('assert');
2+
3+
export default function expectErrorStacksCorrect(line: number, column: number) {
4+
try {
5+
assert.fail('AssertionError should be thrown');
6+
} catch(e) {
7+
const matches = e.stack.match(/expectErrorStacksCorrect\.ts:(\d+):(\d+)/);
8+
assert(!!matches);
9+
assert.equal(matches[1], line);
10+
assert.equal(matches[2], column);
11+
}
12+
}

test/to_be_instrumented_test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import assert = require('assert');
44
import expectPowerAssertMessage from './lib/expectPowerAssertMessage';
5+
import expectErrorStacksCorrect from './lib/expectErrorStacksCorrect';
56
import MyComponent from './lib/mycomponent';
67

78
describe('espower-typescript: ts', function() {
@@ -39,6 +40,10 @@ describe('espower-typescript: ts', function() {
3940
}, expected);
4041
});
4142

43+
it('error stack line number and column number should correct', function() {
44+
expectErrorStacksCorrect(5, 12);
45+
});
46+
4247
it('jsx:react', function() {
4348
let expected =
4449
` assert.equal(1, mycomponent_1.default())

0 commit comments

Comments
 (0)