Skip to content

Commit

Permalink
test: for ts path mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
jjangga0214 authored and mrjoelkemp committed Apr 30, 2022
1 parent e9cddf1 commit bc9913f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/root3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# root3

This is a simple example for typescript monorepo, using [path mapping](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping).
8 changes: 8 additions & 0 deletions test/root3/packages/bar/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { doubleNumbers } from "@monorepo/foo";

export const run = () => {
const value = doubleNumbers([1, 2, 3]);
return value;
};

console.log(run());
3 changes: 3 additions & 0 deletions test/root3/packages/foo/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const hello = (to: string) => {
console.log(`hello ${to}`)
}
10 changes: 10 additions & 0 deletions test/root3/packages/foo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* The module name can be just './hello'.
But '#foo/hello' is demonstration of "Path Mapping" of `tsconfig`
and "Subpath imports"(defined in package.json's `imports` field) of node.js */
import { hello } from "#foo/hello";
// import { hello } from './hello' => this will work, too
// import { hello } from '@monorepo/foo/hello' // => this will not work for tsc, without additional configuration on tsconfig.json

hello("world");

export const doubleNumbers = (data: number[]) => data.map((i) => i * 2);
27 changes: 27 additions & 0 deletions test/root3/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "CommonJS",
"rootDir": ".",
"baseUrl": "packages",
"paths": {
"@monorepo/*": ["*"],
"#foo/*": ["foo/*"],
"#bar/*": ["bar/*"],
"#*": ["*"]
},
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"removeComments": true,
"composite": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"esModuleInterop": true,
"incremental": true,
"resolveJsonModule": true
}
}
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,30 @@ describe('filing-cabinet', function() {
assert.equal(result, path.join(directory, 'foo.ts'));
});
});
describe(`when the typescript's path mapping is configured`, function() {
it('should resolve the path', function() {
const result = cabinet({
partial: '#foo/hello',
filename: path.resolve(__dirname, 'root3', 'packages', 'foo', 'index.ts'),
directory: path.resolve(__dirname, 'root3'),
tsConfig: {
'compilerOptions': {
'rootDir': '.',
'baseUrl': 'packages',
'paths': {
'@monorepo/*': ['*'],
'#foo/*': ['foo/*'],
'#bar/*': ['bar/*'],
'#*': ['*']
},
},
},
tsConfigPath: path.resolve(__dirname, 'root3', 'tsconfig.json'),
});
const expected = path.resolve(__dirname, 'root3', 'packages', 'foo', 'hello.ts');
assert.equal(result, expected);
});
});
});

describe('when not given a tsconfig', function() {
Expand Down

0 comments on commit bc9913f

Please sign in to comment.