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-combo

v0.1.0

Published

A testing framework that will generate mocha test cases based on config

Downloads

132

Readme

mocha-combo

https://github.com/Tedc0819/mocha-combo

mocha-combo is a testing framework based on mocha. It generates test cases by your configuration and input. Testing with high coverage is now easy to achieve.

Testing is basically done against a test point / method. There are lots of factors / arguments that will affect the result. Instead of defining the testing condition as a whole, we can define each condition of each factor. With this new kind of definition, we can easily generate all combintaion. There will be no need to write it one by one.

This layer actually run on top of mocha. All the phases (like beforeEach) can be set up according to combination of testing factors. The result can be easily divided into success assert and failure assert according to combinations.

You can read /examples for more details. https://github.com/Tedc0819/mocha-combo/tree/master/examples

You can also read the API doc in src file https://github.com/Tedc0819/mocha-combo/blob/master/src/MochaCombo.js

Release note

0.1.0

  • using cartesian-product to replace getCombinations
  • using async-await

0.0.9

  • The framework should still run with argument [] even there is no args and argTypes

basic example


const MochaCombo = require('../../../index.js');
const Calculator = require('../../models/Calculator.js');
const assert = require('chai').assert;

class TestSuite extends MochaCombo {

  constructor() {

    super()

    this.methodName = 'Calculator.add'

    this.args = ['value']

    this.argTypes = {
      value: ['integer', 'integerStr', 'string']
    }

  }

  before(test, combination) {}

  beforeEach(test, combination) {

    return this.runTest(test, combination);

  }

  after(test, combination) {}

  afterEach(test, combination) {}

  only(combination) {}

  skip(combination) {}

  stub(test, combination) {}

  setFixtures(test, combination) {}

  getArgValues(test, combination, arg, argType) {

    let argValues = {
      value: {
        'integer': 1,
        'integerStr': "1111",
        'string': "fdfafds"
      }
    }

    return argValues[arg][argType];

  }

  testMethod(test, combination, argsValues) {

    let calculator = new Calculator;
    // you can attach any varible to test. so that you can access it when you assert it
    // test equals to 'this' in moche test function
    test.calculator = calculator;

    return calculator.add(...argsValues);
  }

  clearData(test, combination) {}

  shouldSuccess(combination) {

    let [ value ] = combination;

    return value == 'integer';

  }

  successAssert(combination) {

    it('should work', function() {

      assert.equal(this.res, 1);

    })

  }

  failureAssert(combination) {

    it('should not work', function() {

      assert(this.res instanceof Error);

    })

  }
}

let testSuite = new TestSuite;
testSuite.run();

This should generate mocha test result like this.

Calculator.add(value)
  success - [integer]
    ✓ should work
  failure - [integerStr]
    ✓ should not work
  failure - [string]
    ✓ should not work

This example demonstrate some advanced features.

  1. the 'only' function
  2. the 'skip' function
  3. the 'extraCombinations' function
  4. the case that the arguments is not exactly the argument of the method

const MochaCombo = require('../../../index.js');
const Calculator = require('../../models/Calculator.js');
const assert = require('chai').assert;

class TestSuite extends MochaCombo {

  constructor() {

    super()

    this.methodName = 'Calculator.add'

    this.args = ['currentValue', 'value']

    this.argTypes = {
      currentValue: ['zero', 'five', 'six'], // just example. it can be 'highestCapableValue', blah blah blah
      value: ['integer', 'integerStr', 'string']
    }

  }

  before(test, combination) {}

  beforeEach(test, combination) {

    return this.runTest(test, combination);

  }

  after(test, combination) {}

  afterEach(test, combination) {}

  only(combination) {

    let [currentValue, value] = combination;

    return currentValue.match(/zero|five/);
  }

  skip(combination) {

    let [currentValue, value] = combination;

    return value.match(/string/);

  }

  stub(test, combination) {}

  setFixtures(test, combination) {

    let calculator = new Calculator;
    test.calculator = calculator;

  }

  getArgValues(test, combination, arg, argType) {

    let argValues = {
      currentValue: {
        zero: 0,
        five: 5,
        six: 6
      },
      value: {
        'integer': 1,
        'integerStr': "1111",
        'string': "fdfafds",
        'two': 2
      }
    }

    return argValues[arg][argType];

  }

  extraCombinations(test) {

    return [
      ['zero', 'two']
    ];

  }

  testMethod(test, combination, argsValues) {

    test.calculator.currentValue = argsValues[0];

    return test.calculator.add(argsValues[1]);
  }

  clearData(test, combination) {}

  shouldSuccess(combination) {

    let [ currentValue, value ] = combination;

    return value == 'integer' || value == 'two';

  }

  successAssert(combination) {

    it('should work', function() {

      let [ currentValue, value] = this.args; // you can get the args during Assertion

      assert.equal(this.res, currentValue + value);

    })

  }

  failureAssert(combination) {

    it('should not work', function() {

      assert(this.res instanceof Error);

    })

  }
}

let testSuite = new TestSuite;
testSuite.run();

This should give result like this.

Calculator.add(currentValue,value)
  success - [zero, integer]
    ✓ should work
  failure - [zero, integerStr]
    ✓ should not work
  failure - [zero, string]
    - should not work
  success - [five, integer]
    ✓ should work
  failure - [five, integerStr]
    ✓ should not work
  failure - [five, string]
    - should not work
  success - [zero, two]
    ✓ should work