Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"conn.transaction is not a function" with MSSQL #95

Open
hijaq opened this issue Sep 9, 2019 · 2 comments
Open

"conn.transaction is not a function" with MSSQL #95

hijaq opened this issue Sep 9, 2019 · 2 comments

Comments

@hijaq
Copy link

hijaq commented Sep 9, 2019

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 call transaction method, here's a sample code to reproduce:

const knex = require('knex');
const mockKnex = require('mock-knex');

const mockDb = knex({ client: 'mssql' });
const tracker = mockKnex.getTracker();

const getSome = async (db) => {
  await db.transaction().then((trx) => {
    console.log('started transaction');
    return trx;
  });
  return 1;
};

describe('test', () => {
  beforeAll(() => {
    mockKnex.mock(mockDb);
  });
  afterAll(() => {
    mockKnex.unmock(mockDb);
  });
  beforeEach(() => {
    tracker.install();
  });
  afterEach(() => {
    tracker.uninstall();
  });
  test('should call', async () => {
    tracker.on('query', (query) => {
      query.response([]);
    });
    const result = await getSome(mockDb);
    expect(result).toEqual(1);
  });
});

and the error is:

test
    ✕ should call (8ms)

  ● test › should call

    TypeError: conn.transaction is not a function

      at node_modules/knex/lib/dialects/mssql/transaction.js:81:27
      at tryCatcher (node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (node_modules/bluebird/js/release/promise.js:517:31)
      at Promise._settlePromise (node_modules/bluebird/js/release/promise.js:574:18)
      at Promise._settlePromiseCtx (node_modules/bluebird/js/release/promise.js:611:10)
      at _drainQueueStep (node_modules/bluebird/js/release/async.js:142:12)
      at _drainQueue (node_modules/bluebird/js/release/async.js:131:9)
      at Async.Object.<anonymous>.Async._drainQueues (node_modules/bluebird/js/release/async.js:147:5)
      at Immediate.Async.drainQueues [as _onImmediate] (node_modules/bluebird/js/release/async.js:17:14)

used version:

  • tedious: 6.3.0
  • mssql: 5.1.0
  • knex: 0.19.3
  • mock-knex: 0.4.6
@treffynnon
Copy link

treffynnon commented Oct 2, 2019

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

@PhakornKiong
Copy link

@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()
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants