Skip to content

Commit

Permalink
add compatibility with typescript when using ts-node
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev committed Jul 18, 2022
1 parent 2c92f48 commit bb92769
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
- run: npm run test:ts
if: matrix.node-version == '18.x'
2 changes: 1 addition & 1 deletion hook.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

const specifiers = new Map()

const EXTENSION_RE = /\.(js|mjs|cjs)$/
const EXTENSION_RE = /\.(js|mjs|cjs|ts|mts|cts)$/

const NODE_MAJOR = Number(process.versions.node.split('.')[0])

Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "index.js",
"scripts": {
"test": "c8 --check-coverage --lines 90 imhotap --runner test/runtest --files test/{hook,low-level,other}/*",
"test:ts": "node --test --experimental-specifier-resolution=node --require ts-node/register --loader ./hook.mjs --loader ts-node/esm test/typescript/*.test.mts",
"coverage": "c8 --reporter html imhotap --runner test/runtest --files test/{hook,low-level,other}/* && echo '\nNow open coverage/index.html\n'"
},
"repository": {
Expand All @@ -26,8 +27,11 @@
},
"homepage": "https://github.com/DataDog/import-in-the-middle#readme",
"devDependencies": {
"@types/node": "^18.0.6",
"c8": "^7.8.0",
"imhotap": "^2.0.0"
"imhotap": "^2.0.0",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
},
"dependencies": {
"module-details-from-path": "^1.0.3"
Expand Down
3 changes: 3 additions & 0 deletions test/typescript/say-hi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const sayHi = (name: string) => {
return `Hi ${name}`;
};
14 changes: 14 additions & 0 deletions test/typescript/ts-node.test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import assert from "node:assert/strict";
import test from "node:test";
import { addHook } from "../../index.js";
import { sayHi } from "./say-hi.js";

addHook((url, exported) => {
if (url.toLowerCase().endsWith('say-hi.ts')) {
exported.sayHi = () => 'Hooked';
}
});

test("Hook a module", async () => {
assert.equal(sayHi("test"), "Hooked");
});
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"allowSyntheticDefaultImports": true,
"noEmit": true
},
"ts-node": {
"esm": true
}
}

0 comments on commit bb92769

Please sign in to comment.