Skip to content

Latest commit

 

History

History
160 lines (113 loc) · 2.93 KB

api.md

File metadata and controls

160 lines (113 loc) · 2.93 KB
title type order
API
v0.2.2
2

Helpers

The sazerac module exports 3 helper functions:

import { test, given, forCases } from 'sazerac'

test(testFn, definerFn)

Defines test cases for a function

  • Arguments:

    • {Function} testFn - a function to test
    • {Function} definerFn - a function that defines test cases for testFn
  • Example:

test(mySumFunction, () => {
  // test cases for mySumFunction defined here...
})

given([...args])

Defines the functional arguments for a test case

  • Arguments:

    • {...object} args - arguments that will be passed to the function being tested
  • Returns:

    • {TestCase}
  • Example:

test(mySumFunction, () => {
  var testCase = given(1, 2, 3)
})

forCases(...testCases)

Groups multiple test case objects into a collection

  • Arguments:

    • {...TestCase | Array<TestCase>} testCases
  • Returns:

    • {TestCaseCollection}
  • Example:

test(isPrime, () => {
  var primeTestCases = [2, 3, 5, 7].map(given)
  var testCaseCollection = forCases(primeTestCases)
})

TestCase | TestCaseCollection

The TestCase and TestCaseCollection objects have an identical API:

expect(value, [message])

Defines the expected return value for the test case. Uses chai assert.deepEqual() to assert that the expected return value equals the actual return value.

  • Arguments:

    • {object} value - expected return value
    • {string} message - describes the test case expectation
  • Returns:

    • {TestCase | TestCaseCollection}
  • Example:

test(mySumFunction, () => {
  given(1, 2, 3).expect(6)
  given(4, 5).expect(9, 'should equal 9')
})

describe(message)

Defines a describe message for the test case.

  • Arguments:

    • {string} message
  • Returns:

    • {TestCase | TestCaseCollection}
  • Example:

test(mySumFunction, () => {
  given(1, 2, 3)
    .expect(6)
    .describe('sum of 1, 2, and 3')
})

should(message)

Defines a message to describe the test case assertion. This message is passed to the it function when the test is executed.

  • Arguments:

    • {string} message
  • Returns:

    • {TestCase | TestCaseCollection}
  • Example:

test(mySumFunction, () => {
  given(1, 2, 3)
    .expect(6)
    .should('should equal 6')
})

assert(message, assertFn)

Defines a custom assertion for the test case

  • Arguments:

    • {string} message - describes the assertion
    • {Function} assertFn - function for defining the assertion. Receives the actual return value of the function being tested as its only argument.
  • Returns:

    • {TestCase | TestCaseCollection}
  • Example:

test(mySumFunction, () => {
  given(1, 2, 3)
    .assert(
      'should set the value of the sum property to 6',
      (actualReturnValue) => {
        // do anything with actualReturnValue here,
        // for example:
        assert.equal(actualReturnValue.sum, 6)
      }
    )
})