Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Dec 18, 2024
1 parent 43e9f32 commit 07e767c
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 1 deletion.
37 changes: 37 additions & 0 deletions .github/workflows/nodejs-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Node.js CI
on:
pull_request:
branches: [ "master" ]
push:

jobs:

test:

runs-on: ubuntu-latest

env:
YARN_IGNORE_NODE: 1

strategy:
matrix:
node-version: [18.x, 20.x, 22.x]

steps:

- name: 'Checkout the repository'
uses: actions/checkout@v4

- name: Test with Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm install

- name: Compile TypeScript test code
run: npm run compile-test

- name: Run tests
run: npm run test
3 changes: 3 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"spec": ["test/*.js"]
}
17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
"version": "1.0.0",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"test": "mocha",
"compile-test": "tsc -p test"
},
"files": [
"index.js",
"index.d.ts"
],
"author": {
"name": "Borewit",
"url": "https://github.com/Borewit"
Expand Down Expand Up @@ -45,5 +53,12 @@
"engines": {
"node": ">=13.2.0"
},
"dependencies": {}
"devDependencies": {
"@types/chai": "^5.0.1",
"@types/mocha": "^10.0.10",
"@types/node": "^22.10.2",
"chai": "^4.3.4",
"mocha": "^11.0.1",
"typescript": "^5.7.2"
}
}
1 change: 1 addition & 0 deletions test/mock.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const testValue = 42;
33 changes: 33 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {describe, it} from 'mocha';
import * as chai from 'chai';
import * as path from 'path';
import {loadEsm} from '../index.js';

const {expect} = chai;

describe('loadEsm', function () {
it('should load an ES module dynamically', async function () {
// Mock ES module file for testing.
const modulePath = path.join(__dirname, 'mock.mjs');

// Load the ES module dynamically using loadEsm.
const esmModule = await loadEsm(`file://${modulePath}`);

// Verify the loaded module.
expect(esmModule).to.be.an('module');
expect(esmModule.testValue).to.equal(42);
});

it('should throw an error if the module path is invalid', async function () {
const invalidPath = 'file:///non-existent-module.js';

try {
await loadEsm(invalidPath);
// If no error is thrown, this test should fail.
throw new Error('Expected loadEsm to throw an error, but it did not.');
} catch (error) {
// Verify that the error is thrown.
expect(error).to.be.an('error');
}
});
});
8 changes: 8 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"inlineSources": false,
"module": "commonjs",
"moduleResolution": "node",
"target": "ES2017"
}
}

0 comments on commit 07e767c

Please sign in to comment.