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

findOne() saving over table variable #69

Open
Amber-Williams opened this issue Mar 11, 2019 · 3 comments
Open

findOne() saving over table variable #69

Amber-Williams opened this issue Mar 11, 2019 · 3 comments

Comments

@Amber-Williams
Copy link

Amber-Williams commented Mar 11, 2019

Using the following code and I think there's an issue with the findOne() method because it saves over my table variable in my testing with Jest.
const SequelizeMock = require('sequelize-mock'); let dbMock = new SequelizeMock(); let UserMock = dbMock.define('test', { test: 'test', name: 'no' }

describe('Sequelize Model Testing',() => { test('testingggg', async () => { let user = await UserMock.findOne({ where: { name: 'amber' } }) expect(user.name).toBe('amber'); }) })

The expect user.name should be 'no' but its returning as 'amber'.

@bublig737
Copy link

I have the same problem!

@stoked10
Copy link

Same problem, the findOne seems to be creating the record if it doesn't exist and returns it, my code:

workerController

const { worker } = require('../../models');

async function getAll() {
  const aws = await worker.findAll();

  return aws;
}

async function getOne(id) {
  const aw = await worker.findById(id);

  return aw.get({ plain: true });
}

module.exports = {
  getAll,
  getOne,
};

spec

const Worker = require('../../app/controllers/workerController');
const { worker } = require('../../models');

jest.mock('../../models/worker', () => () => {
  // eslint-disable-next-line global-require
  const SequelizeMock = require('sequelize-mock');
  const dbMock = new SequelizeMock();
  return dbMock.define('worker', {
    id: '6037c891-8237-4596-9665-645b2b6a1f4a',
    available: true,
    createdAt: new Date(),
    updatedAt: new Date(),
  });
});

describe('Worker Controller Functions', () => {
  fit('test', async () => {
    const all = await Worker.getAll();
    console.log(`BEFORE GET_ONE: ${JSON.stringify(all, null, 2)}`);
    const result = await Worker.getOne('1');
    console.log(`RESULT: ${JSON.stringify(result, null, 2)}`);
    expect(result.id).toEqual('1');
  });
});

Results in the following output:

    BEFORE GET_ONE: [
      {
        "id": "6037c891-8237-4596-9665-645b2b6a1f4a",
        "available": true,
        "createdAt": "2019-09-25T11:30:36.629Z",
        "updatedAt": "2019-09-25T11:30:36.629Z"
      }
    ]

    RESULT: {
      "id": "1",
      "available": true,
      "createdAt": "2019-09-25T11:30:36.629Z",
      "updatedAt": "2019-09-25T11:30:36.629Z"
    }

Test Suites: 1 passed, 1 total

@stoked10
Copy link

Never mind, found this in the code base for findById:

/**

  • Executes a mock query to find an instance with the given ID value. Without any other
  • configuration, the default behavior when no queueud query result is present is to
  • create a new Instance with the given id and wrap it in a promise.
  • To turn off this behavior, the $autoQueryFallback option on the model should be set
  • to false.
  • @instance
  • @param {Integer} id ID of the instance
  • @return {Promise} Promise that resolves with an instance with the given ID
    **/

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