Mock-knex 0.3.3 failed on save and update operations
See original GitHub issueMy unit tests worked fine with Mock-knex 0.3.2. With the last version, I’ve got this error when I want to test some insert or update operations inside a transaction:
On insert:
Unhandled rejection TypeError: Cannot read property 'insertId' of undefined
at Client.processResponse (C:\Projets\Front\webapp-internet\node_modules\knex\lib\dialects\mysql\index.js:116:21)
at C:\Projets\Front\webapp-internet\node_modules\knex\lib\runner.js:116:28
at tryCatcher (C:\Projets\Front\webapp-internet\node_modules\knex\node_modules\bluebird\js\main\util.js:26:23)
at Promise._settlePromiseFromHandler (C:\Projets\Front\webapp-internet\node_modules\knex\node_modules\bluebird\js\main\promise.js:507:31)
at Promise._settlePromiseAt (C:\Projets\Front\webapp-internet\node_modules\knex\node_modules\bluebird\js\main\promise.js:581:18)
at Async._drainQueue (C:\Projets\Front\webapp-internet\node_modules\knex\node_modules\bluebird\js\main\async.js:128:12)
at Async._drainQueues (C:\Projets\Front\webapp-internet\node_modules\knex\node_modules\bluebird\js\main\async.js:133:10)
at Immediate.Async.drainQueues [as _onImmediate] (C:\Projets\Front\webapp-internet\node_modules\knex\node_modules\bluebird\js\main\async.js:15:14)
at processImmediate [as _immediateCallback] (timers.js:367:17)
On update:
Unhandled rejection TypeError: Cannot read property 'affectedRows' of undefined
at Client.processResponse (C:\Projets\Front\webapp-internet\node_modules\knex\lib\dialects\mysql\index.js:120:20)
at C:\Projets\Front\webapp-internet\node_modules\knex\lib\runner.js:116:28
at tryCatcher (C:\Projets\Front\webapp-internet\node_modules\knex\node_modules\bluebird\js\main\util.js:26:23)
at Promise._settlePromiseFromHandler (C:\Projets\Front\webapp-internet\node_modules\knex\node_modules\bluebird\js\main\promise.js:507:31)
at Promise._settlePromiseAt (C:\Projets\Front\webapp-internet\node_modules\knex\node_modules\bluebird\js\main\promise.js:581:18)
at Async._drainQueue (C:\Projets\Front\webapp-internet\node_modules\knex\node_modules\bluebird\js\main\async.js:128:12)
at Async._drainQueues (C:\Projets\Front\webapp-internet\node_modules\knex\node_modules\bluebird\js\main\async.js:133:10)
at Immediate.Async.drainQueues [as _onImmediate] (C:\Projets\Front\webapp-internet\node_modules\knex\node_modules\bluebird\js\main\async.js:15:14)
at processImmediate [as _immediateCallback] (timers.js:367:17)
This is an exemple of code which failed:
bookshelf.transaction(function(t) {
return Customer.forge(customer)
.save(null, {
method: 'insert',
transacting: t
}).then(function(insertedCustomer) {
return insertedCustomer.customers().attach(customers, {
transacting: t
});
}).then(function(result) {
resolve(customer);
}).catch(function(err) {
reject(err);
});
});
And this is my tracker code:
var customer = [];
var insertedCustomer = {
id: "TEST",
name: "TEST"
};
var customers = ['2', '3', '4'];
tracker.on('query', function sendResult(query) {
if (query.method === 'select') {
query.response(customer);
} else if (query.method === 'insert') {
if (query.bindings.length === 3) {
customer.push(insertedCustomer);
customer[0].customers = [];
} else {
customer[0].customers.push(query.bindings[0]);
}
query.response(customer);
} else {
query.response(customer);
}
});
Issue Analytics
- State:
- Created 7 years ago
- Comments:13 (5 by maintainers)
Top Results From Across the Web
Mock-knex 0.3.3 failed on save and update operations #42
My unit tests worked fine with Mock-knex 0.3.2. With the last version, I've got this error when I want to test some insert...
Read more >How to mock Knex using jest - Stack Overflow
jest.mock() will mock a module with an auto-mocked version, factory and options are optional. You can use mockFn.mockReturnThis() to mock ...
Read more >mock-knex - npm
Start using mock-knex in your project by running `npm i mock-knex`. There are 11 other projects in the npm registry using mock-knex.
Read more >Open Source Product Listing | Tenfold
@babel/plugin-proposal-logical-assignment-operators@7.14.5 ... @schematics/update@0.1100.7, MIT, https://github.com/angular/angular-cli.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fantastic! Thanks again for resolve this. Happy to help!
There were a few issues
This resolved the issue with transactions
stream
method https://github.com/colonyamerican/mock-knex/commit/da037a0920abbdbd6cb598b729c22783c6c6d3da#diff-afb57afee1821745f7d0c2b15087aec2R49response
in their query object. To avoid these issues we nest our properties now, https://github.com/colonyamerican/mock-knex/commit/da037a0920abbdbd6cb598b729c22783c6c6d3da#diff-d8494ca7e744216ae18ca4aa7d9eab7cR18Thanks for your help 😃