Skip to content

papercuptech/mocha-lambda

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Provide mocha this context to lambda functions

This is an alpha solution enabling effective use of lambdas with mocha in a backwards compatible way

The quick way

require('mocha-lambda')
// global '_tst' can now be used where 'this' was before

describe('mocha-lambda', () => {
  it('provides "this" as "_tst"', function() {
    assert(this === _tst)
  })

  beforeEach(() => {
    _tst.answer = 42
  })
	
  it('works', () => {
    assert(_tst.answer === 42)
    _tst.skip()
  })
	
  it('works with done', done => {
    assert(_tst.answer === 42)
    done()
  })
	
  it('works with async', async() => {
    _tst.timeout(100)
    await someAsync()
  })
})

The other way(s)

var ctx = require('mocha-lambda')
// 'ctx' is a function that returns mocha 'this' context when called

describe('mocha-lambda', () => {
  it('provides "this" as "_tst"', function() {
    assert(this === ctx())
  })

  beforeEach(() => {
    ctx().answer = 42
  })
	
  it('works', () => {
    assert(_tst.answer === 42)
    ctx().skip()
  })
})

// 'ctx()' can also change name of global
ctx('mo')
describe('mocha-lambda', () => {
  it('provides "this" as "_tst"', function() {
    assert(this === mo)
  })

  beforeEach(() => {
    mo.answer = 42
  })
	
  it('works', () => {
    assert(mo.answer === 42)
    mo.skip()
  })
})

If mocha interface needs to be explicitly imported, import from 'mocha-lamba' rather than 'mocha' (if this project is merged into mocha, this wont be required)

import ctx, {describe as d, it as i, beforeEach as b} from 'mocha-lambda'
ctx('t')
d('mocha-lambda', () => {
  i('provides "this" as "_tst"', function() {
    assert(this === t)
  })

  b(() => {
    t.answer = 42
  })
	
  i('works', () => {
    assert(mo.answer === 42)
    t.skip()
  })
})

About

Provide mocha 'this' context to lambda functions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published