/pythagora

Primary LanguageJavaScriptGNU General Public License v3.0GPL-3.0

Text changing depending on mode. Light: 'So light!' Dark: 'So dark!'

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 data 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 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.

Pythagora records all requests to endpoints of your app, along with Mongo/Redis queries. Then, when you run the tests, Pythagora mocks all responses from Redis and simulates the database conditions from the time when the API requests were captured.

Tests do not depend on the state of the database. When an API request is being recorded, Pythagora saves all documents used during the request. Later, when you run the actual test, it restores those documents into pythagoraDb database. This way, the app gets the same data it got from the database on the machine on which it was captured and the data on your local database will NOT be affected while running tests.

If the request updates the database, Pythagora also tests the database to see if it was updated correctly.

Pythagora Alpha Demo

Watch Pythagora Demo (2:28 min)



⚙️ Setup

  1. Install Pythagora by running

    npm install pythagora
    
  2. Integrate Pythagora by adding the Express app and mongoose instance to global.Pythagora right after you initialize express/mongoose. 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

  1. From the root directory run Pythagora in a capture mode first to capture test data and mocks.

    npx pythagora --mode capture --initScript ./path/to/your/server.js
    
    Eg. if you start your Node.js app with node ./server.js then the command would be:

    npx pythagora --mode capture --initScript ./server.js
    
  2. Click around your application or make requests to your API. Pythagora will capture all requests and responses.

▶️ 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.



🔗 Connect with us

📫 Stay updated by subscribing to our email list here.

💬 Join the discussion at Github discussions or on our Discord server.

⭐ Star this repo to show your interest/support.