Last active
March 22, 2024 22:46
-
-
Save osartun/d40a8f6d2b0b37bece376e974705beb6 to your computer and use it in GitHub Desktop.
Test utilities for TypeScript pads on CoderPad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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