From 49e7f0c43b85f436ab65635a44d7bdf1673c8ced Mon Sep 17 00:00:00 2001 From: Nathan Bridgewater Date: Thu, 23 Nov 2023 20:01:41 -0600 Subject: [PATCH] Helps when I update unit tests. Renamed shutdown to close(). Duh... --- demo/src/bin/web.mts | 2 +- package/README.md | 2 +- package/src/index.mts | 2 +- package/src/index.test.mts | 66 ++++++++++++++++++++++---------------- 4 files changed, 41 insertions(+), 31 deletions(-) diff --git a/demo/src/bin/web.mts b/demo/src/bin/web.mts index 3301249..8f8159c 100755 --- a/demo/src/bin/web.mts +++ b/demo/src/bin/web.mts @@ -87,7 +87,7 @@ function onListening() { function shutdownHandler(_sig: any, _n: any) { if (server != null) server.close() - store.shutdown() + store.close() } process.on('SIGTERM', shutdownHandler) diff --git a/package/README.md b/package/README.md index e2076a3..a323a09 100644 --- a/package/README.md +++ b/package/README.md @@ -64,7 +64,7 @@ import { store as mongoStore } from './store.mjs' function shutdownHandler(_sig: any, _n: any) { if (server != null) server.close() - mongoStore.shutdown() + mongoStore.close() } process.on('SIGTERM', shutdownHandler) diff --git a/package/src/index.mts b/package/src/index.mts index 2e3ef2e..845b4d2 100644 --- a/package/src/index.mts +++ b/package/src/index.mts @@ -93,7 +93,7 @@ export class MongoSessionStore extends Store { /** * Closes the MongoDB connection. */ - public shutdown() { + public close() { if (this.connected) { this.emit('info', 'Closing MongoDB connection to session store.') this.client.close() diff --git a/package/src/index.test.mts b/package/src/index.test.mts index 23f9d80..b3aa647 100644 --- a/package/src/index.test.mts +++ b/package/src/index.test.mts @@ -19,6 +19,10 @@ vi.mock('mongodb', async () => { return Promise.resolve() } + close() { + + } + db() { return { collection() { @@ -44,25 +48,28 @@ describe('MongoSessionStore', () => { createIndexSpy.mockClear() }) it('should connect on construction with defaults', async () => { + const spyInfo = vi.fn() + const spyError = vi.fn() const store = new mongoSessionStore.MongoSessionStore() - let error: any = null; let info: any = null - store.on('error', (err: any) => { error = err }) - store.on('info', (message: string) => { info = message }) + store.on('info', spyInfo) + store.on('error', spyError) - await vi.waitUntil( - () => info != null || error != null, - { timeout: 1000 } - ) - expect(error).not.to.be.ok - expect(constSpy).toBeCalledWith('mongodb://localhost:27017/express_sessions') - expect(createIndexSpy).toBeCalledWith({ expires: 1 }, { expireAfterSeconds: 0 }) - expect(info).to.equal('Connected to MongoDB') - expect(store.collectionName).to.equal('sessions') - expect(store.prefix).to.equal('') - expect(store.ttl).to.equal(86400000) + await vi.waitFor(() => { + expect(constSpy).toBeCalledWith('mongodb://localhost:27017/express_sessions') + expect(connectSpy).toHaveBeenCalled() + expect(createIndexSpy).toBeCalledWith({ expires: 1 }, { expireAfterSeconds: 0 }) + expect(store.collectionName).to.equal('sessions') + expect(store.prefix).to.equal('') + expect(store.ttl).to.equal(86400000) + expect(spyError).not.toHaveBeenCalled() + expect(spyInfo).toHaveBeenCalledWith('Connected to MongoDB for session store.') + }) }) it('should connect on construction', async () => { + const spyInfo = vi.fn() + const spyError = vi.fn() + const store = new mongoSessionStore.MongoSessionStore({ uri: 'mongodb://localhost:27018/test', collection: 'sessions2', @@ -70,20 +77,23 @@ describe('MongoSessionStore', () => { prefix: '2', createTTLIndex: false }) - let error: any = null; let info: any = null - store.on('error', (err: any) => { error = err }) - store.on('info', (message: string) => { info = message }) + store.on('error', spyError) + store.on('info', spyInfo) - await vi.waitUntil( - () => info != null || error != null, - { timeout: 1000 } - ) - expect(error).not.to.be.ok - expect(constSpy).toBeCalledWith('mongodb://localhost:27018/test') - expect(createIndexSpy).not.toBeCalledWith({ expires: 1 }, { expireAfterSeconds: 0 }) - expect(info).to.equal('Connected to MongoDB') - expect(store.collectionName).to.equal('sessions2') - expect(store.prefix).to.equal('2') - expect(store.ttl).to.equal(1209600000) + await vi.waitFor(() => { + expect(constSpy).toBeCalledWith('mongodb://localhost:27018/test') + expect(createIndexSpy).not.toBeCalledWith({ expires: 1 }, { expireAfterSeconds: 0 }) + expect(store.collectionName).to.equal('sessions2') + expect(store.prefix).to.equal('2') + expect(store.ttl).to.equal(1209600000) + expect(spyError).not.toHaveBeenCalled() + expect(connectSpy).toHaveBeenCalled() + expect(spyInfo).toHaveBeenCalledWith('Connected to MongoDB for session store.') + }) + spyInfo.mockClear() + store.close() + await vi.waitFor(() => { + expect(spyInfo).toHaveBeenCalledWith('Closing MongoDB connection to session store.') + }) }) })