{"id":18596981,"url":"https://github.com/focusaurus/mock-pg-with-testdouble","last_synced_at":"2025-05-16T13:33:11.260Z","repository":{"id":141309915,"uuid":"58876551","full_name":"focusaurus/mock-pg-with-testdouble","owner":"focusaurus","description":null,"archived":false,"fork":false,"pushed_at":"2016-05-16T14:16:49.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T18:02:55.684Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/focusaurus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-15T18:09:28.000Z","updated_at":"2016-05-15T18:27:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"ca720e33-262e-441c-9d1c-edadc22308e2","html_url":"https://github.com/focusaurus/mock-pg-with-testdouble","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/focusaurus%2Fmock-pg-with-testdouble","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/focusaurus%2Fmock-pg-with-testdouble/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/focusaurus%2Fmock-pg-with-testdouble/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/focusaurus%2Fmock-pg-with-testdouble/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/focusaurus","download_url":"https://codeload.github.com/focusaurus/mock-pg-with-testdouble/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254538479,"owners_count":22087871,"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":"2024-11-07T01:26:32.454Z","updated_at":"2025-05-16T13:33:11.235Z","avatar_url":"https://github.com/focusaurus.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mocking pg with testdouble\n\nThis project is an example setup of the following stack:\n\n- [express](http://expressjs.com/) for an API server\n- [pg-query-bluebird](https://www.npmjs.com/package/pg-query-bluebird) as our interface to a PostgreSQL database\n- [testdouble](https://www.npmjs.com/package/testdouble) for unit test mocking/stubbing\n- [joi](https://www.npmjs.com/package/joi) for input and configuration validation\n\nI have been experimenting with mocking postgres via testdouble to get high unit test code coverage. This repo shows some of the patterns and examples I have found.\n\n## Mocking Database Results\n\n- relatively straightforward to `td.replace(pg, 'query')` and return promises for zero rows, some rows, an error\n\n## Configuration Validation\n\n- nice pattern to declare schema for your configuration\n- joi handles validation, coercion to desired types, and reasonable error message generation\n- clearly exit with error and good error message when configuration is invalid\n- configuration comes from environment variables exclusively which is 12-Factor App compliant\n- read `code/config.js` for supported environment variables\n- Use the official postgresql supported environment variable names and their defaults as well\n- reasonable defaults for local development provided\n- valid configuration not required to run and pass unit tests\n  - `git clone \u0026\u0026 npm install \u0026\u0026 npm test` works\n\n## How to run the npm scripts\n\n- `npm test` to run tape unit tests\n- `npm start` to run the server\n- `npm run lint` to run eslint static analysis\n- `npm run coverage` to run istanbul code coverage for the unit tests\n\n## Unit Test Code Coverage\n\n- `npm run coverage`\n- have 100% coverage of the express route handler functions, which represent the primary business logic of the application\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffocusaurus%2Fmock-pg-with-testdouble","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffocusaurus%2Fmock-pg-with-testdouble","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffocusaurus%2Fmock-pg-with-testdouble/lists"}