question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mock-knex 0.3.3 failed on save and update operations

See original GitHub issue

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 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:closed
  • Created 7 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dustinmartincommented, Sep 13, 2016

Fantastic! Thanks again for resolve this. Happy to help!

0reactions
jbrumwellcommented, Sep 13, 2016

There were a few issues

This resolved the issue with transactions

Thanks for your help 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found