Skip to content

Commit

Permalink
fix(core): default baseUrl value to '.' when registering ts-node (#4680)
Browse files Browse the repository at this point in the history
Fixes #4679. See the linked issue for the full context of this PR.
  • Loading branch information
ianyong committed Sep 7, 2023
1 parent b0ecd21 commit cc0fc5f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/utils/ConfigurationLoader.ts
Expand Up @@ -142,7 +142,7 @@ export class ConfigurationLoader {

if (Object.entries(options?.paths ?? {}).length > 0) {
Utils.requireFrom('tsconfig-paths', tsConfigPath).register({
baseUrl: options.baseUrl,
baseUrl: options.baseUrl ?? '.',
paths: options.paths,
});
}
Expand Down
47 changes: 47 additions & 0 deletions tests/features/cli/CLIHelper.test.ts
Expand Up @@ -16,6 +16,9 @@ jest.mock(process.cwd() + '/tsconfig.extended-abs.json', () => tscExtendedAbs, {
const tscExtended = { extends: './tsconfig.extended-abs.json', compilerOptions: { module: 'commonjs' } } as any;
jest.mock(process.cwd() + '/tsconfig.extended.json', () => tscExtended, { virtual: true });

const tscWithoutBaseUrl = { compilerOptions: { paths: { '@some-path/some': './libs/paths' } } };
jest.mock(process.cwd() + '/tsconfig.without-baseurl.json', () => tscWithoutBaseUrl, { virtual: true });

const tsc = { compilerOptions: { } } as any;
jest.mock(process.cwd() + '/tsconfig.json', () => tsc, { virtual: true });

Expand Down Expand Up @@ -126,6 +129,50 @@ describe('CLIHelper', () => {
requireFromMock.mockRestore();
});

test('configures yargs instance [ts-node and ts-paths] without baseUrl', async () => {
const pathExistsMock = jest.spyOn(require('fs-extra'), 'pathExists');
pathExistsMock.mockResolvedValue(true);
pkg['mikro-orm'].useTsNode = true;
pkg['mikro-orm'].tsConfigPath = './tsconfig.without-baseurl.json';
delete tsc.compilerOptions.baseUrl;
const requireFromMock = jest.spyOn(Utils, 'requireFrom');
const registerMock = jest.fn();
const registerPathsMock = jest.fn();
registerMock.mockImplementation(() => {
return {
config: {
options: {
...tscWithoutBaseUrl.compilerOptions,
},
},
};
});
requireFromMock.mockImplementation(id => {
if (id === 'ts-node') {
return { register: registerMock };
}

if (id === 'tsconfig-paths') {
return { register: registerPathsMock };
}

return {};
});
const cli = await CLIConfigurator.configure() as any;
expect(cli.$0).toBe('mikro-orm');
expect(requireFromMock).toHaveBeenCalledTimes(2);
expect(requireFromMock).toHaveBeenCalledWith('ts-node', process.cwd() + '/tsconfig.without-baseurl.json');
expect(requireFromMock).toHaveBeenCalledWith('tsconfig-paths', process.cwd() + '/tsconfig.without-baseurl.json');
expect(registerPathsMock).toHaveBeenCalledWith({
baseUrl: '.',
paths: { '@some-path/some': './libs/paths' },
});
pathExistsMock.mockRestore();
requireFromMock.mockRestore();
delete pkg['mikro-orm'].useTsNode;
delete pkg['mikro-orm'].tsConfigPath;
});

test('gets ORM configuration [no mikro-orm.config]', async () => {
delete process.env.MIKRO_ORM_ALLOW_GLOBAL_CONTEXT;
await expect(CLIHelper.getConfiguration()).rejects.toThrowError(`MikroORM config file not found in ['./src/mikro-orm.config.js', './mikro-orm.config.js']`);
Expand Down

0 comments on commit cc0fc5f

Please sign in to comment.