1 | /**
|
2 | * Only works in test/cov command with `espower-typescript`.
|
3 | *
|
4 | * Fix bug that error stack gives incorrect line number and column number in unit testing.
|
5 | *
|
6 | * ### Reason
|
7 | *
|
8 | * `espower-typescript` combines the sourceMap of `ts-node` and `power-assert` and return a new sourceMap
|
9 | * for supporting `power-assert`. But `source-map-support` receives old sourceMap when handling the code because
|
10 | * `ts-node` cache its sourceMap in `retrieveFile` method: https://github.com/TypeStrong/ts-node/blob/master/src/index.ts#L218
|
11 | *
|
12 | * ```typescript
|
13 | * // Install source map support and read from memory cache.
|
14 | * sourceMapSupport.install({
|
15 | * environment: 'node',
|
16 | * retrieveFile: function (path) {
|
17 | * return memoryCache.outputs[path];
|
18 | * }
|
19 | * });
|
20 | * ```
|
21 | *
|
22 | * ### Solution
|
23 | *
|
24 | * Overwriting the `retrieveFile` method of `source-map-support` in `egg-bin test` to return a correct sourceMap for code.
|
25 | *
|
26 | *
|
27 | * https://github.com/eggjs/egg-bin/pull/107
|
28 | * https://github.com/power-assert-js/espower-typescript/issues/54
|
29 | *
|
30 | */
|
31 |
|
32 | ;
|
33 |
|
34 | const sourceMapSupport = require('source-map-support');
|
35 | const cacheMap = {};
|
36 | const extensions = [ '.ts', '.tsx' ];
|
37 |
|
38 | sourceMapSupport.install({
|
39 | environment: 'node',
|
40 | retrieveFile(path) {
|
41 | return cacheMap[path];
|
42 | },
|
43 | });
|
44 |
|
45 | for (const ext of extensions) {
|
46 | const originalExtension = require.extensions[ext];
|
47 | require.extensions[ext] = (module, filePath) => {
|
48 | const originalCompile = module._compile;
|
49 | module._compile = function(code, filePath) {
|
50 | cacheMap[filePath] = code;
|
51 | return originalCompile.call(this, code, filePath);
|
52 | };
|
53 | return originalExtension(module, filePath);
|
54 | };
|
55 | }
|