-
Notifications
You must be signed in to change notification settings - Fork 0
Adding Sample #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| # hapi-test-utils | ||
|
|
||
| This package provides common helpers for testing [hapijs](https://hapijs.com/) code. | ||
| This package provides common helpers for testing API(unit test)[hapijs](https://hapijs.com/) code. | ||
| It is currently compatible with hapi v17 and therefore also only works on nodejs 8. | ||
|
|
||
| Install the module as a dev dependency | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a comment and link to the examples folder here in the readme! :) |
||
|
|
@@ -10,9 +10,10 @@ yarn add --dev hapi-test-utils # or npm install --save-dev hapi-test-utils | |
| ``` | ||
|
|
||
| The package exports an object with "namespaces" for the various test functionality, which will be covered below | ||
|
|
||
| ## Routes | ||
|
|
||
| The `routing` namespace currently contains one helper for testing whether routes have been registered. Imagine some code like this | ||
| Imagine some code like this.We want to test `/hello` route with hapi-test-utils module | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space after first |
||
|
|
||
| ```js | ||
| const Hapi = require('hapi'); | ||
|
|
@@ -26,10 +27,11 @@ server.route({ | |
| }); | ||
| ``` | ||
|
|
||
| The `routing` namespace currently contains one helper for testing whether routes have been registered. | ||
| One can of course test that the hapi server responds with hello, however, the `hasRoute` helper from this library can help you test whether your routes / plugin have been registered in hapi according to specification: | ||
|
|
||
| ```js | ||
| const { hasRoute } = require('hapi-test-util').routing; | ||
| const { hasRoute } = require('hapi-test-utils').routing; | ||
|
|
||
| // Test in jest format | ||
| describe('hello api', () => { | ||
|
|
@@ -40,6 +42,8 @@ describe('hello api', () => { | |
| }); | ||
| ``` | ||
|
|
||
| add `mocha` to run the test. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The given test suite in this example is actually written with |
||
|
|
||
| ## Auth | ||
|
|
||
| Often you develop apis that are either fully or partially protected with authentication and authorization, e.g like this: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "name": "sample", | ||
| "version": "1", | ||
| "main": "server.js", | ||
| "scripts": { | ||
| "lint": "eslint src/", | ||
| "test": "mocha --recursive test/" | ||
| }, | ||
| "author": "Navideh", | ||
| "license": "MIT", | ||
| "dependencies": { | ||
| "eslint": "^4.19.1", | ||
| "eslint-plugin-jest": "^21.15.0", | ||
| "eslint-plugin-node": "^6.0.1", | ||
| "hapi": "^17.3.1", | ||
| "hoek": "^5.0.3", | ||
| "unexpected": "^10.37.2" | ||
| }, | ||
| "devDependencies": { | ||
| "hapi-test-utils": "^1.0.0", | ||
| "mocha": "^5.0.5" | ||
| }, | ||
| "engines": { | ||
| "node": ">= 8.9" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| const Hapi=require('hapi'); | ||
| const helloPlugin=require('./src/helloPlugin'); | ||
| const server=Hapi.Server({ | ||
| host:'localhost', | ||
| port:'8000' | ||
| }); | ||
| async function start(){ | ||
| await server.register(helloPlugin); | ||
| await server.start(); | ||
| console.log('server is running at:',server.info.uri); // eslint-disable-line no-console | ||
| } | ||
| start(); | ||
| module.exports={ | ||
| server | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| const register=async(server)=>{ | ||
| server.route({ | ||
| method: 'GET', | ||
| path: '/hello', | ||
| handler: () => { | ||
| return 'Hello, world!'; | ||
| } | ||
| }); | ||
|
|
||
| }; | ||
|
|
||
| module.exports={ | ||
| register, | ||
| name:'hello-world-plugin', | ||
| version: '1.0.0' | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "rules": { | ||
| "node/no-unpublished-require": 0, | ||
| "node/no-missing-require": 0 | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| const {hasRoute}=require('hapi-test-utils').routing; | ||
| const Hapi = require('hapi'); | ||
| const helloPlugin = require('../src/helloPlugin'); | ||
| const uexpect = require('unexpected'); | ||
|
|
||
| describe('test helloPlugin',()=>{ | ||
|
|
||
| let server; | ||
| beforeEach(async ()=>{ | ||
| server=new Hapi.Server(); | ||
| await server.register(helloPlugin); | ||
| }); | ||
| it('has a GET /hello rout',()=>{ | ||
| uexpect (hasRoute(server,'/hello','get'),'to be true'); | ||
|
|
||
| }); | ||
| it('returns hello world', async () => { | ||
| const request = { | ||
| url:'/hello', | ||
| method:'get' | ||
| }; | ||
| const response = await server.inject(request); | ||
| uexpect(response.statusCode, 'to be', 200); | ||
| uexpect(response.payload, 'to equal', 'Hello, world!'); | ||
| }); | ||
|
|
||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's change it to:
This package provides common helpers for unit testing