File: mocha.js

package info (click to toggle)
node-tap 12.0.1%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,996 kB
  • sloc: javascript: 13,674; sh: 75; makefile: 73
file content (132 lines) | stat: -rw-r--r-- 3,559 bytes parent folder | download | duplicates (2)
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
131
132
'use strict'
const mocha = require('../lib/mocha.js')
const assert = require('assert')

mocha.describe('globals', () => {
  let beforeEaches = 0
  mocha.beforeEach(() => beforeEaches++)

  mocha.beforeEach(cb => setTimeout(cb))
  mocha.beforeEach(() => new Promise(r => r()))

  let afterEaches = 0
  mocha.afterEach(() => afterEaches++)

  // test that afterEach is happening correct number
  // of times.
  let eachExpect = 0
  mocha.afterEach(() => new Promise(res => {
    eachExpect ++
    assert.equal(beforeEaches, eachExpect, 'before')
    assert.equal(afterEaches, eachExpect, 'after')
    res()
  }))

  mocha.it('has no describe', () =>
    assert.equal(global.describe, undefined))

  mocha.it('is ok running deglobal() first', () => {
    mocha.deglobal()
    assert.equal(global.describe, undefined)
  })

  mocha.it('has describe after call', () => {
    mocha.global()
    mocha.global()
    assert.equal(global.describe, mocha.describe)
  })

  mocha.it('has no describe after deglobal', () => {
    deglobal()
    assert.equal(global.describe, undefined)
  })

  mocha.it('escape to tap', function () {
    const t = this
    t.test('should not get a beforeEach', t =>
      t.test('or an after each', t => {
        t.pass('this is fine')
        t.end()
      }))
  })

  // at this point, beforeEach has been called
  // 1 more time than afterEach
  mocha.it('called beforeEach/afterEach', () =>
     new Promise((resolve) => {
       assert.equal(beforeEaches, eachExpect + 1)
       assert.equal(afterEaches, eachExpect)
       resolve()
     }))
})

if (process.version.match(/v[0-9]\./)) {
  assert.throws(_ => mocha.after(),
    'cannot call "after" outside of describe()')
  assert.throws(_ => mocha.before(),
    'cannot call "before" outside of describe()')
} else {
  assert.throws(_ => mocha.after(),
    new Error('cannot call "after" outside of describe()'))
  assert.throws(_ => mocha.before(),
    new Error('cannot call "before" outside of describe()'))
}

let calledAfter = false
let calledBefore = false
mocha.describe(function after_and_before () {
  mocha.before((cb) => {
    assert.equal(calledBefore, false)
    calledBefore = true
    setTimeout(cb)
  })
  mocha.before('named before', () => new Promise(r => {
    assert.equal(calledBefore, true)
    r()
  }))

  mocha.after(() => {
    assert.equal(calledAfter, false)
    calledAfter = true
  })

  mocha.after('named after', () => {
    assert.equal(calledAfter, true)
  })

  mocha.after(function named_after () {
    assert.equal(calledAfter, true)
  })

  mocha.it(function this_is_fine () {})
  mocha.it(() => {})
  mocha.it(cb => cb())
})
mocha.describe('after after', function () {
  this.test.plan(1)
  mocha.it('should have called after fn', () =>
    assert.equal(calledAfter, true))
})

mocha.describe('todo, skip, and failure', () => {
  let calledTodoFn = false
  let calledSkipFn = false
  const it = mocha.it
  mocha.describe('expect todo and skip', function () {
    /* istanbul ignore next */
    it.todo('expected todo', () => calledTodoFn = true)
    /* istanbul ignore next */
    it.skip('expected skip', () => calledSkipFn = true)
  }, { silent: true })
  it('expected fail from cb(er)', cb => {
    cb(new Error('expected failure'))
  }, { expectFail: true })
  it('did not call skip/todo functions', () => {
    assert.equal(calledTodoFn, false)
    assert.equal(calledSkipFn, false)
  })
})

mocha.describe('expected before failure', () =>
  mocha.before('expect failure', (cb) =>
    cb(new Error('expected')), { expectFail : true }))