クイックリファレンス

Tape チートシート(極小 JavaScript TAP テストハーネス)

TAP 準拠の極小 JavaScript テストハーネス Tape のクイックリファレンス。t.plan、アサーション一覧、ライフサイクル制御、非同期 t.end を網羅。

· 公開日

Tape は、標準的な TAP(Test Anything Protocol)を出力する、シンプルで極小な Node.js / JavaScript 向けテストハーネスです。

基本テスト例とアサーション計画(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')
})

主要アサーションメソッド(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')
})

ライフサイクル制御と単一テスト実行

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)
})

関連するチートシート

関連テーマ