New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

pythagora-dev

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pythagora-dev

<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

  • 0.0.1
  • unpublished
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

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

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

  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.

    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.



🔗 Connect with us

📫 Stay updated by subscribing to our email list here.

💬 Join the discussion on our Discord server.

⭐ Star this repo to show your interest/support.

FAQs

Package last updated on 13 Feb 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc