Use tesugen to setup API-testing framework in Typescript whenever you need it.
Make sure that you are running latest stable NodeJS (>= 14.6.0 LTS) and npm (>=7.6.1) version.
- Installation & Usage
- Project setup: Jest
- Project setup: TestyTS
- Structure: Jest
- Structure: TestyTS
- Test client
- Class validator
- Base URL
- Dotenv file example
Navigate to desired directory and run:
npx tesugen
You will be asked to choose your package manager, test runner, name your project and you will be ready to go.
Project-setup: Jest as selected test runner
- Typescript preprocessor for Jest (If selected runner is Jest): TS-Jest
- Test runner: Jest
- Test reporter for Jest: Jest-html-reporters
- HTTP Client: Supertest Agent
- Class-validator: Class-validator - For aditional contract/integration testing.
- Working with dotenv files: Dotenv
Project-setup: TestyTS as selected test runner
- Test runner: TestyTS
- HTTP Client: Supertest Agent
- Class-validator: Class-validator - For aditional contract/integration testing.
- Working with dotenv files: Dotenv
- src
- routes
- testRoutes.ts - Initial routes setup (Endpoints which will be the target)
- client.ts - Initial test client setup
- setup.ts - Initial test controller setup
- testAgent.ts - Superagent initial setup
- routes
- test - test folder where you can organize your tests
- validation - validateResponse function for aditional contract/integration testing
- .env - local environment file
- .gitignore
- index.ts
- jest.config.js - Initial Jest config file (Generated only if the selected runner is Jest)
Create desired npm scripts to run specific tests:
"scripts": {
"test": "jest ./test/functional/example",
"test:basic": "jest ./test/basic/example"
},
If you run jest
in project root, Jest will by default look for all files with *.test.ts and run them if possible.
To have everything running smoothly, testEnvironment and preset in jest.config.jest should be set by default:
preset: 'ts-jest',
testEnvironment: 'node',
Test reports will be generated in ./html-report in HTML format via Jest-html-reporters. Reporter can be configured in jest.config.js file. Jest supports multiple reporters. You can even create own custom reporter.
- src
- routes
- testRoutes.ts - Initial routes setup (Endpoints which will be the target)
- client.ts - Initial test client setup
- routes
- test - test folder where you can organize your tests
- validation - validateResponse function for aditional contract/integration testing
- .env - local environment file
- .gitignore
Create desired npm scripts to run specific tests:
"scripts": {
"test": "testyts",
"test:basic": "testyts ./test/basic/example"
},
Test client can be configured and reused with no limitations. For testing multiple services, feel free to configure TestController to have multiple TestClient properties for every desired service.
Test client has four basic example methods for four HTTP methods: GET, POST, PUT and DELETE.
Function located in ./validation/responseValidation.ts can be used for extended contract/integration testing. Make sure to create proper models according to desired response from your project documentation. To speed this process up, use any JSON to TS generator online to quickly convert JSON to TS interfaces/classes. Example: json2ts. Follow Class-validator documentation. Your tests will look like this:
expect(validateResponse(ProfileExampleResponse).toBeTruthy();
is stored in your local .env file. This is also highly conigurable, you can adjust this to your project needs. Test credentials can also be stored in local .env file and be used for authorized endpoints. Supertest Agent supports setting headers:
supertest.agent(String(process.env.BASE_URL)).set({'Authorization':`${bearerToken}`});
BASE_URL=https://www.aaa.ccc