Skip to content

Commit 4e31278

Browse files
committed
Add source map support
1 parent 14368e9 commit 4e31278

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,9 @@ Normally, to run Jest from `package.json`, we'd add a `"test": "jest"` line. Tha
9999
To pass parameters to Node when running Jest, we'll add the following `test` line:
100100

101101
"test": "node --harmony node_modules/.bin/jest"
102+
103+
104+
# Source maps
105+
106+
If your script generates an error, you'll see the line numbers from the generated `.js` files, which is not helpful. We want to see the original paths and line numbers from the `.ts` files. To do that, we'll add `sourceMap: true` to `tsconfig.json`, install [`source-map-support`](https://www.npmjs.com/package/source-map-support) and run node with the `-r source-map-support/register` parameter. Note that Jest already takes care of source mapping so you'll see the `.ts` line numbers without having to do anything extra.
107+

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"main": "run.js",
1717
"scripts": {
1818
"build": "rm *.js && tsc",
19-
"start": "node --experimental-specifier-resolution=node --harmony run.js",
19+
"start": "node --experimental-specifier-resolution=node --harmony -r source-map-support/register run.js",
2020
"lint": "eslint *.ts",
2121
"test": "node --harmony node_modules/.bin/jest"
2222
},
@@ -32,6 +32,7 @@
3232
"eslint": "^6.8.0",
3333
"eslint-plugin-jest": "^23.8.2",
3434
"jest": "^25.2.3",
35+
"source-map-support": "^0.5.16",
3536
"typescript": "^3.8.3"
3637
}
3738
}

run.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,12 @@ const influx = new Influx.InfluxDB();
1010

1111
const l = new Lib();
1212
l.run();
13-
console.log('Should display "object":', typeof influx);
13+
14+
try {
15+
throw new Error('testing source mapping');
16+
} catch (e) {
17+
if (e.stack.includes('.ts:')) {
18+
console.info('Stack trace mapped back to TypeScript should indicate correct error line number');
19+
console.info(e);
20+
}
21+
}

test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@ test('constructor', () => {
55
const instance = new Lib();
66
expect(instance instanceof Lib).toBeTruthy();
77
});
8+
9+
try {
10+
throw new Error('testing source mapping in Jest');
11+
} catch (e) {
12+
if (e.stack.includes('.ts:')) {
13+
console.info('Stack trace mapped back to TypeScript should indicate correct error line number');
14+
console.info(e);
15+
}
16+
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//"module": "CommonJS", // enables `import { InfluxDB } from 'influx'
66
"allowSyntheticDefaultImports": true, // allow default imports from modules with no default export. This does not affect code emit, just typechecking.
77
"moduleResolution": "node", // find modules in node_modules - https://github.com/Microsoft/TypeScript/issues/8189#issuecomment-212079251
8-
"types": ["node", "jest"] // Influx uses built-in node modules like http or url
8+
"types": ["node", "jest"], // Influx uses built-in node modules like http or url
9+
"sourceMap": true
910
}
1011
}

0 commit comments

Comments
 (0)