Skip to content

Commit

Permalink
Add first esm unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
timokoessler committed Sep 30, 2024
1 parent 14ef9c1 commit 5a8a195
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 3 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/unit-test-esm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Unit tests (ESM)
on:
push: {}
workflow_call: {}
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version: [22.x]
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- name: Add local.aikido.io to /etc/hosts
run: |
sudo echo "127.0.0.1 local.aikido.io" | sudo tee -a /etc/hosts
- run: make install
- run: cd library && npm run test:esm
- name: "Upload coverage"
uses: codecov/[email protected]
with:
file: ./library/.tap/report/lcov.info
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
slug: AikidoSec/firewall-node
40 changes: 40 additions & 0 deletions library/agent/hooks/wrapImport.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import t from "tap";

import wrapImport from "./wrapImport.js";
import Package from "./Package.js";
import BuiltinModule from "./BuiltinModule.js";

t.test("it works", async (t) => {
const initialSqlite3 = await import("sqlite3");
t.same(typeof initialSqlite3.default, "object");

const sqlite3Pkg = new Package.Package("sqlite3");
sqlite3Pkg.withVersion("^5.0.0").onRequire((exports, pkgInfo) => {
t.same(pkgInfo.name, "sqlite3");
t.same(pkgInfo.type, "external");
t.ok(pkgInfo.path?.base.endsWith("node_modules/sqlite3"));
t.same(pkgInfo.path?.relative, "lib/sqlite3.js");

exports.default = 42;
});

const internalFs = new BuiltinModule.BuiltinModule("fs");
internalFs.onRequire((exports, pkgInfo) => {
t.same(pkgInfo.name, "fs");
t.same(pkgInfo.type, "builtin");
t.same(pkgInfo.path, undefined);

exports.readFile = () => "hello from aikido";
});

wrapImport.wrapImport([sqlite3Pkg], [internalFs]);

// Should not change anything
wrapImport.wrapImport();

const sqlite3 = await import("sqlite3");
t.same(sqlite3.default, 42);

const fs = await import("fs");
t.same(fs.readFile(), "hello from aikido");
});
4 changes: 3 additions & 1 deletion library/agent/hooks/wrapRequire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { isMainJsFile } from "./isMainJsFile";
import { getInstance } from "../AgentSingleton";
import { executeInterceptors } from "./executeInterceptors";

const originalRequire = mod.prototype.require;
let originalRequire: typeof mod.prototype.require | undefined;
let isRequireWrapped = false;

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

// @ts-expect-error TS doesn't know that we are not overwriting the subproperties
mod.prototype.require = function wrapped() {
Expand Down
3 changes: 2 additions & 1 deletion library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
"build:watch": "tsc --watch",
"lint": "npm run lint-eslint && npm run lint-tsc",
"lint-eslint": "eslint '**/*.ts'",
"lint-tsc": "tsc --noEmit"
"lint-tsc": "tsc --noEmit",
"test:esm": "npx tap --tsconfig ./tsconfig.esm-test.json '!(*node_modules)/**/*.test.mjs'"
},
"dependencies": {
"import-in-the-middle": "^1.11.1"
Expand Down
15 changes: 15 additions & 0 deletions library/tsconfig.esm-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "ESNext",
"lib": ["ESNext", "DOM"],
"module": "NodeNext",
"moduleResolution": "NodeNext",
"declaration": true,
"strict": true,
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
},
"include": ["**/*.test.mjs"]
}
2 changes: 1 addition & 1 deletion library/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"skipLibCheck": true
},
"include": ["**/*.ts"],
"exclude": ["**/*.test.ts"]
"exclude": ["**/*.test.ts", "**/*.test.mjs"]
}

0 comments on commit 5a8a195

Please sign in to comment.