diff --git a/lib/cmd/test.js b/lib/cmd/test.js index 47e9f8aa..3dfef01a 100644 --- a/lib/cmd/test.js +++ b/lib/cmd/test.js @@ -68,19 +68,6 @@ class TestCommand extends Command { yield this.helper.forkNode(mochaFile, testArgs, opt); } - get context() { - const context = super.context; - const { argv, execArgvObj } = context; - - // remove ts-node, ts-node and espower-typescript can't coexist - // because espower-typescript@9 has already register ts-node - if (argv.typescript) { - execArgvObj.require.splice(execArgvObj.require.indexOf(require.resolve('ts-node/register')), 1); - } - - return context; - } - /** * format test args then change it to array style * @param {Object} context - { cwd, argv, ...} @@ -123,8 +110,9 @@ class TestCommand extends Command { // for power-assert if (testArgv.typescript) { - // remove ts-node in context getter on top. - requireArr.push(require.resolve('espower-typescript/guess')); + const tscompilerPath = require.resolve(testArgv.tscompiler); + requireArr.splice(requireArr.indexOf(tscompilerPath), 1); + requireArr.push(tscompilerPath, require.resolve('../espower-typescript')); } testArgv.require = requireArr; diff --git a/lib/espower-typescript.js b/lib/espower-typescript.js new file mode 100644 index 00000000..a50484e3 --- /dev/null +++ b/lib/espower-typescript.js @@ -0,0 +1,41 @@ +'use strict'; + +const path = require('path'); +const espowerSource = require('espower-source'); +const minimatch = require('minimatch'); +const sourceMapSupport = require('source-map-support'); +const sourceCache = {}; +const cwd = process.cwd(); + +espowerTypeScript({ + pattern: path.resolve(cwd, 'test/**/*.@(ts|tsx)'), + extensions: [ 'ts', 'tsx' ], +}); + +function espowerTypeScript(options) { + // install source-map-support again to correct the source-map + sourceMapSupport.install({ + environment: 'node', + retrieveFile: p => sourceCache[p], + }); + + options.extensions.forEach(ext => { + espowerTsRegister(`.${ext}`, options); + }); +} + +function espowerTsRegister(ext, options) { + const originalExtension = require.extensions[ext]; + require.extensions[ext] = (module, filepath) => { + if (!minimatch(filepath, options.pattern)) { + return originalExtension(module, filepath); + } + const originalCompile = module._compile; + module._compile = function(code, filepath) { + const newSource = espowerSource(code, filepath, options); + sourceCache[filepath] = newSource; + return originalCompile.call(this, newSource, filepath); + }; + return originalExtension(module, filepath); + }; +} diff --git a/package.json b/package.json index b54ac130..6f63ac55 100644 --- a/package.json +++ b/package.json @@ -17,16 +17,18 @@ "detect-port": "^1.3.0", "egg-ts-helper": "^1.25.2", "egg-utils": "^2.4.1", - "espower-typescript": "9.0.1", + "espower-source": "^2.3.0", "globby": "^9.2.0", "inspector-proxy": "^1.2.1", "intelli-espower-loader": "^1.0.1", "jest-changed-files": "^24.7.0", + "minimatch": "^3.0.4", "mocha": "^6.0.2", "mz-modules": "^2.1.0", "nyc": "^13.3.0", "power-assert": "^1.6.1", "semver": "^6.0.0", + "source-map-support": "^0.5.19", "test-exclude": "^5.1.0", "ts-node": "^7", "ypkgfiles": "^1.6.0" diff --git a/test/fixtures/example-ts-error-stack/test/index.test.ts b/test/fixtures/example-ts-error-stack/test/index.test.ts index d77166d3..77d6899c 100644 --- a/test/fixtures/example-ts-error-stack/test/index.test.ts +++ b/test/fixtures/example-ts-error-stack/test/index.test.ts @@ -1,6 +1,6 @@ 'use strict'; -// import * as assert from 'assert'; +import assert = require('assert'); describe('test/index.test.ts', () => { // placeholder comments diff --git a/test/ts.test.js b/test/ts.test.js index 5469a812..b7e59fec 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -110,6 +110,25 @@ describe('test/ts.test.js', () => { .expect('stdout', /error/) .expect('stdout', /test\/index\.test\.ts:8:11\)/) .expect('stdout', /test\/index\.test\.ts:14:5\)/) + .expect('stdout', /assert\(obj\.key === '222'\)/) + .expect('stdout', /| {3}| {3}|/) + .expect('stdout', /| {3}| {3}false/) + .expect('stdout', /| {3}"111"/) + .expect('stdout', /Object\{key:"111"}/) + .end(); + }); + + it('should correct error stack line number in testing app with other tscompiler', () => { + return coffee.fork(eggBin, [ 'test', '--tscompiler=esbuild-register' ], { cwd }) + .debug() + .expect('stdout', /error/) + .expect('stdout', /test\/index\.test\.ts:8:11\)/) + .expect('stdout', /test\/index\.test\.ts:14:5\)/) + .expect('stdout', /assert\(obj\.key === "222"\)/) + .expect('stdout', /| {3}| {3}|/) + .expect('stdout', /| {3}| {3}false/) + .expect('stdout', /| {3}"111"/) + .expect('stdout', /Object\{key:"111"}/) .end(); }); @@ -119,6 +138,11 @@ describe('test/ts.test.js', () => { .expect('stdout', /error/) .expect('stdout', /test\/index\.test\.ts:8:11\)/) .expect('stdout', /test\/index\.test\.ts:14:5\)/) + .expect('stdout', /assert\(obj\.key === '222'\)/) + .expect('stdout', /| {3}| {3}|/) + .expect('stdout', /| {3}| {3}false/) + .expect('stdout', /| {3}"111"/) + .expect('stdout', /Object\{key:"111"}/) .end(); }); });