From 07e767c5765a1206cdeadcd098e2f951bd2fa441 Mon Sep 17 00:00:00 2001 From: Borewit Date: Wed, 18 Dec 2024 18:01:31 +0100 Subject: [PATCH] Add unit tests --- .github/workflows/nodejs-ci.yml | 37 +++++++++++++++++++++++++++++++++ .mocharc.json | 3 +++ package.json | 17 ++++++++++++++- test/mock.mjs | 1 + test/test.ts | 33 +++++++++++++++++++++++++++++ test/tsconfig.json | 8 +++++++ 6 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/nodejs-ci.yml create mode 100644 .mocharc.json create mode 100644 test/mock.mjs create mode 100644 test/test.ts create mode 100644 test/tsconfig.json diff --git a/.github/workflows/nodejs-ci.yml b/.github/workflows/nodejs-ci.yml new file mode 100644 index 0000000..3721ad5 --- /dev/null +++ b/.github/workflows/nodejs-ci.yml @@ -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 \ No newline at end of file diff --git a/.mocharc.json b/.mocharc.json new file mode 100644 index 0000000..fbbfc17 --- /dev/null +++ b/.mocharc.json @@ -0,0 +1,3 @@ +{ + "spec": ["test/*.js"] +} \ No newline at end of file diff --git a/package.json b/package.json index e5f8525..42413dc 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" + } } diff --git a/test/mock.mjs b/test/mock.mjs new file mode 100644 index 0000000..18ebbf4 --- /dev/null +++ b/test/mock.mjs @@ -0,0 +1 @@ +export const testValue = 42; \ No newline at end of file diff --git a/test/test.ts b/test/test.ts new file mode 100644 index 0000000..25b6cff --- /dev/null +++ b/test/test.ts @@ -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'); + } + }); +}); diff --git a/test/tsconfig.json b/test/tsconfig.json new file mode 100644 index 0000000..635b5ad --- /dev/null +++ b/test/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "inlineSources": false, + "module": "commonjs", + "moduleResolution": "node", + "target": "ES2017" + } +} \ No newline at end of file