Skip to content

Commit fe46a0c

Browse files
committed
feat: Mock the time when testing expiresAt
1 parent c9d4bca commit fe46a0c

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

test/test.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ describe("DynamoDBStore", () => {
4646
const sessionId = Math.random().toString();
4747

4848
describe("Instantiation", () => {
49+
let consoleWarnStub;
50+
51+
beforeEach(() => {
52+
consoleWarnStub = sinon.stub(console, 'warn');
53+
})
54+
55+
afterEach(() => {
56+
consoleWarnStub.restore();
57+
})
58+
4959
it("should be able to be created", () => {
5060
store.should.be.an.instanceOf(DynamoDBStore);
5161
});
@@ -69,6 +79,33 @@ describe("DynamoDBStore", () => {
6979
})
7080
.finally(done);
7181
});
82+
<<<<<<< Updated upstream
83+
=======
84+
85+
it("should store a valid expiresIn", () => {
86+
const store = new DynamoDBStore({
87+
table: "sessions-test",
88+
expiresIn: 3600
89+
});
90+
store.expiresIn.should.equal(3600);
91+
});
92+
93+
it("should revert expiresIn to 0 when set to a non-integer", () => {
94+
const store = new DynamoDBStore({
95+
table: "sessions-test",
96+
expiresIn: 1.5
97+
});
98+
store.expiresIn.should.equal(0);
99+
});
100+
101+
it("should revert expiresIn to 0 when set to a negative integer", () => {
102+
const store = new DynamoDBStore({
103+
table: "sessions-test",
104+
expiresIn: -10
105+
});
106+
store.expiresIn.should.equal(0);
107+
});
108+
>>>>>>> Stashed changes
72109
});
73110

74111
describe("Initializing", () => {
@@ -174,6 +211,16 @@ describe("DynamoDBStore", () => {
174211
});
175212

176213
describe("Setting", () => {
214+
let clock;
215+
216+
beforeEach(() => {
217+
clock = sinon.useFakeTimers(1000000000);
218+
})
219+
220+
afterEach(() => {
221+
clock.restore();
222+
})
223+
177224
it("should store data correctly", async () => {
178225
return new Promise((resolve, reject) => {
179226
const name = Math.random().toString();
@@ -239,7 +286,6 @@ describe("DynamoDBStore", () => {
239286
table: tableName,
240287
expiresIn: 1000
241288
});
242-
const beforeTime = Math.floor(Date.now() / 1000);
243289

244290
await new Promise((resolve, reject) => {
245291
storeWithExpiry.set(
@@ -263,8 +309,7 @@ describe("DynamoDBStore", () => {
263309
);
264310

265311
const expiryValue = parseInt(result.Item.expires.N);
266-
const expectedExpiry = beforeTime + 1000;
267-
expiryValue.should.be.approximately(expectedExpiry, 2);
312+
expiryValue.should.equal(1000000 + 1000);
268313
});
269314
});
270315

0 commit comments

Comments
 (0)