Skip to content

Commit 4bc8ad4

Browse files
committed
✨✅ add tsconfigs & tests
1 parent a3b32ef commit 4bc8ad4

File tree

19 files changed

+230
-10
lines changed

19 files changed

+230
-10
lines changed

typescript-configs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/

typescript-configs/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# @dudeofawesome/typescript-configs
2+
3+
My set of tsc config files
4+
5+
## Usage
6+
7+
1. Install this config plugin as a dev dependency.
8+
9+
```sh
10+
npm i -D @dudeofawesome/eslint-config-typescript
11+
```
12+
13+
1. Pick the appropriate config to base yours on.
14+
1. Create your `tsconfig.json`.
15+
16+
```json
17+
{
18+
"extends": "./node.json"
19+
}
20+
```
21+
22+
1. Set your `include` and `exclude` paths.
23+
24+
Here's an example of something that might work for you:
25+
26+
```json
27+
{
28+
...
29+
"include": ["src/"],
30+
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
31+
}
32+
```

typescript-configs/base.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
// input
77
"skipLibCheck": false, // TODO: do we actually want false?
88
"allowJs": true,
9+
"checkJs": true,
910
"incremental": true,
10-
"forceConsistentCasingInFileNames": false,
11+
"forceConsistentCasingInFileNames": true,
1112
"resolveJsonModule": true,
13+
"lib": ["ES2022"],
1214

1315
// output
1416
"declaration": true,
@@ -34,7 +36,5 @@
3436
// "strictPropertyInitialization": true,
3537
"useUnknownInCatchVariables": true,
3638
"noUncheckedIndexedAccess": true // TODO: can we actually do this?
37-
},
38-
"include": ["src/"],
39-
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
39+
}
4040
}

typescript-configs/browser.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{
2-
"extends": "./base.json"
2+
"extends": "./base.json",
3+
"compilerOptions": {
4+
"lib": ["DOM"],
5+
"target": "ES2019" // TODO: what browsers do we want to support?
6+
}
37
}

typescript-configs/jest.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
{
2-
"extends": "./base.json"
3-
}
1+
{}

typescript-configs/library.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"declarationMap": true,
5+
"sourceMap": true
6+
}
7+
}

typescript-configs/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
"license": "MIT",
1616
"main": "base.json",
1717
"scripts": {
18-
"test": "# node --require ts-node/register --test test/index.ts"
18+
"test": "node --require ts-node/register --test test/index.ts"
1919
},
2020
"peerDependencies": {
2121
"typescript": "^5.0.0, <=5.2.0"
2222
},
2323
"devDependencies": {
24+
"@types/jest": "^29.5.5",
2425
"@types/node": "^20.6.3",
25-
"ts-node": "^10.9.1"
26+
"@types/react": "^18.2.23",
27+
"ts-node": "^10.9.1",
28+
"typescript": "^5.0.0, <=5.2.0"
2629
}
2730
}

typescript-configs/react.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"jsx": "react"
4+
}
5+
}

typescript-configs/test/index.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { describe, it } from 'node:test';
2+
import { deepEqual, strictEqual, match } from 'node:assert';
3+
import { exec as execCallback } from 'node:child_process';
4+
import { promisify } from 'node:util';
5+
import { basename, join } from 'node:path';
6+
import { readFile } from 'node:fs/promises';
7+
8+
const exec = promisify(execCallback);
9+
10+
describe('typescript-configs', () => {
11+
describe('jest', async () => {
12+
await transpile_and_test({ dir: 'jest', code_fragment: 'function foo()' });
13+
await transpile_and_test({
14+
dir: 'jest',
15+
ts_config: 'tsconfig.test.json',
16+
output_files: [
17+
'index.js',
18+
'index.js.map',
19+
'index.d.ts',
20+
'index.spec.js',
21+
'index.spec.js.map',
22+
'index.spec.d.ts',
23+
],
24+
search_file: 'test/index.spec.js',
25+
code_fragment: 'expect',
26+
});
27+
});
28+
29+
describe('library', async () => {
30+
await transpile_and_test({
31+
dir: 'library',
32+
output_files: [
33+
'index.js',
34+
'index.js.map',
35+
'index.d.ts',
36+
'index.d.ts.map',
37+
],
38+
code_fragment: `return 'bar'`,
39+
});
40+
});
41+
42+
describe('node', async () => {
43+
await transpile_and_test({
44+
dir: 'node',
45+
code_fragment: 'process.env.PATH',
46+
});
47+
});
48+
49+
describe('react', async () => {
50+
await transpile_and_test({
51+
dir: 'react',
52+
code_fragment: 'window.location.href',
53+
});
54+
});
55+
});
56+
57+
interface TranspileAndTestOpts {
58+
dir: string;
59+
ts_config?: string;
60+
output_files?: string[];
61+
search_file?: string;
62+
code_fragment?: string;
63+
}
64+
async function transpile_and_test({
65+
dir,
66+
ts_config = 'tsconfig.json',
67+
output_files = ['index.js', 'index.js.map', 'index.d.ts'],
68+
search_file = 'index.js',
69+
code_fragment,
70+
}: TranspileAndTestOpts): Promise<{
71+
stdout: string;
72+
stderr: string;
73+
}> {
74+
const cwd = join(__dirname, dir);
75+
const out_dir = join(cwd, 'dist');
76+
const out = await exec(
77+
`tsc --project "${ts_config}" --outDir "${out_dir}" --incremental false --listEmittedFiles`,
78+
{
79+
cwd,
80+
env: {
81+
...process.env,
82+
// prevent debugging TSC
83+
NODE_OPTIONS: undefined,
84+
},
85+
},
86+
);
87+
88+
it('should not output any errors', () => {
89+
strictEqual(out.stderr, '');
90+
});
91+
92+
it('should create transpiled files', () => {
93+
deepEqual(
94+
out.stdout
95+
.split('\n')
96+
.filter(Boolean)
97+
.map((line) => basename(line.split('TSFILE: ')[1])),
98+
output_files,
99+
);
100+
});
101+
102+
if (code_fragment != null) {
103+
it('should contain some expected code', async () => {
104+
match(
105+
(await readFile(join(out_dir, search_file))).toString(),
106+
new RegExp(code_fragment),
107+
);
108+
});
109+
}
110+
111+
return out;
112+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function foo(): string {
2+
return 'bar';
3+
}

0 commit comments

Comments
 (0)