Skip to content

Commit

Permalink
test: re-implement original tests
Browse files Browse the repository at this point in the history
  • Loading branch information
effervescentia committed Apr 25, 2024
1 parent 834b4b5 commit 525ffa2
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 14 deletions.
5 changes: 5 additions & 0 deletions example/basic/tsconfig.json
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"baseUrl": "hej"
}
}
10 changes: 10 additions & 0 deletions example/extend-multiple/dir1/dir2/tsconfig.json
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"foo": [
"bar2"
]
}
}
}
5 changes: 5 additions & 0 deletions example/extend-multiple/dir1/tsconfig.json
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"baseUrl": "."
}
}
10 changes: 10 additions & 0 deletions example/extend-multiple/tsconfig.base.json
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"foo": [
"bar"
]
}
}
}
7 changes: 7 additions & 0 deletions example/extend-multiple/tsconfig.json
@@ -0,0 +1,7 @@
{
"extends": [
"./tsconfig.base.json",
"./dir1/tsconfig.json",
"./dir1/dir2/tsconfig.json"
]
}
11 changes: 11 additions & 0 deletions example/extend-node-module/tsconfig.json
@@ -0,0 +1,11 @@
{
"extends": "shared-tsconfig",
"compilerOptions": {
"baseUrl": "kalle",
"paths": {
"foo": [
"bar2"
]
}
}
}
11 changes: 11 additions & 0 deletions example/extend-overwrite/nested/tsconfig.json
@@ -0,0 +1,11 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"baseUrl": "kalle",
"paths": {
"foo": [
"bar2"
]
}
}
}
11 changes: 11 additions & 0 deletions example/extend-overwrite/tsconfig.base.json
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"baseUrl": "olle",
"paths": {
"foo": [
"bar1"
]
},
"strict": true
}
}
10 changes: 10 additions & 0 deletions example/extend-without-extension/tsconfig.base.json
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"foo": [
"bar"
]
}
}
}
5 changes: 5 additions & 0 deletions example/extend-without-extension/tsconfig.json
@@ -0,0 +1,5 @@
{
"extends": [
"./tsconfig.base"
]
}
8 changes: 8 additions & 0 deletions example/invalid/tsconfig.json
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"paths": {
"foo": [
"bar"
]
}
}
3 changes: 3 additions & 0 deletions example/resolve-closest/dir1/dir2/tsconfig.json
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.json"
}
3 changes: 3 additions & 0 deletions example/resolve-closest/dir1/tsconfig.json
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.json"
}
5 changes: 5 additions & 0 deletions example/resolve-closest/tsconfig.json
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"baseUrl": "."
}
}
6 changes: 6 additions & 0 deletions example/with-comments/tsconfig.json
@@ -0,0 +1,6 @@
{
// my comment
"compilerOptions": {
"baseUrl": "hej"
}
}
5 changes: 5 additions & 0 deletions example/with-trailing-commas/tsconfig.json
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"baseUrl": "hej",
},
}
117 changes: 114 additions & 3 deletions src/__tests__/tsconfig-loader.test.ts
Expand Up @@ -164,13 +164,124 @@ describe("walkForTsConfig", () => {
});

