All files / lib suite.js

94.83% Statements 55/58
70% Branches 7/10
100% Functions 10/10
94.83% Lines 55/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130                                        4x 4x 4x   4x 4x       19x 19x 19x   19x   19x       42x 42x   42x   42x   42x   42x   42x       16x   16x   16x       19x 19x 19x 19x   19x     19x 42x 42x 42x 42x 42x     42x   42x 42x 42x   42x   42x         19x   19x 19x 19x   19x 19x       19x   19x 17x       19x       55x 55x 55x 79x     79x     55x 13x         4x  
// suite.js -- a suite of batches of tests for vows
//
// Copyright 2016 fuzzy.ai <evan@fuzzy.ai>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
 
/* jshint esversion: 6 */
 
'use strict'
 
const _ = require('lodash')
const async = require('async')
const debug = require('debug')('vows:suite')
 
const assert = require('./assert')
const Batch = require('./batch')
 
class Suite {
  constructor (description) {
    this.run = this.run.bind(this)
    this.description = description
    assert.isString(this.description, 'Suite description must be a string')
 
    debug(`Suite constructor with description '${this.description}'`)
 
    this.batches = []
  }
 
  addBatch (obj) {
    assert.isObject(obj, 'Argument to addBatch must be an Object')
    assert.equal(_.keys(obj).length, 1, 'Argument to addBatch must have one key')
 
    const title = _.keys(obj)[0]
 
    debug(`Adding batch '${title}' to suite '${this.description}'`)
 
    const batch = new Batch(title, obj[title])
 
    this.batches.push(batch)
 
    return this
  }
 
  export (module) {
    assert.isObject(module, 'Module is not an object')
 
    module.exports = this.run
 
    return this
  }
 
  run (callback) {
    let broken = 0
    let successes = 0
    let failures = 0
    const suite = this
 
    assert(_.isUndefined(callback) || _.isFunction(callback),
      'If defined callback must be a function')
 
    const runBatch = (batch, callback) => {
      assert.isObject(batch, 'batch is not an object')
      assert.instanceOf(batch, Batch, 'batch is not a Batch')
      debug(`Running batch '${batch.title}' from suite '${this.description}'`)
      batch.run([], (err, report) => {
        Iif (err) {
          callback(err)
        } else {
          debug(`Batch ${batch.title} complete`)
 
          broken += report.broken
          successes += report.successes
          failures += report.failures
 
          suite.showReport(report)
 
          callback(null)
        }
      })
    }
 
    debug(`Running ${_.size(this.batches)} batches for suite '${this.description}'`)
 
    console.log()
    console.log(this.description)
    console.log()
 
    async.eachSeries(this.batches, runBatch, (err) => {
      Iif (err) {
        console.error(err)
      }
 
      console.log()
 
      if (_.isFunction(callback)) {
        return callback(null, broken, successes, failures)
      }
    })
 
    return this
  }
 
  showReport (report) {
    const suite = this
    console.log(report.title)
    _.each(report.tests, (value, name) => {
      Iif (_.isString(value)) {
        console.log(`  ${name}: ${value}`)
      } else {
        console.log(`  ${name}: OK`)
      }
    })
    _.each(report.subs, (subReport) => {
      suite.showReport(subReport)
    })
  }
}
 
module.exports = Suite