recital / packages / test / test.js
test.js
Raw
const glob = require('glob')
const path = require('path')

// Basic principles based off of https://www.sohamkamani.com/javascript/making-a-node-js-test-runner/
let tests = []

function test(name, fn) {
	tests.push({ name, fn })
}

function run() {
	tests.forEach((t) => {
		try {
			t.fn()
			console.log('✅', t.name)
		} catch (e) {
			console.log('❌', t.name)
			console.log(e.stack)
		}
	})
}

const args = process.argv.slice(2)
const fileGlob = args[0]

// expose the test function as a global variable
global.test = test
global.it = test

glob(fileGlob, (er, files) => {
	console.log(files)
	if (er) {
		console.log(err)
		return
	}
	if (!files) {
		console.log('No input files found')
	}

	// Load each file using `require`
	files.forEach((file) => {
		// Once a file is loaded, it's tests are
		// added to the `tests` singleton variable
		require(path.resolve(process.cwd(), file))
	})

	run()
})