Skip to content

Commit

Permalink
Do not reset prior Promise overrides on knex import (#2944)
Browse files Browse the repository at this point in the history
* Do not reset prior Promise overrides on knex import

* Do not leak global state outside of the test
  • Loading branch information
kibertoad authored Dec 10, 2018
1 parent ca45cd1 commit ff75f1d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
7 changes: 7 additions & 0 deletions knex.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
// For details and documentation:
// http://knexjs.org

const oldPromise = global.Promise;

// Should be safe to remove after support for Node.js 6 is dropped
require('@babel/polyfill');

// Preserve any Promise overrides set globally prior to importing knex
if (oldPromise) {
global.Promise = oldPromise;
}

module.exports = require('./lib/index');
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"chai-subset-in-order": "^2.1.2",
"coveralls": "^3.0.2",
"cross-env": "^5.2.0",
"eslint": "5.9.0",
"eslint": "5.10.0",
"eslint-config-prettier": "^3.3.0",
"eslint-plugin-import": "^2.14.0",
"husky": "^1.2.0",
Expand Down
15 changes: 12 additions & 3 deletions test/unit/knex.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
const Knex = require('../../lib/index');
const { expect } = require('chai');
const Promise = require('bluebird');
const bluebird = require('bluebird');
const sqliteConfig = require('../knexfile').sqlite3;
const sqlite3 = require('sqlite3');
const { noop } = require('lodash');

describe('knex', () => {
it('preserves global Bluebird Promise', () => {
const oldPromise = global.Promise;
global.Promise = bluebird;
expect(Promise.map).to.be.a('function'); // eslint-disable-line no-undef
require('../../knex');
expect(Promise.map).to.be.a('function'); // eslint-disable-line no-undef
global.Promise = oldPromise;
});

describe('supports passing existing connection', () => {
let connection;
beforeEach(() => {
Expand Down Expand Up @@ -143,7 +152,7 @@ describe('knex', () => {
userParam: '451',
});
done();
return Promise.resolve();
return bluebird.resolve();
});
});

Expand All @@ -157,7 +166,7 @@ describe('knex', () => {
/Cannot set user params on a transaction - it can only inherit params from main knex instance/
);
done();
return Promise.resolve();
return bluebird.resolve();
});
});

Expand Down

0 comments on commit ff75f1d

Please sign in to comment.