Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
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.

Copy link
Copy Markdown
Contributor

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

It is currently compatible with hapi v17 and therefore also only works on nodejs 8.

Install the module as a dev dependency

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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! :)

Expand All @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after first .


```js
const Hapi = require('hapi');
Expand All @@ -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', () => {
Expand All @@ -40,6 +42,8 @@ describe('hello api', () => {
});
```

add `mocha` to run the test.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The given test suite in this example is actually written with jest :)


## Auth

Often you develop apis that are either fully or partially protected with authentication and authorization, e.g like this:
Expand Down
26 changes: 26 additions & 0 deletions src/sample/package.json
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"
}
}
15 changes: 15 additions & 0 deletions src/sample/server.js
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
}
16 changes: 16 additions & 0 deletions src/sample/src/helloPlugin.js
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'
}
6 changes: 6 additions & 0 deletions src/sample/test/.eslintrc
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
}
}
27 changes: 27 additions & 0 deletions src/sample/test/helloPlugin.test.js
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!');
});

});
Loading