https://github.com/focusaurus/mock-pg-with-testdouble
https://github.com/focusaurus/mock-pg-with-testdouble
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/focusaurus/mock-pg-with-testdouble
- Owner: focusaurus
- Created: 2016-05-15T18:09:28.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-05-16T14:16:49.000Z (about 10 years ago)
- Last Synced: 2025-03-25T18:02:55.684Z (about 1 year ago)
- Language: JavaScript
- Size: 9.77 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Mocking pg with testdouble
This project is an example setup of the following stack:
- [express](http://expressjs.com/) for an API server
- [pg-query-bluebird](https://www.npmjs.com/package/pg-query-bluebird) as our interface to a PostgreSQL database
- [testdouble](https://www.npmjs.com/package/testdouble) for unit test mocking/stubbing
- [joi](https://www.npmjs.com/package/joi) for input and configuration validation
I 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.
## Mocking Database Results
- relatively straightforward to `td.replace(pg, 'query')` and return promises for zero rows, some rows, an error
## Configuration Validation
- nice pattern to declare schema for your configuration
- joi handles validation, coercion to desired types, and reasonable error message generation
- clearly exit with error and good error message when configuration is invalid
- configuration comes from environment variables exclusively which is 12-Factor App compliant
- read `code/config.js` for supported environment variables
- Use the official postgresql supported environment variable names and their defaults as well
- reasonable defaults for local development provided
- valid configuration not required to run and pass unit tests
- `git clone && npm install && npm test` works
## How to run the npm scripts
- `npm test` to run tape unit tests
- `npm start` to run the server
- `npm run lint` to run eslint static analysis
- `npm run coverage` to run istanbul code coverage for the unit tests
## Unit Test Code Coverage
- `npm run coverage`
- have 100% coverage of the express route handler functions, which represent the primary business logic of the application