Generate 90% code coverage with integration tests in 1 hour
Pythagora is a tool that generates integration tests for your Node.js app by recording server activity without you having to write a single line of code.
🏗️ How it works
To integrate Pythagora into your Node.js app, you just need to paste one line of code
if (global.Pythagora) global.Pythagora.setApp(app);
and run the Pythagora capture command. Then, just play around with your app and from all API requests and database queries Pythagora will generate integration tests.
1. Capturing requests
Pythagora records all requests to endpoints of your app with the response and everything that's happening during the request. Currently, that means all Mongo and Redis queries with responses (in the future 3rd party API requests, disk IO operations, etc.). Then, when you run the tests, Pythagora can simulate the server conditions from the time when the request was captured.
2. Running tests
When running tests, it doesn’t matter what database is your Node.js connected to or what is the state of that database. Actually, that database is never touched or used —> instead, Pythagora creates a special, ephemeral `pythagoraDb` database, which it uses to restore the data before each test is executed, which was present at the time when the test was recorded. Because of this, tests can be run on any machine or environment.
If a test does an update to the database, Pythagora also checks the database to see if it was updated correctly.
Integrate Pythagora by adding the Express instance to global.Pythagora right after you initialize Express. Eg. if you initialize Express with let app = express(); then add this on the next line:
if (global.Pythagora) global.Pythagora.setApp(app);
IMPORTANT: make sure that you add this line before any routes or middlewares are configured.
🎥 Capturing tests
From the root directory run Pythagora in a capture mode first to capture test data and mocks.
Click around your application or make requests to your API. Pythagora will capture all requests and responses.
NOTE: To stop the capture, you can exit the process like you usually do (Eg. Ctrl + C)
▶️ Executing tests
After you captured all requests you want, you just need to add the mode parameter --mode test to the Pythagora command.
npx pythagora --script ./path/to/your/server.js --mode test
📝 Code Coverage Report
Code coverage is a great metric while building automated tests as it shows us which lines of code are covered by the tests. Pythagora uses nyc to generate a report about code that was covered with Pythagora tests. By default, Pythagora will show you the basic code coverage report summary when you run tests.
If you want to generate a more detailed report, you can do so by running Pythagora with --full-code-coverage-report flag. Eg.
npx pythagora --script ./path/to/your/server.js --mode test --full-code-coverage-report
You can find the code coverage report inside pythagora_data folder in the root of your repository. You can open the HTML view of the report by opening pythagora_data/code_coverage_report/lcov-report/index.html.
In case you don't want the code coverage to be shown at all while running tests, you can run the tests with --no-code-coverage parameter.
ℹ️ Test data
If you are interested in what has been recorded with pythagora
you can see files in your root directory inside pythagora_data directory.
Those are tests that Pythagora captured.
<source media="(prefers-color-scheme: dark)" srcset="https://user-images.githubusercontent.com/10895136/217571898-14e94ea7-75a5-4a50-a7dc-486e10a8b462.png"> <img height="300px" alt="Text changing depending on mode. Lig
The npm package pythagora-dev receives a total of 0 weekly downloads. As such, pythagora-dev popularity was classified as not popular.
We found that pythagora-dev demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.It has 1 open source maintainer collaborating on the project.
Package last updated on 13 Feb 2023
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.