forked from jquense/yup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-setup.js
55 lines (42 loc) · 1.3 KB
/
test-setup.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
const { SynchronousPromise } = require('synchronous-promise');
global.chai = require('chai');
global.sinon = require('sinon');
global.chai.use(require('sinon-chai'));
global.chai.use(require('chai-as-promised'));
global.chai.use(require('dirty-chai'));
global.expect = global.chai.expect;
global.chai.should();
// WTF???
Object.defineProperty(
Promise.prototype,
'should',
Object.getOwnPropertyDescriptor(Object.prototype, 'should'),
);
global.TestHelpers = require('./test/helpers');
if (global.YUP_USE_SYNC) {
const { BaseSchema } = require('./src'); // eslint-disable-line global-require
const { validate } = BaseSchema.prototype;
BaseSchema.prototype.validate = function (value, options = {}, maybeCb) {
let run = false;
options.sync = true;
if (maybeCb) {
return validate.call(this, value, options, (...args) => {
if (run) {
return maybeCb(new Error('Did not execute synchronously'));
}
maybeCb(...args);
});
}
const result = new SynchronousPromise((resolve, reject) => {
validate.call(this, value, options, (err, value) => {
if (run) {
throw new Error('Did not execute synchronously');
}
if (err) reject(err);
else resolve(value);
});
});
run = true;
return result;
};
}