Skip to content

Commit 5a8a195

Browse files
committed
Add first esm unit test
1 parent 14ef9c1 commit 5a8a195

File tree

6 files changed

+90
-3
lines changed

6 files changed

+90
-3
lines changed

.github/workflows/unit-test-esm.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Unit tests (ESM)
2+
on:
3+
push: {}
4+
workflow_call: {}
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
fail-fast: false
10+
matrix:
11+
node-version: [22.x]
12+
steps:
13+
- uses: actions/checkout@v4
14+
- name: Use Node.js ${{ matrix.node-version }}
15+
uses: actions/setup-node@v2
16+
with:
17+
node-version: ${{ matrix.node-version }}
18+
- name: Add local.aikido.io to /etc/hosts
19+
run: |
20+
sudo echo "127.0.0.1 local.aikido.io" | sudo tee -a /etc/hosts
21+
- run: make install
22+
- run: cd library && npm run test:esm
23+
- name: "Upload coverage"
24+
uses: codecov/codecov-action@v4.0.1
25+
with:
26+
file: ./library/.tap/report/lcov.info
27+
env:
28+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
29+
slug: AikidoSec/firewall-node
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import t from "tap";
2+
3+
import wrapImport from "./wrapImport.js";
4+
import Package from "./Package.js";
5+
import BuiltinModule from "./BuiltinModule.js";
6+
7+
t.test("it works", async (t) => {
8+
const initialSqlite3 = await import("sqlite3");
9+
t.same(typeof initialSqlite3.default, "object");
10+
11+
const sqlite3Pkg = new Package.Package("sqlite3");
12+
sqlite3Pkg.withVersion("^5.0.0").onRequire((exports, pkgInfo) => {
13+
t.same(pkgInfo.name, "sqlite3");
14+
t.same(pkgInfo.type, "external");
15+
t.ok(pkgInfo.path?.base.endsWith("node_modules/sqlite3"));
16+
t.same(pkgInfo.path?.relative, "lib/sqlite3.js");
17+
18+
exports.default = 42;
19+
});
20+
21+
const internalFs = new BuiltinModule.BuiltinModule("fs");
22+
internalFs.onRequire((exports, pkgInfo) => {
23+
t.same(pkgInfo.name, "fs");
24+
t.same(pkgInfo.type, "builtin");
25+
t.same(pkgInfo.path, undefined);
26+
27+
exports.readFile = () => "hello from aikido";
28+
});
29+
30+
wrapImport.wrapImport([sqlite3Pkg], [internalFs]);
31+
32+
// Should not change anything
33+
wrapImport.wrapImport();
34+
35+
const sqlite3 = await import("sqlite3");
36+
t.same(sqlite3.default, 42);
37+
38+
const fs = await import("fs");
39+
t.same(fs.readFile(), "hello from aikido");
40+
});

library/agent/hooks/wrapRequire.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { isMainJsFile } from "./isMainJsFile";
1212
import { getInstance } from "../AgentSingleton";
1313
import { executeInterceptors } from "./executeInterceptors";
1414

15-
const originalRequire = mod.prototype.require;
15+
let originalRequire: typeof mod.prototype.require | undefined;
1616
let isRequireWrapped = false;
1717

1818
let packages: Package[] = [];
@@ -36,6 +36,8 @@ export function wrapRequire() {
3636
}
3737
// Prevent wrapping the require function multiple times
3838
isRequireWrapped = true;
39+
// Save the original require function
40+
originalRequire = mod.prototype.require;
3941

4042
// @ts-expect-error TS doesn't know that we are not overwriting the subproperties
4143
mod.prototype.require = function wrapped() {

library/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@
9999
"build:watch": "tsc --watch",
100100
"lint": "npm run lint-eslint && npm run lint-tsc",
101101
"lint-eslint": "eslint '**/*.ts'",
102-
"lint-tsc": "tsc --noEmit"
102+
"lint-tsc": "tsc --noEmit",
103+
"test:esm": "npx tap --tsconfig ./tsconfig.esm-test.json '!(*node_modules)/**/*.test.mjs'"
103104
},
104105
"dependencies": {
105106
"import-in-the-middle": "^1.11.1"

library/tsconfig.esm-test.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"lib": ["ESNext", "DOM"],
5+
"module": "NodeNext",
6+
"moduleResolution": "NodeNext",
7+
"declaration": true,
8+
"strict": true,
9+
"allowJs": false,
10+
"skipLibCheck": true,
11+
"esModuleInterop": true,
12+
"allowSyntheticDefaultImports": true
13+
},
14+
"include": ["**/*.test.mjs"]
15+
}

library/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"skipLibCheck": true
1212
},
1313
"include": ["**/*.ts"],
14-
"exclude": ["**/*.test.ts"]
14+
"exclude": ["**/*.test.ts", "**/*.test.mjs"]
1515
}

0 commit comments

Comments
 (0)