Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions src/baseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,15 @@
}
}
flags.typescript = typescript;

let rootDir = path.dirname(getSourceDirname());
if (path.basename(rootDir) === 'dist') {
rootDir = path.dirname(rootDir);
}
// try app baseDir first on custom tscompiler
// then try to find tscompiler in @eggjs/bin/node_modules
const findPaths: string[] = [ flags.base, rootDir ];
this.isESM = pkg.type === 'module';
if (typescript) {
const findPaths: string[] = [ getSourceDirname() ];
if (flags.tscompiler) {
// try app baseDir first on custom tscompiler
// then try to find tscompiler in @eggjs/bin/node_modules
findPaths.unshift(flags.base);
}
flags.tscompiler = flags.tscompiler ?? 'ts-node/register';
const tsNodeRegister = importResolve(flags.tscompiler, {
paths: findPaths,
Expand All @@ -229,14 +229,15 @@
this.env.TS_NODE_FILES = process.env.TS_NODE_FILES ?? 'true';
// 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 ${importResolve('tsconfig-paths/register', {
// paths: [ getSourceDirname() ],
// })}`, ctx.env);
const tsConfigPathsRegister = importResolve('tsconfig-paths/register', {
paths: findPaths,
});
this.addNodeOptions(this.formatImportModule(tsConfigPathsRegister));
}
if (this.isESM) {
// use ts-node/esm loader on esm
let esmLoader = importResolve('ts-node/esm', {
paths: [ getSourceDirname() ],
paths: findPaths,
});
// ES Module loading with absolute path fails on windows
// https://github.com/nodejs/node/issues/31710#issuecomment-583916239
Expand All @@ -257,10 +258,7 @@
}
if (flags.declarations) {
const etsBin = importResolve('egg-ts-helper/dist/bin', {
paths: [
flags.base,
getSourceDirname(),
],
paths: findPaths,

Check warning on line 261 in src/baseCommand.ts

View check run for this annotation

Codecov / codecov/patch

src/baseCommand.ts#L261

Added line #L261 was not covered by tests
});
debug('run ets first: %o', etsBin);
await runScript(`node ${etsBin}`);
Expand Down
2 changes: 1 addition & 1 deletion test/commands/cov.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('test/commands/cov.test.ts', () => {
await coffee.fork(eggBin, [ 'cov' ], { cwd })
// .debug()
.expect('stdout', /should work/)
.expect('stdout', /1 passing/)
.expect('stdout', /3 passing/)
.expect('stdout', /Statements\s+: 100% \( \d+\/\d+ \)/)
.expect('code', 0)
.end();
Expand Down
4 changes: 2 additions & 2 deletions test/commands/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ describe('test/commands/test.test.ts', () => {
it('should success on ts', async () => {
const cwd = getFixtures('example-ts');
await coffee.fork(eggBin, [ 'test' ], { cwd })
// .debug()
.debug()
.expect('stdout', /should work/)
.expect('stdout', /1 passing/)
.expect('stdout', /3 passing \(/)
.expect('code', 0)
.end();
});
Expand Down
7 changes: 6 additions & 1 deletion test/fixtures/example-ts/app/controller/home.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Controller } from 'egg';

import { Foo } from '@/module/foo';
export default class HomeController extends Controller {
public async index() {
const obj: PlainObject = {};
obj.text = 'hi, egg';
this.ctx.body = obj.text;
}

async foo() {
const instance = new Foo();
this.ctx.body = instance.bar();
}
}
5 changes: 5 additions & 0 deletions test/fixtures/example-ts/app/module/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class Foo {
public bar() {
return 'bar';
}
}
6 changes: 6 additions & 0 deletions test/fixtures/example-ts/app/module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "foo",
"eggModule": {
"name": "foo"
}
}
1 change: 1 addition & 0 deletions test/fixtures/example-ts/app/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { Application } from 'egg';

export default (app: Application) => {
app.router.get('/', app.controller.home.index);
app.router.get('/foo', app.controller.home.foo);
};
2 changes: 1 addition & 1 deletion test/fixtures/example-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"typescript": true
},
"devDependencies": {
"@eggjs/mock": "beta"
"@eggjs/mock": "6"
}
}
18 changes: 16 additions & 2 deletions test/fixtures/example-ts/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
// @ts-ignore
import { strict as assert } from 'node:assert';
import { app } from '@eggjs/mock/bootstrap';
import { Foo } from '@/module/foo'

describe('test/index.test.ts', () => {
describe('example-ts/test/index.test.ts', () => {
it('should work', async () => {
await app.ready();
await app.httpRequest()
.get('/')
.expect('hi, egg')
.expect(200);
});

it('should paths work', async () => {
await app.ready();
await app.httpRequest()
.get('/foo')
.expect('bar')
.expect(200);
});

it('should auto import tsconfig-paths/register', async () => {
const instance = new Foo();
assert.equal(instance.bar(), 'bar');
});
});
12 changes: 11 additions & 1 deletion test/fixtures/example-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{
"extends": "@eggjs/tsconfig"
"extends": "@eggjs/tsconfig",
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"declaration": false,
"paths": {
"@/module/*": ["./app/module/*"]
},
"baseUrl": "."
}
}
Loading