caderek / aocrunner

Advent of Code runner
ISC License
163 stars 23 forks source link

How can I add jest or mocha to this? #7

Closed williammartin closed 2 years ago

williammartin commented 2 years ago

Hey,

I absolutely don't want to take up much of your time with this issue so please if it's totally out of scope just close it.

I've been using this runner and I really like it but with the strict ESM settings, I'm not really sure which package.json or tsconfig.json things I'd need to set to get tests working (initial attempts fell over relating to ESM).

If you happened to know how to set this up, or felt like it would be a useful addition to the runner as the exercises get trickier and testing smaller parts might be useful, that would be super awesome.

Cheers for the great project either way!

caderek commented 2 years ago

I haven't use Mocha in years, so I won't be much of a help here. Also, Jest has rather poor support for running ES modules directly, and the docs for this are terrible - I didn't manage to set it up successfully (I guess your best chance is to use it wit Babel plugin to translate back to CommonJS).

That said, if you don't mind other testing libraries, node-tap should work quite well, and the setup is simple:

npm i tap @types/tap -D

then add these lines to the scripts section in your package.json:

"test": "tap --test-regex='/.(spec|test).js/'",
"test:watch": "tap --test-regex='/.(spec|test).js/' --watch",

Now you can write your tests as .spec.js / .spec.ts / .test.js / .test.ts files (if you are using TypeScript, make sure that aocrunner is running), and run them with npm run test or npm run test:watch.

API is quite simple, example test file:

import { test } from "tap"
import add from "./add.js"

test("Works", (t) => {
  t.equal(1, 1)
  t.end()
})

test("Also works", (t) => {
  t.equal(add(1, 1), 2)
  t.end()
})

Node Tap docs: https://node-tap.org/docs/api/

williammartin commented 2 years ago

Thanks so much, after some frustration with mocha and jest I used https://github.com/lukeed/uvu but I'll try out tap today and get back to you, it looks very neat.