-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
158 lines (124 loc) · 5.33 KB
/
index.test.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const mongoose = require('mongoose');
const sinon = require('sinon');
const assert = require('assert');
const scheduler = require('node-schedule');
const schema = new mongoose.Schema({
text: { type: String, required: true },
status: { type: String, required: true },
scheduleRun: { type: Date, required: true }
}, {
timestamps: true
});
const Model = mongoose.model('Test', schema);
describe('test', async () => {
before(async () => {
await mongoose.connect('mongodb://localhost:27017/mongoose_sinon_faketimer', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
mongoose.set('useFindAndModify', false);
await mongoose.connection.dropDatabase();
})
// Run this on 5.10.4
it('mongoose 5.10.4: working fine', async () => {
const now = new Date();
const later = new Date(now.getTime() + 60000);
const clock = sinon.useFakeTimers(now);
const newDoc = {
text: 'name',
status: 'old',
scheduleRun: later,
}
const cDoc = await Model.create(newDoc);
scheduler.scheduleJob(newDoc.scheduleRun, async (fireDate) => {
console.log('Running @ ', fireDate);
await Model.findByIdAndUpdate(cDoc._id, { status: 'scheduled' });
});
const oldDoc = await Model.find();
console.log('Current time', new Date());
console.log('Before', oldDoc, new Date());
assert.strictEqual(oldDoc[0].status, 'old', 'status should be old');
clock.tick(60000);
const updatedDoc = await Model.find();
console.log('Next tick time', new Date());
console.log('After', updatedDoc, new Date());
assert.strictEqual(updatedDoc[0].status, 'scheduled', 'status should be old');
});
// Run this on 5.10.5
it.skip('mongoose 5.10.5: not working', async () => {
const now = new Date();
const later = new Date(now.getTime() + 60000);
const clock = sinon.useFakeTimers(now);
// const clock = sinon.useFakeTimers({now, toFake: ['Date', 'setTimeout']});
const newDoc = {
text: 'name',
status: 'old',
scheduleRun: later,
}
const cDoc = await Model.create(newDoc);
scheduler.scheduleJob(newDoc.scheduleRun, async (fireDate) => {
console.log('Running @ ', fireDate);
await Model.findByIdAndUpdate(cDoc._id, { status: 'scheduled' });
});
const oldDoc = await Model.find();
console.log('Current time', new Date());
console.log('Before', oldDoc, new Date());
assert.strictEqual(oldDoc[0].status, 'old', 'status should be old');
clock.tick(60000);
const updatedDoc = await Model.find();
console.log('Next tick time', new Date());
console.log('After', updatedDoc, new Date());
assert.strictEqual(updatedDoc[0].status, 'scheduled', 'status should be old');
});
it.skip('mongoose 5.10.5: switch to toFake w/ only Date does not work', async () => {
const now = new Date();
const later = new Date(now.getTime() + 60000);
// const clock = sinon.useFakeTimers(now);
const clock = sinon.useFakeTimers({now, toFake: ['Date']});
const newDoc = {
text: 'name',
status: 'old',
scheduleRun: later,
}
const cDoc = await Model.create(newDoc);
scheduler.scheduleJob(newDoc.scheduleRun, async (fireDate) => {
console.log('Running @ ', fireDate);
await Model.findByIdAndUpdate(cDoc._id, { status: 'scheduled' });
});
const oldDoc = await Model.find();
console.log('Current time', new Date());
console.log('Before', oldDoc, new Date());
assert.strictEqual(oldDoc[0].status, 'old', 'status should be old');
clock.tick(60000);
const updatedDoc = await Model.find();
console.log('Next tick time', new Date());
console.log('After', updatedDoc, new Date());
assert.strictEqual(updatedDoc[0].status, 'scheduled', 'status should be old');
});
// Run this on 5.10.5
it.skip('mongoose 5.10.5: switch to toFake w/ setTimeout to get it working', async () => {
const now = new Date();
const later = new Date(now.getTime() + 60000);
// const clock = sinon.useFakeTimers(now);
const clock = sinon.useFakeTimers({now, toFake: ['Date', 'setTimeout']});
const newDoc = {
text: 'name',
status: 'old',
scheduleRun: later,
}
const cDoc = await Model.create(newDoc);
scheduler.scheduleJob(newDoc.scheduleRun, async (fireDate) => {
console.log('Running @ ', fireDate);
await Model.findByIdAndUpdate(cDoc._id, { status: 'scheduled' });
});
const oldDoc = await Model.find();
console.log('Current time', new Date());
console.log('Before', oldDoc, new Date());
assert.strictEqual(oldDoc[0].status, 'old', 'status should be old');
clock.tick(60000);
const updatedDoc = await Model.find();
console.log('Next tick time', new Date());
console.log('After', updatedDoc, new Date());
assert.strictEqual(updatedDoc[0].status, 'scheduled', 'status should be old');
});
})