Open pkyeck opened 6 years ago
The $overrideImport
function is, at the moment, covered in the Swapping Model for Mocks section on the Getting Started page.
Were you able to find this documentation, or was this confusing or unhelpful in some way? Just want to try to make sure I know how to improve the documentation.
I also found this somewhat confusing to get started, as all of my models are similarly accessed through a models/index.js
require statement. What I did was instead of overriding individual models as the documentation seems to suggest, I overwrote my require statement with a collection and proxyquire.
In my-module.js
var models = require("../common/models");
In my-test.js
var mockModels = {
"User": UserMock = DBConnectionMock.define('user', {
'name': 'Bob'
}),
"Project": ProjectMock= DBConnectionMock.define('project', {
'name': '2018 Budget'
})
};
var proxyquire = require('proxyquire');
var MyModule = proxyquire('../common/my-module', {
'../common/models': mockModels
});
I did this so that my-module.js can continue to reference models.User.blah and models.Project.blah.
See if this article helps - Sequelize mocking with Jest and Node” https://link.medium.com/7p2tj2cwhV
Have been looking for this for a while too. It would be great if the example by @dalexander-trc was included in the docs.
yes, its quite confusing how to mock the database at Swapping Model for Mocks. where to use over $overrideImport.
can anybody explain me how to swap the model for mocks.
@singhprabhanshu Did you already try the setup that I recommended above? Do you have any sample to post? This is a pretty old topic and I've been out of this code for some time, so if your setup is vastly different you may want to create a new topic.
I'm trying to use this lib in my project but can't wrap my head around how exactly to integrate it. I'm using migrations so I have the auto-generated
models/index.js
that is importing all Models.Was trying to use
$overrideImport
but don't know how ...