Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including dhruv with all npm packages installed. Try it out:

var dhruv = require("dhruv")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

dhruv v1.0.3

Writing tests by redacting instead of mocking.

Dhruv

Writing tests by redacting instead of mocking.

Simple

Parse Parse the file. Find Find the node you want to test. CallWith Call with the arguments.

\\ Calculator.js
const sum = (a, b) => a + b

\\ Calculator.test.js
const parsed = parseFn(`${__dirname}/calculator.js`)

describe('sum()', () => {
  it('should return the sum of two numbers', () => {
    let a = parsed.find('sum').callWith(1, 2).result
    expect(a).to.equal(3)
  });
})

Throws

To test something that throws.

\\ Thrower.js
const throwUndefined = (bracket) => {
  let foo
  return foo.bar
}

\\ Thrower.test.js
const parsed = parseFn(`${__dirname}/thrower.js`)

describe('throwSomething()', () => {
  it('should throw', () => {
    let ex = parsed
      .find('throwSomething')
      .fold('foo', `undefined`)
      .callWith(5)
      .exception
    expect(!!ex).to.equal(true)
  })
})

Async

To test something async.

// SumAsync.js
const sumAsync = async (a, b) => {
  let foo = await new Promise((resolve, reject) => {
    setTimeout(() => resolve(a + b), 500)
  })
  return foo
}

// SumAsync.test.js
const parsed = parseFn(`${__dirname}/async.js`)

describe('sumAsync()', () => {
  it('should return the sumAsync of two numbers', async () => {
    let a = await parsed
      .find('sumAsync')
      .callWith(5, 7)
      .result
    expect(a).to.equal(12)
  })
})

Destroy

This destroys the Call Expression that starts with the key.

// Handler.js
const handler = (req, res) => {
  res.end()
  return true
}

// Handler.test.js
const parsed = parseFn(`${__dirname}/Handler.js`)

describe('handler()', () => {
  parsed.find('handler')
  it('should return true', () => {
    let foo = handler
      .destroy('res')
      .callWith({}, {})
      .result
    expect(foo).to.equal(true)
  })
})

Provide

This reduces the Variable Declarator's initialization for the key to the the value provided.

// Handler.js
const handler = (req, res) => {
  res.end()
  return true
}

// Handler.test.js
import sinon from 'sinon'
const parsed = parseFn(`${__dirname}/Handler.js`)

describe('handler()', () => {
  parsed.find('handler')
  it('should return true', () => {
    let resStub = sinon.stub()
    let foo = handler
      .provide('res', resStub)
      .callWith({}, {})
      .result
    expect(resStub.called).to.equal(true)
    expect(foo).to.equal(true)
  })
})
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free