Skip to content

Commit 430a5c8

Browse files
committed
Add ESM support
- Add also supertest as example
1 parent c741d7e commit 430a5c8

22 files changed

+913
-46
lines changed

.github/workflows/main.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- uses: actions/checkout@v4
16-
- uses: actions/setup-python@v4
16+
- uses: actions/setup-python@v5
1717
with:
18+
# python-version: '3.12'
1819
cache: 'pip'
1920
- uses: pre-commit/[email protected]
2021

@@ -23,7 +24,7 @@ jobs:
2324

2425
strategy:
2526
matrix:
26-
node-version: [18.x, 20.x]
27+
node-version: [20.x, 22.x]
2728

2829
steps:
2930
- uses: actions/checkout@v4
@@ -38,3 +39,5 @@ jobs:
3839
- run: npm run format
3940
- run: npm run build:debug
4041
- run: npm run build:prod
42+
- run: npm run check:esmloads
43+
- run: npm run check:cjsloads

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*
2+
!dist/**
3+
!src/**
4+
!package*json
5+
!README*
6+
!tsconfig*
7+
tsconfig.eslint.json

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
},
88
"editor.formatOnSaveMode": "modificationsIfAvailable",
99
"[javascript,typescript,jsonc,json]": {
10+
"editor.indentSize": "tabSize",
11+
"editor.tabSize": 2,
1012
"editor.defaultFormatter": "esbenp.prettier-vscode",
1113
"editor.formatOnSaveMode": "modificationsIfAvailable",
1214
},
13-
"eslint.experimental.useFlatConfig": true,
15+
"eslint.useFlatConfig": true,
1416
"jest.jestCommandLine": "npm test -- ",
1517
"jest.runMode": {
1618
"type": "on-demand",

__tests__/replace.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fc, { Arbitrary } from 'fast-check';
2-
import { replace } from '../src';
2+
import { replace } from '../src/replacer.js';
33

44
const EachSimpleType = [
55
['string', fc.string()],

__tests__/server.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { serverFactory } from '@/server.js';
2+
import type { Server } from 'http';
3+
import request from 'supertest';
4+
if (process.env.liveUrl) {
5+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
6+
//@ts-ignore this enables connection to a live server and verify the app
7+
request = request.bind(request, process.env.liveUrl);
8+
}
9+
10+
let app: Server;
11+
12+
beforeAll(() => {
13+
app = serverFactory();
14+
});
15+
16+
describe('Server responds', () => {
17+
// eslint-disable-next-line jest/expect-expect
18+
test('Responds 200', async () => {
19+
await request(app).get('/health').expect(200);
20+
});
21+
});

__tests__/tsconfig.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Typescript config for tests
2+
{
3+
"extends": "../tsconfig.json",
4+
"compilerOptions": {
5+
// "rootDir": "../",
6+
"types": ["jest", "node"],
7+
"composite": true,
8+
"allowJs": true,
9+
"sourceMap": true,
10+
},
11+
"exclude": ["./tsconfig.json"],
12+
// "references": [{ "path": "../tsconfig.json" }],
13+
"include": [
14+
"./**/*",
15+
"./**/*",
16+
"./*",
17+
"../src/**/*",
18+
"../jest*.ts",
19+
"../tsconfig.aliases.json",
20+
"../package.json",
21+
],
22+
}

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export default [
7474
project: [
7575
'./tsconfig.eslint.json',
7676
'./tsconfig.json',
77-
'./tsconfig.prod.json',
77+
'./tsconfig.node.json',
7878
],
7979
},
8080
},

jest.config.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
1-
import type { JestConfigWithTsJest } from 'ts-jest';
1+
import {
2+
createDefaultEsmPreset,
3+
pathsToModuleNameMapper,
4+
type JestConfigWithTsJest,
5+
} from 'ts-jest';
6+
7+
import { compilerOptions } from './tsconfig.aliases.json';
8+
import packageJson from './package.json';
9+
10+
const pathAliases = {
11+
...compilerOptions.paths,
12+
// Jest wants to know the folder to do the transformation, not the `src/index.js`. 🤷
13+
[packageJson.name]: ['src'],
14+
};
15+
16+
// See here for more info https://kulshekhar.github.io/ts-jest/docs/getting-started/presets/#advanced
17+
const preset = createDefaultEsmPreset({
18+
tsconfig: './__tests__/tsconfig.json',
19+
});
220

321
const config: JestConfigWithTsJest = {
4-
preset: 'ts-jest',
22+
...preset,
23+
roots: ['<rootDir>'],
24+
modulePaths: [compilerOptions.baseUrl],
25+
moduleNameMapper: pathsToModuleNameMapper(pathAliases, {
26+
useESM: true,
27+
}),
28+
modulePathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/build/'],
529
testRegex: [
630
'/tests/.*tests?.[jt]sx?',
731
'/__tests__/.*tests?.[jt]sx?',
832
'.*.(spec|test).[jt]sx?',
933
],
1034
// I dono't think we need to run the spec multiple times.. the functional test on tests/ maybe.
11-
// We can change this if we consider it useful to run the spec tests when the code is transpiled to javascript
12-
testPathIgnorePatterns: ['node_modules', 'build/'],
35+
// We can change this back if we consider it useful to run the spec tests when the code is transpiled to javascript
36+
testPathIgnorePatterns: [
37+
'node_modules',
38+
'<rootDir>/build/',
39+
'<rootDir>/dist/',
40+
],
1341
testEnvironment: 'node',
1442
collectCoverageFrom: ['src/**/*.{js,ts,jsx,tsx}'],
1543
verbose: true,

0 commit comments

Comments
 (0)