File: test.js

package info (click to toggle)
node-domain-browser 1.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 148 kB
  • ctags: 26
  • sloc: makefile: 4; sh: 2
file content (100 lines) | stat: -rw-r--r-- 2,583 bytes parent folder | download
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
/* eslint handle-callback-err:0, no-magic-numbers:0, no-unused-vars:0 */
'use strict'

// Import
const events = require('events')
const equal = require('assert-helpers').equal
const joe = require('joe')
const domain = require('./')

// =====================================
// Tests

joe.describe('domain-browser', function (describe, it) {
	it('should work on throws', function (done) {
		const d = domain.create()
		d.on('error', function (err) {
			equal(err && err.message, 'a thrown error', 'error message')
			done()
		})
		d.run(function () {
			throw new Error('a thrown error')
		})
	})

	it('should be able to add emitters', function (done) {
		const d = domain.create()
		const emitter = new events.EventEmitter()

		d.add(emitter)
		d.on('error', function (err) {
			equal(err && err.message, 'an emitted error', 'error message')
			done()
		})

		emitter.emit('error', new Error('an emitted error'))
	})

	it('should be able to remove emitters', function (done) {
		const emitter = new events.EventEmitter()
		const d = domain.create()
		let domainGotError = false

		d.add(emitter)
		d.on('error', function (err) {
			domainGotError = true
		})

		emitter.on('error', function (err) {
			equal(err && err.message, 'This error should not go to the domain', 'error message')

			// Make sure nothing race condition-y is happening
			setTimeout(function () {
				equal(domainGotError, false, 'no domain error')
				done()
			}, 0)
		})

		d.remove(emitter)
		emitter.emit('error', new Error('This error should not go to the domain'))
	})

	it('bind should work', function (done) {
		const d = domain.create()
		d.on('error', function (err) {
			equal(err && err.message, 'a thrown error', 'error message')
			done()
		})
		d.bind(function (err, a, b) {
			equal(err && err.message, 'a passed error', 'error message')
			equal(a, 2, 'value of a')
			equal(b, 3, 'value of b')
			throw new Error('a thrown error')
		})(new Error('a passed error'), 2, 3)
	})

	it('intercept should work', function (done) {
		const d = domain.create()
		let count = 0
		d.on('error', function (err) {
			if ( count === 0 ) {
				equal(err && err.message, 'a thrown error', 'error message')
			}
			else if ( count === 1 ) {
				equal(err && err.message, 'a passed error', 'error message')
				done()
			}
			count++
		})

		d.intercept(function (a, b) {
			equal(a, 2, 'value of a')
			equal(b, 3, 'value of b')
			throw new Error('a thrown error')
		})(null, 2, 3)

		d.intercept(function (a, b) {
			throw new Error('should never reach here')
		})(new Error('a passed error'), 2, 3)
	})
})