From db07648432380066ba33543dd129e9ee7c951edf Mon Sep 17 00:00:00 2001 From: wanghx Date: Tue, 5 Jul 2022 18:16:05 +0800 Subject: [PATCH] fix: conflix source map support --- lib/cmd/test.js | 13 +++++-------- lib/espower-typescript.js | 4 ++-- .../example-ts-error-stack-mixed/app.ts | 9 +++++++++ .../config/config.default.ts | 7 +++++++ .../example-ts-error-stack-mixed/package.json | 6 ++++++ .../test/index.test.js | 16 ++++++++++++++++ .../tsconfig.json | 19 +++++++++++++++++++ test/ts.test.js | 18 ++++++++++++++++++ 8 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 test/fixtures/example-ts-error-stack-mixed/app.ts create mode 100644 test/fixtures/example-ts-error-stack-mixed/config/config.default.ts create mode 100644 test/fixtures/example-ts-error-stack-mixed/package.json create mode 100644 test/fixtures/example-ts-error-stack-mixed/test/index.test.js create mode 100644 test/fixtures/example-ts-error-stack-mixed/tsconfig.json diff --git a/lib/cmd/test.js b/lib/cmd/test.js index 4e479015..08ed397a 100644 --- a/lib/cmd/test.js +++ b/lib/cmd/test.js @@ -111,15 +111,12 @@ class TestCommand extends Command { if (requireArr.includes('intelli-espower-loader')) { console.warn('[egg-bin] don\'t need to manually require `intelli-espower-loader` anymore'); } else if (testArgv.espower) { - requireArr.push(require.resolve('intelli-espower-loader')); - } - - // for power-assert - if (testArgv.typescript && testArgv.espower) { - if (requireArr.includes(testArgv.tscompiler)) { - requireArr.splice(requireArr.indexOf(testArgv.tscompiler), 1); + if (testArgv.typescript) { + // espower-typescript must append after ts-node + requireArr.push(testArgv.tscompiler, require.resolve('../espower-typescript')); + } else { + requireArr.push(require.resolve('intelli-espower-loader')); } - requireArr.push(testArgv.tscompiler, require.resolve('../espower-typescript')); } testArgv.require = requireArr; diff --git a/lib/espower-typescript.js b/lib/espower-typescript.js index a50484e3..5515e622 100644 --- a/lib/espower-typescript.js +++ b/lib/espower-typescript.js @@ -8,8 +8,8 @@ const sourceCache = {}; const cwd = process.cwd(); espowerTypeScript({ - pattern: path.resolve(cwd, 'test/**/*.@(ts|tsx)'), - extensions: [ 'ts', 'tsx' ], + pattern: path.resolve(cwd, 'test/**/*.@(js|jsx|ts|tsx)'), + extensions: [ 'js', 'jsx', 'ts', 'tsx' ], }); function espowerTypeScript(options) { diff --git a/test/fixtures/example-ts-error-stack-mixed/app.ts b/test/fixtures/example-ts-error-stack-mixed/app.ts new file mode 100644 index 00000000..e1a8cc29 --- /dev/null +++ b/test/fixtures/example-ts-error-stack-mixed/app.ts @@ -0,0 +1,9 @@ +export default function() { + // placeholder comments + // placeholder comments + // placeholder comments + // placeholder comments + if (process.env.THROW_ERROR === 'true') { + throw new Error('throw error'); + } +} \ No newline at end of file diff --git a/test/fixtures/example-ts-error-stack-mixed/config/config.default.ts b/test/fixtures/example-ts-error-stack-mixed/config/config.default.ts new file mode 100644 index 00000000..106950bc --- /dev/null +++ b/test/fixtures/example-ts-error-stack-mixed/config/config.default.ts @@ -0,0 +1,7 @@ +'use strict'; + +export default () => { + const config = {} as any; + config.keys = '123456'; + return config; +}; diff --git a/test/fixtures/example-ts-error-stack-mixed/package.json b/test/fixtures/example-ts-error-stack-mixed/package.json new file mode 100644 index 00000000..fdbe7648 --- /dev/null +++ b/test/fixtures/example-ts-error-stack-mixed/package.json @@ -0,0 +1,6 @@ +{ + "name": "example-ts-error-stack", + "egg": { + "typescript": true + } +} \ No newline at end of file diff --git a/test/fixtures/example-ts-error-stack-mixed/test/index.test.js b/test/fixtures/example-ts-error-stack-mixed/test/index.test.js new file mode 100644 index 00000000..8bc5ae3e --- /dev/null +++ b/test/fixtures/example-ts-error-stack-mixed/test/index.test.js @@ -0,0 +1,16 @@ +'use strict'; + +const assert = require('assert'); + +describe('test/index.test.ts', () => { + // placeholder comments + it('should throw error', async () => { + throw new Error('error'); + }); + + // placeholder comments + it('should assert', async () => { + const obj = { key: '111' }; + assert(obj.key === '222'); + }); +}); diff --git a/test/fixtures/example-ts-error-stack-mixed/tsconfig.json b/test/fixtures/example-ts-error-stack-mixed/tsconfig.json new file mode 100644 index 00000000..4f10abf7 --- /dev/null +++ b/test/fixtures/example-ts-error-stack-mixed/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "target": "es2017", + "module": "commonjs", + "strict": true, + "noImplicitAny": false, + "moduleResolution": "node", + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "pretty": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true, + "skipLibCheck": true, + "skipDefaultLibCheck": true, + "inlineSourceMap": true, + "importHelpers": true + }, +} \ No newline at end of file diff --git a/test/ts.test.js b/test/ts.test.js index 6bbd0fef..f0dc3d66 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -160,6 +160,24 @@ describe('test/ts.test.js', () => { .expect('stdout', /Object\{key:"111"}/) .end(); }); + + it('should correct error stack line number in mixed app', () => { + if (process.platform === 'win32') return; + + const appDir = path.join(__dirname, './fixtures/example-ts-error-stack-mixed'); + const testFile = path.resolve(appDir, 'test/index.test.js'); + return coffee.fork(eggBin, [ 'test', testFile ], { cwd: appDir }) + // .debug() + .expect('stdout', /error/) + .expect('stdout', /test[\/\\]{1}index\.test\.js:8:11\)/) + .expect('stdout', /test[\/\\]{1}index\.test\.js: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(); + }); }); describe('egg.typescript = true', () => {