GithubHelp home page GithubHelp logo

Comments (3)

bublig737 avatar bublig737 commented on May 24, 2024 2

I have the same problem!

from sequelize-mock.

stoked10 avatar stoked10 commented on May 24, 2024

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

from sequelize-mock.

stoked10 avatar stoked10 commented on May 24, 2024

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
    **/

from sequelize-mock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.