-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathpromise.js
97 lines (78 loc) · 2.66 KB
/
promise.js
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
'use strict'
const { expect } = require('chai')
const semver = require('semver')
const { storage } = require('../../../datadog-core')
const agent = require('../../../dd-trace/test/plugins/agent')
module.exports = (name, factory, versionRange) => {
describe('Instrumentation', () => {
let Promise
describe(name, () => {
withVersions(name, name, version => {
if (versionRange && !semver.intersects(version, versionRange)) return
before(() => {
return agent.load(name)
})
after(() => {
return agent.close({ ritmReset: false })
})
beforeEach(() => {
const moduleExports = require(`../../../../versions/${name}@${version}`, {}).get()
Promise = factory ? factory(moduleExports) : moduleExports
})
it('should run the then() callbacks in the context where then() was called', () => {
const store = 'store'
let promise = new Promise((resolve, reject) => {
setImmediate(() => {
storage('legacy').run('promise', () => {
resolve()
})
})
})
storage('legacy').run(store, () => {
for (let i = 0; i < promise.then.length; i++) {
const args = new Array(i + 1)
args[i] = () => {
expect(storage('legacy').getStore()).to.equal(store)
}
promise = promise.then.apply(promise, args)
}
})
return promise
})
it('should run the catch() callback in the context where catch() was called', () => {
const store = storage('legacy').getStore()
let promise = new Promise((resolve, reject) => {
setImmediate(() => {
storage('legacy').run('promise', () => {
reject(new Error())
})
})
})
storage('legacy').run(store, () => {
promise = promise
.catch(err => {
throw err
})
.catch(() => {
expect(storage('legacy').getStore()).to.equal(store)
})
})
return promise
})
it('should allow to run without a scope if not available when calling then()', () => {
storage('legacy').run(null, () => {
const promise = new Promise((resolve, reject) => {
setImmediate(() => {
resolve()
})
})
return promise
.then(() => {
expect(storage('legacy').getStore()).to.be.null
})
})
})
})
})
})
}