From a12dc1a7277a99fe842bdec2861b58a61769f9e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=96=E7=8C=A9?= Date: Mon, 11 Jul 2022 08:49:07 +0800 Subject: [PATCH] fix: conflict source map support (#182) Co-authored-by: wanghx --- .github/workflows/nodejs.yml | 6 +++--- 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/fixtures/test-files-c8/.DS_Store | Bin 0 -> 6148 bytes test/fixtures/test-files-c8/test/a.test.js | 1 + test/ts.test.js | 18 +++++++++++++++++ 11 files changed, 86 insertions(+), 13 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 create mode 100644 test/fixtures/test-files-c8/.DS_Store diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 229ccbff..ea2dd67a 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -6,10 +6,10 @@ name: Node.js CI on: push: branches: - - master + - 4.x pull_request: branches: - - master + - 4.x schedule: - cron: '0 2 * * *' @@ -20,7 +20,7 @@ jobs: strategy: fail-fast: false matrix: - node-version: [8, 10, 12, 14, 16] + node-version: [14, 16] os: [ubuntu-latest, windows-latest, macos-latest] steps: diff --git a/lib/cmd/test.js b/lib/cmd/test.js index 889a2970..d581c48c 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/fixtures/test-files-c8/.DS_Store b/test/fixtures/test-files-c8/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1ad899dbcdc748036dd701eacb0ec11e243703d4 GIT binary patch literal 6148 zcmeHKyJ`bL3>+mc4$`I(2Sbbi#2M3J zT*oXyY@Q(Y!ZDE|1kgGlC+ZoQs7@HV6)|Fx!@~RZ=JlH_u58Z)4k@C?#6Xc7@{2$qaAbO f?f5y0vab1>=e=-D3_9~cC+cUwb&*MdzgFN1-fR_w literal 0 HcmV?d00001 diff --git a/test/fixtures/test-files-c8/test/a.test.js b/test/fixtures/test-files-c8/test/a.test.js index d1829307..170fb456 100644 --- a/test/fixtures/test-files-c8/test/a.test.js +++ b/test/fixtures/test-files-c8/test/a.test.js @@ -6,6 +6,7 @@ const a = require('../lib/a'); describe('a.test.js', () => { it('should success', () => { a(true); + a(false); }); it('should show tmp', () => { diff --git a/test/ts.test.js b/test/ts.test.js index 3714a859..43e20a0e 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -149,6 +149,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', () => {