Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auto import tsconfig-paths/register.js #282

Merged
merged 3 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions src/baseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,10 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
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.js', {
paths: [ getSourceDirname() ],
});
this.addNodeOptions(this.formatImportModule(tsConfigPathsRegister));
}
if (this.isESM) {
// use ts-node/esm loader on esm
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