Snelle referentie

Tape JavaScript test-harness spiekbriefje

Snelle naslaggids voor Tape: minimalistisch JavaScript test-harness met beweringen, plannen en levenscyclusbeheer.

· Gepubliceerd

Tape is een minimalistisch test-harness voor JavaScript en Node.js dat standaard TAP-uitvoer (Test Anything Protocol) produceert.

Testvoorbeeld en uitvoeringsplan (t.plan)

const test = require('tape')

test('basic mathematical operations', (t) => {
  t.plan(2)

  t.equal(1 + 1, 2, 'addition should compute sum')
  t.notEqual(2 * 3, 5, 'multiplication should not equal five')
})

Overzicht van beweringen (Assertion Methods)

test('assertion reference', (t) => {
  t.plan(10)

  t.equal(actual, expected, 'values are equal (===)')
  t.notEqual(actual, expected, 'values are not equal')

  t.deepEqual(actual, expected, 'equivalent objects using deepEqual')
  t.notDeepEqual(actual, expected, 'objects are not deeply equal')

  t.looseEqual(actual, expected, 'values are loose equal (==)')
  t.notLooseEqual(actual, expected, 'values are not loose equal')

  t.ok(value, 'value is truthy')
  t.notOk(value, 'value is falsy')
  t.error(err, 'error argument is falsy')

  t.throws(fn, /ExpectedError/, 'function throws matching error')
  t.doesNotThrow(fn, 'function runs without throwing')
})

Levenscyclusbeheer van tests (Lifecycle)

test('lifecycle control', (t) => {
  t.comment('informational log message')
  t.pass('explicit pass')
  t.fail('explicit failure')
  t.timeoutAfter(2000)
  t.skip('skipped assertion')
  t.end(err) // end test asynchronously
})
test.only('run only this focused test', (t) => {
  t.plan(1)
  t.ok(true)
})

Gerelateerde spiekbriefjes

Verwante onderwerpen