Open hijaq opened 5 years ago
I have the same issue. My current workaround is to create my own implementation of the mock spec:
import Promise from 'bluebird'
import _ from 'lodash'
// tslint:disable: no-submodule-imports
// @ts-ignore
import { spec } from 'mock-knex/dist/platforms/knex/0.11'
// @ts-ignore
import { makeClient } from 'mock-knex/dist/platforms/knex/0.8'
// tslint:enable: no-submodule-imports
// tslint:disable-next-line: no-namespace
namespace MSSQLMockKnex {
class MockTransaction {
public async begin() {
return this
}
// tslint:disable: no-empty
public async commit() {}
public async request() {}
public async rollback() {}
// tslint:enable: no-empty
}
interface Connection {
__knexUid: string
timeout: any
transaction: () => MockTransaction
}
const connection: Connection = {
__knexUid: 'mockedConnection',
timeout: Promise.method(getConnection),
transaction: () => new MockTransaction(),
}
function getConnection() {
return { ...connection }
}
export const newSpec = _.defaultsDeep(
{
replace: [
{
client: {
acquireConnection() {
return Promise.resolve(getConnection())
},
// tslint:disable-next-line: no-empty
destroyRawConnection() {},
},
},
],
},
spec
)
export const client = makeClient(newSpec)
}
Then to use it:
const db = knex({ client: 'mssql' })
MSSQLMockKnex.client.mock(db)
// now you can use db to create mock MSSQL transactions
@treffynnon
I've tried the code you've given with hijaq's code, but I'm still getting this error.
TypeError: conn.beginTransaction is not a function
at node_modules/knex/lib/dialects/mssql/transaction.js:8:12
at Transaction_MSSQL.begin (node_modules/knex/lib/dialects/mssql/transaction.js:7:12)
at node_modules/knex/lib/execution/transaction.js:203:16
at Transaction_MSSQL.acquireConnection (node_modules/knex/lib/execution/transaction.js:254:20)
I did try to do this, but the test just goes on never ending land.
const connection = {
__knexUid: 'mockedConnection111',
timeout: Promise.method(getConnection),
transaction: () => new MockTransaction()
beginTransaction: () => new MockTransaction()
};
Hey, first of all, thanks for that useful package! Now the issue... I'm trying to use
mock-knex
with MSSQL (no connection to db) and it fails when I try to calltransaction
method, here's a sample code to reproduce:and the error is:
used version:
6.3.0
5.1.0
0.19.3
0.4.6