Sample unit test for model
See original GitHub issueIs your feature request related to a problem? Please describe. I’m new to Hapi & mongoose and trying to setup unit test for some model I define with Jest, but having found no references on google how to do it properly, so running into some sort of trial and error to make it work.
Describe the solution you’d like One sample unit test where you could run CRUD or call method in a model
Describe alternatives you’ve considered
const Mongoose = require('mongoose')
const RestHapi = require('rest-hapi')
const Config = require('../../config')
const restHapiConfig = Config.get('/restHapiConfig')
// process.env.NODE_ENV = 'development'
const path = require('path')
// we require the handlers directly, so we can test the "Lib" functions in isolation
const Manifest = require('../../config/manifest.conf');
const Glue = require('glue');
describe('unit tests - programs', () => {
let models
beforeAll(async (done) => {
RestHapi.config = restHapiConfig
RestHapi.config.absoluteModelPath = true
RestHapi.config.modelPath = path.join(__dirname, '../../server/models')
models = await RestHapi.generateModels(Mongoose)
const composeOptions = {
relativeTo: path.join(__dirname, '../../')
}
const manifest = Manifest.get('/')
const server = await Glue.compose(manifest, composeOptions)
await server.start()
done()
})
it('should return a report body', async (done) => {
const logger = RestHapi.getLogger('test').bind()
const UserData = Mongoose.model('user_program')
let userData = await RestHapi.list(UserData, {$limit: 1}, logger)
let reportBody = await models.program.getEvaluationSummary(userData, logger)
console.log(reportBody)
expect(reportBody).toBeDefined();
done()
});
afterAll((done) => {
process.exit(0)
done()
});
});
Additional context Above is my trial and it works but could have been better, I guess having a setup.js and not calling generateModels and server.start every time.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
unit testing - Should model classes be tested? - Stack Overflow
Given that, if your model code is getting exercised when you run other tests, it may not be necessary to have tests specific...
Read more >Unit Testing Tutorial – What is, Types & Test Example - Guru99
Unit Testing is a type of software testing where individual units or components of a software are tested. The purpose is to validate...
Read more >Unit Testing — Learn with examples - Tuskr
Unit testing is testing the smallest testable unit of an application. It is done during the coding phase by the developers. To perform...
Read more >Swift: Write Unit Test for Model Class | by Amsaraj Mariyappan
Swift: Write Unit Test for Model Class. This blog we are going to explore brief introduction to a Test Driven Development in iOS...
Read more >Unit Testing Tutorial: A Comprehensive Guide With Examples ...
Unit tests are generally written in a test model, which can be run independently of the application. Integration testing is ideal to ensure...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi @1change, sorry for the late response, life has been busy lately.
Glad you got a working example. For your ‘globalSetup’, are you trying to run an external setup script (like the
setup.js
you mentioned)? I believe after you register the rest-hapi plugin you need to pass theMongoose
instance back to your test script instead of using a separaterequire('mongoose')
. So maybe something likeAlso, for the code you posted, since you are using
beforeAll
it should not have to callserver.start
andgenerateModels
every time, correct?Hi @JKHeadley, thx for taking the time. Yes beforeAll won’t call server every time but in every testspec, so having a globalSetup would help.