Last active
October 16, 2016 13:51
-
-
Save kristianmandrup/16cb55ac677f88f34abb79e9baf1aa89 to your computer and use it in GitHub Desktop.
Improved Javascript testing DSL
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
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); | |
} |
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
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