{"id":24660225,"url":"https://github.com/skarpdev/hapi-test-utils","last_synced_at":"2026-05-20T04:36:17.054Z","repository":{"id":57261125,"uuid":"125715183","full_name":"skarpdev/hapi-test-utils","owner":"skarpdev","description":"Utilities and helpers for testing hapijs code","archived":false,"fork":false,"pushed_at":"2018-04-06T14:00:54.000Z","size":109,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-26T03:18:46.443Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/skarpdev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-18T10:50:09.000Z","updated_at":"2018-03-23T10:31:48.000Z","dependencies_parsed_at":"2022-09-13T03:24:21.303Z","dependency_job_id":null,"html_url":"https://github.com/skarpdev/hapi-test-utils","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skarpdev%2Fhapi-test-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skarpdev%2Fhapi-test-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skarpdev%2Fhapi-test-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skarpdev%2Fhapi-test-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/skarpdev","download_url":"https://codeload.github.com/skarpdev/hapi-test-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244747245,"owners_count":20503344,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-26T03:18:49.767Z","updated_at":"2026-05-20T04:36:17.024Z","avatar_url":"https://github.com/skarpdev.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hapi-test-utils\n\nThis package provides common helpers for testing [hapijs](https://hapijs.com/) code.\nIt is currently compatible with hapi v17 and therefore also only works on nodejs 8.\n\nInstall the module as a dev dependency\n\n```bash\nyarn add --dev hapi-test-utils # or npm install --save-dev hapi-test-utils\n```\n\nThe package exports an object with \"namespaces\" for the various test functionality, which will be covered below\n## Routes\n\nThe `routing` namespace currently contains one helper for testing whether routes have been registered. Imagine some code like this\n\n```js\nconst Hapi = require('hapi');\n\nconst server = new Hapi.Server({});\n\nserver.route({\n    method: 'get',\n    path: '/hello',\n    handler: () =\u003e return 'hello';\n});\n```\n\nOne 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:\n\n```js\nconst { hasRoute } = require('hapi-test-util').routing;\n\n// Test in jest format\ndescribe('hello api', () =\u003e {\n    it('has a get /hello route', () =\u003e {\n        // we are using server instance described above.\n        expect(hasRoute(server, '/hello', 'get')).toBe(true);\n    });\n});\n```\n\n## Auth\n\nOften you develop apis that are either fully or partially protected with authentication and authorization, e.g like this:\n\n```js\nconst Hapi = require('hapi');\n\n// Ugly pattern for brewity, this addRootRoute should be made into a plugin instead!\nconst addRootRoute = (server) =\u003e {\n    server.route({\n        method: 'GET',\n        path: '/',\n        options: {\n            auth: 'simple'\n        },\n        handler: function (request, h) {\n            return $`welcome request.auth.credentials.name`;\n        }\n    });\n};\n\nconst server = new Hapi.Server({});\nawait server.register(require('hapi-auth-basic'));\n\n// validate function cut out for brewity\nserver.auth.strategy('simple', 'basic', { validateFn });\naddRootRoute(server);\n```\n\nNow you can't easily test this `/` handler as you have to provide correct credentials for the `validateFn`, which might go into the database and so forth.\n\nTo overcome this, the `fake auth scheme` from this package can be utilized. The fake auth scheme takes in a `credentialsFn` which should return an object with the authenticated user's credentials - but no real authentication is going to take place, it will simply inject the credentials into the hapi auth pipeline as if real auth had taken place.\n\nAn example:\n\n```js\nconst Hapi = require('hapi');\nconst { fakeAuthScheme } = require('hapi-test-utils').auth;\n\n// this function is passed to the fake auth scheme to provide the required credentials to the hapi route function\nconst credentialsFn = () =\u003e {\n    return {\n        name: 'Bob The Builder'\n    }\n};\n\ndescribe('root route', () =\u003e {\n    let server;\n    // create a new test server before each test execution\n    beforeEach(async () =\u003e {\n        server = new Hapi.server({});\n        // get in the fake auth scheme\n        await server.register(fakeAuthScheme);\n        // register the fake strategy under the name 'simple' to satisfy the root route's requirements\n        server.auth.strategy('simple', 'fake', {\n            credentialsFn\n        });\n\n        // add the route like we did above, really, it should have been a plugin\n        addRootRoute(server);\n    })\n    it('responds with user name', async () =\u003e {\n        const request = {\n            url: '/',\n            method: 'get'\n        };\n        const response = await server.inject(request);\n        expect(response.payload).toBe('welcome Bob The Builder');\n    });\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskarpdev%2Fhapi-test-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskarpdev%2Fhapi-test-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskarpdev%2Fhapi-test-utils/lists"}