task-tape

1.2.2 • Public • Published

task-tape

Tape with gulpish task support.

version status dependencies devDependencies

Usage

Global installation

npm install -g task-tape
 

Go to your project directory, which has tape installed already:

task-tape test/*.js
 

Or with babel (please install babel first):

task-tape --babel test/*.js
 

Local installation

npm install --save-dev task-tape tape
 

Require directly

var test = require('task-tape')
 

Hook require

require('task-tape')
var test = require('tape')
 

With gulp-tape

gulp.task('test', function test() {
  require('task-tape')
  var tape = require('gulp-tape')
  var reporter = require('tap-spec')
  return gulp.src('test/*.js')
    .pipe(tape({
      reporter: reporter(),
    }))
})
 

npm test

In package.json:

{
  "scripts": {
    "test": "task-tape test/*.js"
  }
}
 

To support babel:

{
  "scripts": {
    "test": "task-tape --babel test/*.js"
  }
}
 

Example

End the test

t.end

import test from 'tape'
 
test('sync', (t) => {
  t.ok(true)
  t.ok(true)
  t.end()
})
 
test('async', (t) => {
  process.nextTick(() => {
    t.ok(true)
    t.end()
  })
  t.ok(true)
})
 

t.plan

import test from 'tape'
 
test('async', (t) => {
  t.plan(2)
  process.nextTick(() => {
    t.ok(true)
  })
  t.ok(true)
})
 

Gulpish callback

import test from 'tape'
import concat from 'concat-stream'
 
// accept a callback
test('callback', (t, cb) => {
  process.nextTick(() => {
    t.ok(true)
    cb()
  })
})
 
// return a promise
test('promise', (t) => {
  return new Promise((rs) => {
    process.nextTick(() => {
      t.ok(true)
      rs()
    })
  })
})
 
// return a stream
test('stream', (t) => {
  let s = concat({ encoding: 'object' }, (rows) => {
    t.same(rows, [ { x: 1 }, { y: 2 }, { z: 3 } ])
  })
  s.write({ x: 1 })
  s.write({ y: 2 })
  process.nextTick(() => {
    s.end({ z: 3 })
  })
  return s
})
 

Gulpish callbacks in sequence

import test from 'tape'
import concat from 'concat-stream'
import gulp from 'gulp'
import fs from 'fs'
import del from 'del'
 
// Run tasks in sequence.
test('tasks in sequence', function(t) {
  // t.plan(7)
 
  let rows = []
 
  // clean
  t.task(() => {
    t.ok(true)
    return del('build')
  })
 
  // delay
  t.task((cb) => {
    t.ok(true)
    setTimeout(() => {
      cb()
    }, 100)
  })
 
  // collect rows
  t.task(() => {
    t.ok(true)
    let stream = thr.obj()
 
    stream.write({ index: 0 })
 
    process.nextTick(() => {
      stream.end({ index: 1 })
    })
 
    return stream.pipe(concat({ encoding: 'object' }, function (rs) {
      rows = rs.sort((a, b) => {
        return a.index < b.index ? 1 : -1
      })
    }))
  })
 
  // stringify rows
  t.task(() => {
    t.same(rows, [ { index: 1 }, { index: 0 } ])
    let ws = fs.createWriteStream('rows.json')
 
    process.nextTick(() => {
      ws.end(JSON.stringify(rows, null, 2))
    })
 
    return ws
  })
 
  // build
  t.task(() => {
    t.ok(true)
    return gulp.src('rows.json')
      .pipe(gulp.dest('build'))
  })
 
  // check
  t.task(() => {
    t.equal(
      fs.readFileSync('rows.json', 'utf8'),
      fs.readFileSync('build/rows.json', 'utf8')
    )
  })
 
  // clean
  t.task(() => {
    t.ok(true)
    return del('rows.json')
  })
})
 
 

Package Sidebar

Install

npm i task-tape

Weekly Downloads

1

Version

1.2.2

License

MIT

Last publish

Collaborators

  • zoubin