describe("loadSyncDefault", () => {
it("should result multiple levels of tsconfig extension", () => {
const cwd = resolve(__dirname, "../../example/inherited");
it("should load a config", () => {
const cwd = resolve(__dirname, "../../example/basic")

const result = tsConfigLoader({ cwd, getEnv: () => undefined });

expect(result).toEqual({
baseUrl: "./hej",
paths: {},
tsConfigPath: resolve(cwd, "tsconfig.json"),
});
});

it("should load a config with comments", () => {
const cwd = resolve(__dirname, "../../example/with-comments")

const result = tsConfigLoader({ cwd, getEnv: () => undefined });

expect(result).toEqual({
baseUrl: "./hej",
paths: {},
tsConfigPath: resolve(cwd, "tsconfig.json"),
});
});

it("should load a config with trailing commas", () => {
const cwd = resolve(__dirname, "../../example/with-trailing-commas")

const result = tsConfigLoader({ cwd, getEnv: () => undefined });

expect(result).toEqual({
baseUrl: "./hej",
paths: {},
tsConfigPath: resolve(cwd, "tsconfig.json"),
});
});

it("should gracefully handle invalid JSON5", () => {
const cwd = resolve(__dirname, "../../example/invalid")

const result = tsConfigLoader({ cwd, getEnv: () => undefined });

expect(result).toEqual({
baseUrl: undefined,
paths: { foo: ['bar'] },
tsConfigPath: resolve(cwd, 'tsconfig.json')
});
});

it("should load a config with string extends and overwrite all options", () => {
const cwd = resolve(__dirname, "../../example/extend-overwrite")
const tsConfigPath = resolve(cwd, 'nested/tsconfig.json');

const result = tsConfigLoader({
cwd,
getEnv: (_: string) => undefined,
getEnv: (name: string) => name === 'TS_NODE_PROJECT' ? tsConfigPath : undefined
});

expect(result).toEqual({
baseUrl: "./kalle",
paths: { foo: ["bar2"] },
strict: true,
tsConfigPath
});
});

it("should load a config with string extends from node_modules and overwrite all options", () => {
const cwd = resolve(__dirname, "../../example/extend-node-module")

const result = tsConfigLoader({ cwd, getEnv: () => undefined });

expect(result).toEqual({
baseUrl: "./kalle",
paths: { foo: ["bar2"] },
strict: true,
tsConfigPath: resolve(cwd, "tsconfig.json")
});
});

it("should use baseUrl relative to location of extended tsconfig", () => {
const cwd = resolve(__dirname, "../../example/resolve-closest/dir1/dir2")

const result = tsConfigLoader({ cwd, getEnv: () => undefined });

expect(result).toEqual({
baseUrl: "../..",
paths: {},
tsConfigPath: resolve(cwd, "tsconfig.json")
});
});

it("should load a config with array extends and overwrite all options", () => {
const cwd = resolve(__dirname, "../../example/extend-multiple")

const result = tsConfigLoader({ cwd, getEnv: () => undefined });

expect(result).toEqual({
baseUrl: "./dir1/dir2",
paths: { foo: ["bar2"] },
tsConfigPath: resolve(cwd, "tsconfig.json")
});
});

it("should load a config with array extends without .json extension", () => {
const cwd = resolve(__dirname, "../../example/extend-without-extension")

const result = tsConfigLoader({ cwd, getEnv: () => undefined });

expect(result).toEqual({
baseUrl: "./",
paths: { foo: ["bar"] },
tsConfigPath: resolve(cwd, "tsconfig.json")
});
});

it("should resolve multiple levels of tsconfig extension", () => {
const cwd = resolve(__dirname, "../../example/inherited");
const result = tsConfigLoader({ cwd, getEnv: () => undefined });

expect(result).toEqual({
baseUrl: undefined,
paths: { "@": [] },
Expand Down
14 changes: 3 additions & 11 deletions src/tsconfig-loader.ts
Expand Up @@ -64,17 +64,9 @@ function loadSyncDefault(

return {
tsConfigPath: configPath,
baseUrl:
baseUrl ||
(tsconfig &&
tsconfig.config.compilerOptions &&
tsconfig.config.compilerOptions.baseUrl) ||
undefined,
paths:
(tsconfig &&
tsconfig.config.compilerOptions &&
tsconfig.config.compilerOptions.paths) ??
{},
...tsconfig?.config.compilerOptions,
baseUrl: baseUrl || tsconfig?.config.compilerOptions?.baseUrl,
paths: tsconfig?.config.compilerOptions?.paths ?? {},
};
}

Expand Down

0 comments on commit 525ffa2

Please sign in to comment.