From 6ae10444af01cccfac19b2e4210f7fb06382e728 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 16 Sep 2023 00:00:52 +0800 Subject: [PATCH] feat: support run test/cov on esm package (#239) --- package.json | 4 ++-- src/middleware/global_options.ts | 7 ++++++- test/cmd/cov.test.ts | 12 ++++++++++++ test/cmd/test.test.ts | 12 ++++++++++++ test/fixtures/mocha-test-ts-esm/package.json | 6 ++++++ test/fixtures/mocha-test-ts-esm/test/bar.test.ts | 7 +++++++ test/fixtures/mocha-test-ts-esm/test/foo.test.ts | 7 +++++++ test/fixtures/mocha-test-ts-esm/tsconfig.json | 11 +++++++++++ 8 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/mocha-test-ts-esm/package.json create mode 100644 test/fixtures/mocha-test-ts-esm/test/bar.test.ts create mode 100644 test/fixtures/mocha-test-ts-esm/test/foo.test.ts create mode 100644 test/fixtures/mocha-test-ts-esm/tsconfig.json diff --git a/package.json b/package.json index 5ab90f56..22d1aa78 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "@swc-node/register": "^1.6.1", "@swc/core": "^1.3.35", "@types/mocha": "^10.0.1", - "@types/node": "^18.11.19", + "@types/node": "^20.6.1", "assert-file": "^1.0.0", "coffee": "^5.4.0", "cpy": "^8.1.2", @@ -54,7 +54,7 @@ "eslint-config-egg": "^12.0.0", "git-contributor": "2", "npminstall": "^7.5.0", - "typescript": "^4.9.5" + "typescript": "^5.2.2" }, "repository": { "type": "git", diff --git a/src/middleware/global_options.ts b/src/middleware/global_options.ts index 896c71d5..26a16d65 100644 --- a/src/middleware/global_options.ts +++ b/src/middleware/global_options.ts @@ -101,8 +101,13 @@ export default class implements ApplicationLifecycle { // keep same logic with egg-core, test cmd load files need it // see https://github.com/eggjs/egg-core/blob/master/lib/loader/egg_loader.js#L49 addNodeOptionsToEnv(`--require ${require.resolve('tsconfig-paths/register')}`, ctx.env); - debug('set NODE_OPTIONS: %o', ctx.env.NODE_OPTIONS); } + if (pkg.type === 'module') { + // use ts-node/esm loader on esm + addNodeOptionsToEnv('--loader ts-node/esm', ctx.env); + } + + debug('set NODE_OPTIONS: %o', ctx.env.NODE_OPTIONS); debug('ctx.args: %o', ctx.args); debug('enter next'); await next(); diff --git a/test/cmd/cov.test.ts b/test/cmd/cov.test.ts index 7fad3c48..b36ee428 100644 --- a/test/cmd/cov.test.ts +++ b/test/cmd/cov.test.ts @@ -168,5 +168,17 @@ describe('test/cmd/cov.test.ts', () => { .expect('code', 0) .end(); }); + + it('should run cov on ts-esm module', () => { + const cwd = path.join(fixtures, 'mocha-test-ts-esm'); + return coffee.fork(eggBin, [ 'cov' ], { + cwd, + }) + .debug() + .expect('stdout', /should work/) + .expect('stdout', /2 passing/) + .expect('code', 0) + .end(); + }); }); }); diff --git a/test/cmd/test.test.ts b/test/cmd/test.test.ts index b4626aef..f282ddca 100644 --- a/test/cmd/test.test.ts +++ b/test/cmd/test.test.ts @@ -168,6 +168,18 @@ describe('test/cmd/test.test.ts', () => { .end(); }); + it('should run test on ts-esm module', () => { + const cwd = path.join(fixtures, 'mocha-test-ts-esm'); + return coffee.fork(eggBin, [ 'test' ], { + cwd, + }) + .debug() + .expect('stdout', /should work/) + .expect('stdout', /2 passing/) + .expect('code', 0) + .end(); + }); + it('should success js', () => { return coffee.fork(eggBin, [ 'test' ], { cwd: path.join(fixtures, 'test-unhandled-rejection') }) // .debug() diff --git a/test/fixtures/mocha-test-ts-esm/package.json b/test/fixtures/mocha-test-ts-esm/package.json new file mode 100644 index 00000000..bfef359b --- /dev/null +++ b/test/fixtures/mocha-test-ts-esm/package.json @@ -0,0 +1,6 @@ +{ + "type": "module", + "devDependencies": { + "typescript": "*" + } +} diff --git a/test/fixtures/mocha-test-ts-esm/test/bar.test.ts b/test/fixtures/mocha-test-ts-esm/test/bar.test.ts new file mode 100644 index 00000000..f6b1903a --- /dev/null +++ b/test/fixtures/mocha-test-ts-esm/test/bar.test.ts @@ -0,0 +1,7 @@ +import assert from 'assert'; + +describe('mocha-test-ts-esm/bar.test.ts', () => { + it('should work', () => { + assert(true); + }); +}); diff --git a/test/fixtures/mocha-test-ts-esm/test/foo.test.ts b/test/fixtures/mocha-test-ts-esm/test/foo.test.ts new file mode 100644 index 00000000..5584dee2 --- /dev/null +++ b/test/fixtures/mocha-test-ts-esm/test/foo.test.ts @@ -0,0 +1,7 @@ +import assert from 'assert'; + +describe('mocha-test-ts-esm/foo.test.ts', () => { + it('should work', () => { + assert(true); + }); +}); diff --git a/test/fixtures/mocha-test-ts-esm/tsconfig.json b/test/fixtures/mocha-test-ts-esm/tsconfig.json new file mode 100644 index 00000000..298cdd25 --- /dev/null +++ b/test/fixtures/mocha-test-ts-esm/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "@eggjs/tsconfig", + "compileOnSave": true, + "compilerOptions": { + "strict": true, + "noImplicitAny": true, + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext", + } +}