Skip to content

Commit

Permalink
feat: support run test/cov on esm package (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Sep 15, 2023
1 parent 6bb3dbb commit 6ae1044
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion src/middleware/global_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions test/cmd/cov.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
12 changes: 12 additions & 0 deletions test/cmd/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/mocha-test-ts-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "module",
"devDependencies": {
"typescript": "*"
}
}
7 changes: 7 additions & 0 deletions test/fixtures/mocha-test-ts-esm/test/bar.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import assert from 'assert';

describe('mocha-test-ts-esm/bar.test.ts', () => {
it('should work', () => {
assert(true);
});
});
7 changes: 7 additions & 0 deletions test/fixtures/mocha-test-ts-esm/test/foo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import assert from 'assert';

describe('mocha-test-ts-esm/foo.test.ts', () => {
it('should work', () => {
assert(true);
});
});
11 changes: 11 additions & 0 deletions test/fixtures/mocha-test-ts-esm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@eggjs/tsconfig",
"compileOnSave": true,
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
}
}

1 comment on commit 6ae1044

@vercel
Copy link

@vercel vercel bot commented on 6ae1044 Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

egg-bin – ./

egg-bin-no-veronica.vercel.app
egg-bin-git-master-no-veronica.vercel.app
egg-bin.vercel.app

Please sign in to comment.