node.js - Hoe mysql2 `getConnection` te mocken
Ik kan de getConnection-methode van pool in de mysql2-bibliotheek niet correct namaken. Mijn setup is als volgt:
"ts-mockito": "^2.6.1"
"typescript": "^4.8.4"
"mocha": "^10.1.0",
"mysql2": "^2.3.3",
Test :
import { Pool, PoolConnection } from "mysql2/promise";
import { mock, instance, verify, when} from "ts-mockito";
import Service from './'
describe("Conn test", function() {
const connPool = mock<Pool>();
const connection = mock<PoolConnection>();
when(connPool.getConnection()).thenResolve(instance(connection));
const service = new Service(
instance(connPool)
)
it("Should save info", async function() {
const res = await service.saveInfo(command);
expect(res).to.not.throw;
});
}
Code :
class Service {
constructor(readonly pool: Pool) {}
async saveInfo(txt: string) {
const conn = await this.pool.getConnection();
...
}
}
Test stopt op regel await this.pool.getConnection() en time-out na 2 seconden en geeft foutmelding:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
Het verhogen van de time-out hielp niet.
Antwoord #1
Het lijkt erop dat ts-mockito niet goed werkt met JS-promise. Maar er is een oplossing, referentie: https://github.com/NagRock/ts-mockito/issues/191#issuecomment-708743761
index.ts:
import { Pool } from 'mysql2/promise';
export default class Service {
constructor(readonly pool: Pool) {}
async saveInfo() {
const conn = await this.pool.getConnection();
return conn;
}
}
index.test.ts:
import { Pool, PoolConnection } from 'mysql2/promise';
import { instance, mock, verify, when } from 'ts-mockito';
import Service from './';
const resolvableInstance = <T extends {}>(mock: T) =>
new Proxy<T>(instance(mock), {
get(target, name: PropertyKey) {
if (['Symbol(Symbol.toPrimitive)', 'then', 'catch'].includes(name.toString())) {
return undefined;
}
return (target as any)[name];
},
});
describe('Conn test', function () {
it('Should save info', async function () {
const connPool = mock<Pool>();
const connection = mock<PoolConnection>();
const connectionInstance = resolvableInstance(connection);
when(connPool.getConnection()).thenResolve(connectionInstance);
const connPoolInstance = instance(connPool);
const service = new Service(connPoolInstance);
const conn = await service.saveInfo();
expect(conn).toBe(connectionInstance);
verify(connPool.getConnection()).called();
});
});
Testresultaat:
PASS stackoverflow/76771580/index.test.ts (8.678 s)
Conn test
✓ Should save info (2 ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 8.725 s, estimated 9 s
pakketversies:
"ts-mockito": "^2.6.1",
"mysql2": "^3.5.2",
