From e593ff968c3803e61d5e765c008085e42461adda Mon Sep 17 00:00:00 2001 From: Ryuu Mitsuki Date: Sun, 14 Apr 2024 22:13:27 +0700 Subject: [PATCH] test: Implement unit tests for this project There are two spec files, `lsfnd.spec.cjs` for CommonJS test, and `lsfnd.spec.mjs` for ES Modules test. Additionally, added NPM scripts to run the tests. --- package.json | 5 ++++- test/lsfnd.spec.cjs | 29 +++++++++++++++++++++++++++++ test/lsfnd.spec.mjs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 test/lsfnd.spec.cjs create mode 100644 test/lsfnd.spec.mjs diff --git a/package.json b/package.json index d03ba43..a3fd4ea 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,10 @@ "build": "ts-node scripts/build.ts", "minify": "ts-node scripts/build.ts --minify", "docs": "npm run-script build:docs", - "build:docs": "typedoc --options typedoc.config.js" + "build:docs": "typedoc --options typedoc.config.js", + "test": "node test/lsfnd.spec.cjs && node test/lsfnd.spec.mjs", + "test:cjs": "node test/lsfnd.spec.cjs", + "test:mjs": "node test/lsfnd.spec.mjs" }, "repository": { "type": "git", diff --git a/test/lsfnd.spec.cjs b/test/lsfnd.spec.cjs new file mode 100644 index 0000000..162fe20 --- /dev/null +++ b/test/lsfnd.spec.cjs @@ -0,0 +1,29 @@ +const { join, basename } = require('node:path'); +const { ls, lsFiles, lsDirs } = require('..'); +const { it, rejects, deepEq } = require('./lib/simpletest'); + +console.log(`\n\x1b[1m${basename(__filename)}:\x1b[0m`); + +it('test `ls` function by listing this file directory', async () => { + const results = await ls(__dirname, {}, 0); + const expected = [ 'lib', 'lsfnd.spec.cjs', 'lsfnd.spec.mjs' ] + .map((e) => join(__dirname, e)); + deepEq(results, expected); +}, false); + +it('test `lsFiles` function by listing this file directory', async () => { + const results = await lsFiles(__dirname); + const expected = [ 'lsfnd.spec.cjs', 'lsfnd.spec.mjs' ] + .map((e) => join(__dirname, e)); + deepEq(results, expected); +}, false); + +it('test `lsDirs` function by listing this file directory', async () => { + const results = await lsDirs(__dirname); + const expected = [ 'lib' ].map((e) => join(__dirname, e)); + deepEq(results, expected); +}, false); + +it('throws an error if given directory path not exist', async () => { + await rejects(ls('./this/is/not/exist/directory/path'), Error); +}, false); diff --git a/test/lsfnd.spec.mjs b/test/lsfnd.spec.mjs new file mode 100644 index 0000000..0260156 --- /dev/null +++ b/test/lsfnd.spec.mjs @@ -0,0 +1,35 @@ +import { join, dirname, basename } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { ls, lsFiles, lsDirs } from '../dist/index.js'; +import test from './lib/simpletest.js'; +const { it, rejects, deepEq } = test; + +// Create the '__dirname' and '__filename' variable, because in ESM these are not defined +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +console.log(`\n\x1b[1m${basename(__filename)}:\x1b[0m`); + +it('test `ls` function by listing this file directory', async () => { + const results = await ls(__dirname, {}, 0); + const expected = [ 'lib', 'lsfnd.spec.cjs', 'lsfnd.spec.mjs' ] + .map((e) => join(__dirname, e)); + deepEq(results, expected); +}, false); + +it('test `lsFiles` function by listing this file directory', async () => { + const results = await lsFiles(__dirname); + const expected = [ 'lsfnd.spec.cjs', 'lsfnd.spec.mjs' ] + .map((e) => join(__dirname, e)); + deepEq(results, expected); +}, false); + +it('test `lsDirs` function by listing this file directory', async () => { + const results = await lsDirs(__dirname); + const expected = [ 'lib' ].map((e) => join(__dirname, e)); + deepEq(results, expected); +}, false); + +it('throws an error if given directory path not exist', async () => { + await rejects(ls('./this/is/not/exist/directory/path'), Error); +}, false);