question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sample unit test for model

See original GitHub issue

Is 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:open
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
JKHeadleycommented, Aug 20, 2018

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 the Mongoose instance back to your test script instead of using a separate require('mongoose'). So maybe something like

const setup = require('setup.js')

...

const Mongoose = await setup()

Also, for the code you posted, since you are using beforeAll it should not have to call server.start and generateModels every time, correct?

1reaction
1changecommented, Aug 20, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found