mokapot

DRY, reusable, composable test resources.

Usage no npm install needed!

<script type="module">
  import mokapot from 'https://cdn.skypack.dev/mokapot';
</script>

README

mokapot.js

DRY, reusable and composable test resources for Mocha.

import { mokapot} from 'mokapot'

// pair of Promise-returning functions
const createServer = (dir) => mokapot.async(
  async () => createServerAsync({ workDir: dir() }), // set up
  async (server) => server.closeAsync()              // optional tear down
)

// async generator
const createServerGen = (dir) => mokapot.gen(async function* () {
  const server = await createServerAsync({ workDir: dir() })
  yield server
  await server.closeAsync()
})

// pair of sync functions
const tmpDir = mokapot.sync(
  () => fs.mkdirSync('/tmp/' + Math.random),
  (dir) => fs.rmdirSync(dir)
)

// pair of NodeJS-style functions
const anotherTmpDir = mokapot.node(
  (cb) => fs.mkdir('/tmp/' + Math.random, cb),
  (dir, cb) => fs.rmdir(dir, cb)
)

describe('server test', function () {
  const tmp = mokapot.before(tmpDir)
  const server = mokapot.beforeEach(createServer(tmp))

  it('should respond', function () {
    assert(server() instanceof Server) // get current value
  });
});

Moka Pot icon created by Marvin Wilhelm for the Noun Project