Comments (3)
I have the same problem!
from sequelize-mock.
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.
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)
- SequelizeMockEmptyQueryQueueError: No query results are queued. Unexpected query attempted to be run
- SequelizeMock as a drop-in replacement
- Feature Request - Model.findByPk HOT 1
- findOrCreate returns an Array of Instances intead of just one Instance
- jest not working with sequelize model with associations using sequelize-mock HOT 3
- jest not working with sequelize model with associations using sequelize-mock HOT 6
- Is it possible to define class level methods?
- Mock model definition is not strict
- Maintainership HOT 1
- destroy and findOne returning all instances
- Models sometimes return columns as anonymous functions instead of the actual values
- BelongsTo without 'get' prefix
- Support v5 HOT 1
- fulfilling rawQuery from mock database HOT 1
- transaction commit/rollback/LOCK/afterCommit
- Returning NaN when use sequelize.fn("count")
- Is this library compatible with models defined by Sequelize-Typescript? HOT 1
- TypeError: this.sequelize.runHooks is not a function HOT 1
- Broken link
- with mock model tableAttributes undefined
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sequelize-mock.