Skip to content

Instantly share code, notes, and snippets.

@osartun
Last active March 22, 2024 22:46
Show Gist options
  • Save osartun/d40a8f6d2b0b37bece376e974705beb6 to your computer and use it in GitHub Desktop.
Save osartun/d40a8f6d2b0b37bece376e974705beb6 to your computer and use it in GitHub Desktop.
Test utilities for TypeScript pads on CoderPad
/**
* You want to use mocha in a TypeScript CoderPad but you get the error
* message that `describe` isn't defined even though you're already using
* the `mocha.suite.emit('pre-require', this, 'solution', mocha)` hack?
*
* > Cannot find name 'describe'. Do you need to install type definitions
* > for a test runner? Try `npm i --save-dev @types/jest` or
* > `npm i --save-dev @types/mocha`.
*
* Here are a couple of lines to copy & paste into your pad to use
* `describe` and `it`:
*/
const describe = (name: string, cb: Function) => {
console.log(name);
console.group();
cb();
console.groupEnd();
}
const it = (name: string, cb: Function) => {
try {
cb();
console.log(`✅ ${name}`);
} catch (e) {
console.warn(`❌ ${name}`);
console.error(e);
}
}
/* Happy coding: */
const { expect } = require('chai')
describe('My suite', () => {
describe('My nested suite', () => {
it('resolves successfully', () => {
expect(true).to.equal(true);
})
it('throws an error', () => {
expect(true).to.equal(false);
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment