JKHeadley / JKHeadley/rest-hapi

Sample unit test for model

Open
#141 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
1.2k
Forks
155
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the proposed setup.js and the existing server/models path; review how the example initializes RestHapi.generateModels and Glue.compose/server.start. Add a reusable model-test example covering a CRUD operation or model method, with setup and teardown that make the test outcome clear.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
testing
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.