Skip to content

Commit

Permalink
run typescript tests with imhotap
Browse files Browse the repository at this point in the history
  • Loading branch information
fardjad committed Jul 19, 2022
1 parent 28e4aae commit 0db7af1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 25 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,3 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
- run: npm run test:ts
if: matrix.node-version == '18.x'
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"description": "Intercept imports in Node.js",
"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 ./test/typescript/iitm-ts-node-loader.mjs test/typescript/*.test.mts",
"test": "c8 --check-coverage --lines 80 imhotap --runner test/runtest --files test/{hook,low-level,other,typescript}/*",
"coverage": "c8 --reporter html imhotap --runner test/runtest --files test/{hook,low-level,other}/* && echo '\nNow open coverage/index.html\n'"
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion test/typescript/say-hi.mts → test/fixtures/say-hi.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const sayHi = (name: string) => {
return `Hi ${name}`;
return `Hi ${name}`
};
7 changes: 6 additions & 1 deletion test/runtest
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ const args = [
...process.argv.slice(2)
]
if (!filename.includes('disabled')) {
args.unshift(`--experimental-loader=${path.join(__dirname, '..', 'hook.mjs')}`)
const isTypescript = path.extname(filename).slice(-2) === 'ts'
const loaderPath = isTypescript
? path.join(__dirname, 'typescript', 'iitm-ts-node-loader.mjs')
: path.join(__dirname, '..', 'hook.mjs')

args.unshift(`--experimental-loader=${loaderPath}`)
}

spawn('node', args, { stdio: 'inherit' }).on('close', code => {
Expand Down
18 changes: 9 additions & 9 deletions test/typescript/iitm-ts-node-loader.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import * as iitm from "../../hook.mjs";
import * as tsNode from "ts-node/esm.mjs";
import * as iitm from '../../hook.mjs'
import * as tsNode from 'ts-node/esm.mjs'

const makeNext = (loader, fnName, parentResolveOrLoad) => {
return (specifierOrUrl, context) => {
return loader[fnName](specifierOrUrl, context, parentResolveOrLoad);
};
};
return loader[fnName](specifierOrUrl, context, parentResolveOrLoad)
}
}

export async function resolve(specifier, context, defaultResolve) {
const next = makeNext(tsNode, "resolve", defaultResolve);
return iitm.resolve(specifier, context, next);
const next = makeNext(tsNode, 'resolve', defaultResolve)
return iitm.resolve(specifier, context, next)
}

export async function load(url, context, defaultLoad) {
let next = makeNext(tsNode, "load", defaultLoad);
return iitm.load(url, context, next);
let next = makeNext(tsNode, 'load', defaultLoad)
return iitm.load(url, context, next)
}
17 changes: 7 additions & 10 deletions test/typescript/ts-node.test.mts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import assert from "node:assert/strict";
import test from "node:test";
import { addHook } from "../../index.js";
import { sayHi } from "./say-hi.mjs";
import assert from 'node:assert/strict'
import { addHook } from '../../index.js'
import { sayHi } from '../fixtures/say-hi.mjs'

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

test("Hook a module", async () => {
assert.equal(sayHi("test"), "Hooked");
});
assert.equal(sayHi('test'), 'Hooked')

0 comments on commit 0db7af1

Please sign in to comment.