Skip to content

sqlite client disconnect function does not work, nor does it's test #1413

Closed
@bobhigs

Description

@bobhigs

Describe the bug
@keyv/sqlite does not properly close the client when disconnect is called, it just fetches the function:

this.close = async () => connected.then(database => database.close);

To fix, simply add parentheses: database => database.close().

There is a test for this, but it does not catch the issue because it is implemented incorrectly:

test.it('close connection successfully', async t => {
const keyv = new KeyvSqlite({uri: 'sqlite://test/testdb.sqlite'});
t.expect(await keyv.get('foo')).toBe(undefined);
await keyv.set('foo', 'bar');
t.expect(await keyv.get('foo')).toBe('bar');
await keyv.disconnect();
try {
await keyv.get('foo');
t.expect.fail();
} catch {
t.expect(true).toBeTruthy();
}
});

The await keyv.get('foo'); does not throw, then the t.expect.fail(); does throw, then the catch catches the error and ignores it - so the try catch will just always pass.

One possible solution is replace the try-catch block with:

await t.expect(() => keyv.get('foo')).rejects.toThrow();

How To Reproduce (best to provide workable code or tests!)
The following should throw, it does not:

import Keyv from 'keyv';
import { KeyvSqlite } from '@keyv/sqlite';

const sqliteTest = new Keyv(new KeyvSqlite());

console.log(await sqliteTest.has('key'));
await sqliteTest.disconnect();
console.log(await sqliteTest.has('key'));

If you add the parentheses to close the connection properly, it will then throw.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions