Skip to content

Commit

Permalink
Helps when I update unit tests. Renamed shutdown to close(). Duh...
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanb committed Nov 24, 2023
1 parent 0179a2a commit 49e7f0c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 31 deletions.
2 changes: 1 addition & 1 deletion demo/src/bin/web.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
66 changes: 38 additions & 28 deletions package/src/index.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ vi.mock('mongodb', async () => {
return Promise.resolve()
}

close() {

}

db() {
return {
collection() {
Expand All @@ -44,46 +48,52 @@ 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',
ttl: 60 * 60 * 24 * 14 * 1000, // 2 weeks
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.')
})
})
})

0 comments on commit 49e7f0c

Please sign in to comment.