Skip to content

Instantly share code, notes, and snippets.

@kristianmandrup
Last active October 16, 2016 13:51
Show Gist options
  • Save kristianmandrup/16cb55ac677f88f34abb79e9baf1aa89 to your computer and use it in GitHub Desktop.
Save kristianmandrup/16cb55ac677f88f34abb79e9baf1aa89 to your computer and use it in GitHub Desktop.
Improved Javascript testing DSL
require('babel-core/register');
require('babel-polyfill');
const chai = require('chai');
chai.should();
class Tester {
constructor(parent, text, opts) {
this.parent = parent;
this.text = text;
this.opts = opts;
this.itFuns = [];
this.will = this.should;
if (typeof opts === 'function')
this.fun = opts;
if (typeof opts === 'object' && this.opts.prepare) {
this.clazz = opts.prepare;
}
}
that(text, opts) {
return new Tester(this, text, opts);
}
for(text, opts) {
return new Tester(this, text, opts);
}
describe(fun) {
if (this.parent) {
// console.log('describe w parent', this.text)
this.parent.describe(() => {
let instance;
if (this.clazz) {
instance = new this.clazz();
}
if (instance && instance.beforeEach)
before(instance.before);
if (instance && instance.beforeEach)
beforeEach(instance.beforeEach);
if (instance && instance.afterEach)
afterEach(instance.afterEach);
if (instance && instance.after)
after(instance.after);
describe(this.text, fun);
})
} else {
describe(this.text, fun);
}
}
should(text, fun) {
// console.log('should parent', this.parent.text)
this.itFuns.push(() => {
it(text, fun);
});
return this;
}
run() {
return this.describe(() => {
for (let fun of this.itFuns) {
fun();
}
})
}
}
export const expect = chai.expect;
export function test(text, opts) {
return new Tester(null, text, opts);
}
const fs = require('fs-promise');
import path from 'path';
const { test, expect } = require('./dsl');
const context = {
delete: async () => {
try {
await fs.stat(path.join(__dirname, './dsl.js'));
return true;
} catch (err) {
return false;
}
}
}
const check = {
wasDeleted: (ctx) => {
expect(ctx).to.eql(true);
},
wasIdexed: (ctx) => {
expect(true).to.eql(true);
}
}
class Ctx{
before() {
this.x = 2;
console.log('x = ', this.x)
}
beforeEach() {
console.log('before each');
}
after() {
this.x = 0;
console.log('x = ', this.x)
}
afterEach() {
console.log('after each');
}
}
describe('Plugin', () => {
describe('Adds item', () => {
it('adds all props', () => {
expect(1 + 1).to.eql(2);
})
})
})
test('Addon')
.that('READ item')
.for('some cool stuff')
.will('read a single component', () => {
expect(1 + 1).to.eql(2);
})
.run();
test('Components')
.that('DELETE item', {
prepare: Ctx
})
.should('delete a single component', async () => {
let result = await context.delete(2);
// console.log('result', result);
check.wasDeleted(result);
})
.should('delete also update index', () => {
check.wasIdexed(true);
})
.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment