Run npm test
, than the tests in the folders test-works
and test-works-2
run successfully. The test in the folder test-does-not-work
fails.
After running npm build
:
node dist/run-works.js
runsnode dist/run-does-not-work.js
throwsnode dist/run-does-not-work-2.js
throws
Fix (thanks to @sheremet-va):
import {describe, vi, it, expect} from 'vitest';
import {connect} from './index';
import pg from 'pg';
const {Pool} = pg;
vi.mock('pg', async () => {
const actual = <Record<string, unknown>>await vi.importActual('pg');
return {
...actual,
default: {
Pool: vi.fn(),
}
};
});
describe('connect', () => {
it('should call Pool', () => {
connect('postgres://...');
expect(Pool).toBeCalledTimes(1);
});
});