npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mocha-ctx

v1.0.0-a.2

Published

Enhanced control of mocha context

Downloads

159

Readme

Features

  • Backwards compatible (explicit opt in).
  • Access test context inside lambda functions without being passed parameter.
  • Every test gets its own context, sharing parent context, hiding private context.
  • Explicity declared contextual properties and functions, so lower contexts can set higher shared properties.
  • Contextualized 'global' || 'window', with stronger access and use detection on top of leak detection.

Status

This is a prototype / experimental version of these features, that can be easily 'required' into tests. The purpose of a separate module is to ease exploring the approach and api. Ideally the features will be integrated directly into mocha after feedback.

Use with node and bundlers

npm i mocha-ctx

test.js

require('mocha-ctx')
// the context() global is now overloaded

describe('down with lambda discrimination!!! (and this and that and self)', () => {
  it('provides "this" via "context()"', function() => {
    // context() can still be used as an alias for 'describe(title, fn)', but when
    // passed no arguments, it now returns the 'this' context. ('describe' 
    // will fail however; i.e. context !== describe, but now delegates to it)
    assert(this === context())
    this.immediateContextProp = 1
  })

  it('then lets lambdas effectively get to "this" when needed', () => {
    // NOTE, we're using context(), but semantic of 'this' hasn't changed yet;
    // i.e. tests don't yet have their own context; see what follows
    assert(context().immediateContextProp === 1)
    context().timeout(-42)
  })
})

describe('explicit context', () => {
  before(() => {
    // when 'context()' called with an object arg like 'context({props: values})', 
    // the object is used to explicitly define the shared properties, 
    // functions, and globals (desribed shortly) of the context, and also
    // activates context-per-test semantics for all contained tests
    context({
      outerProperty: 1,
      outerCounter: 0,
      sharedFn: function(offset) {return this.outerProperty + offset},
      sharedFn2: onset => context().outerCounter - onset // of greater things
    })
  })

  beforeEach(() => {
    // context() can only be passed a context-defining object when called
    // from within a 'before' hook (or 'beforeAll', 'suiteSetup'). the 
    // following throws
    context({notGunnaHappen: true})
    // because we're in a 'beforeEach' hook, and the same will throw when 
    // called with an object in any other.. uhm.. context (i.e. in after, afterEach, it)
    
    // however, context({props: values}) can be called multiple times within
    // one or more 'before' hooks, with later calls overiding earlier ones
    // made within the same context (i.e. within the same callback 
    // passed to 'describe()')
  })

  describe('layer 1 context', () => {
    beforeEach(() => {
      context().outerCounter++
    })

    describe('layer 2 context', () => {
      it('can set a property on ancestor context that ancestor function sees', () => {
        context().outerProperty = 100
        assert(context().sharedFn(0) === 100)
      })

      it('should see same value of higher context properties accross tests', () => {
        assert(context().outerProperty === 100)
      })

      it('provides each test its own context', () => {
        context().testProperty = 10
        assert(context().testProperty === 10)
      })

      it('then prevents other tests from seeing each others private context', () => {
        assert(context().testProperty === undefined)
      })

      it('should have made layer 1 count to 5 using outer property context', () => {
        assert(context().outerCounter === 5)
      })
    })
  })
})

describe('globals', () => {
  before(() => {
    context({
      // 'globals' is a distinguished name here, and therefore cant be used
      // generally as a name of a property on a context.
      
      globals: {
        // by setting 'neverAccess' to 'undefined', we're saying this 
        // global 'neverAccess' should never be accessed nor set. when 
        // anything does within this context, it fails. handy as it detects 
        // attempts to merely use a global somewhere.
        neverAccess: undefined,

        // this is read only in this context. any sets fail. useful
        // for mocking globals when necessary
        readOnly: 'read only',
        // NOTE!! we save and restore globals, but not deeply; don't forget.

        // uhhh... uhmmm...(clearing throat)... *cough*... "prototype!"
        // the 'globals' within 'globals', are the, uh, globals to ignore in
        // this context; i.e. to whitelist
        globals: {
          ignored: 'with this init value in this context',
          stillIgnored: undefined // but don't init value; it will be what it is
          // NOTE!! again, we save and restore globals, but not deeply; don't forget.
        }
      }
    })

    // any other globals set in tests, that are not specified above, are automatically 
    // considered leaks that will cause failure regardless of --check-leaks flag. in
    // other words when using mocha-context, you must account for all globals; you
    // are 'using' mocha-context whenever you call 'context({props: values})' in a 'before'
  })

  after(() => {
    // at this point, the globals are restored to the values they had
    // prior to executing tests within this context. keep in mind however
    // globals are saved and restored, but not deeply; we don't clone
    // objects deeply or anything
  })
})



//***** other interfaces... 

// all can explicitly import
var context = require('mocha-ctx')


// or get from globals
// === tdd ===
var context = suite.context
suite('suit', () => {
  suiteSetup(() => {
    context({givesMeaning: 'to EVERYTHING'})
  })
})


// === qunit
var context = suite.context
suite('of law')

before(() => {
  context({seems: 'a little far fetched...'})
})



// === exports
require('mocha-context')
module.exports = {
  'sumpin sweet': {
    before: function() {
      context({
        andTasty: 'like apple pie'
      })
    }
  }
}