Skip to content

Commit

Permalink
test: Implement unit tests for this project
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mitsuki31 committed Apr 14, 2024
1 parent ef895ac commit e593ff9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 29 additions & 0 deletions test/lsfnd.spec.cjs
Original file line number Diff line number Diff line change
@@ -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);
35 changes: 35 additions & 0 deletions test/lsfnd.spec.mjs
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit e593ff9

Please sign in to comment.