nginx-testing
Version:
Support for integration/acceptance testing of nginx configuration.
86 lines (62 loc) • 2.16 kB
Markdown
This project provides support for easy integration/acceptance testing of [nginx](https://nginx.org/) configuration using TypeScript or JavaScript, primarily for testing [njs](https://nginx.org/en/docs/njs/) (NGINX JavaScript) scripts.
It allows you to run tests against various nginx versions on any Linux, macOS or Windows (x64) system without need to install nginx or use some overcomplex methods such as Docker \[1\].
nginx-testing automatically downloads a precompiled nginx binary for your system and architecture from project [nginx-binaries](https://github.com/jirutka/nginx-binaries).
## Installation
``` sh
# using npm:
npm install --save-dev nginx-testing
# or using yarn:
yarn add --dev nginx-testing
```
## Usage Examples
### Testing with Mocha
***example.test.ts:*.**
``` ts
import { strict as assert } from 'assert'
import { after, afterEach, before, beforeEach, test } from 'mocha'
import { startNginx, NginxServer } from 'nginx-testing'
import fetch from 'node-fetch'
let nginx: NginxServer
before(async () => {
nginx = await startNginx({ version: '1.24.x', configPath: './nginx.conf' })
})
after(async () => {
await nginx.stop()
})
beforeEach(async () => {
// Consume logs (i.e. clean them before the test).
await nginx.readAccessLog()
await nginx.readErrorLog()
})
afterEach(async function () {
// Print logs if the test failed.
if (this.currentTest?.state === 'failed') {
console.error('Access log:\n' + await nginx.readAccessLog())
console.error('Error log:\n' + await nginx.readErrorLog())
}
})
test('GET / results in HTTP 200', async () => {
const resp = await fetch(`http://localhost:${nginx.port}/`)
assert.equal(resp.status, 200)
})
```
***nginx.conf:*.**
``` nginx
events {
}
http {
server {
listen localhost:__PORT__;
location / {
return 200 "OK";
}
}
}
```
[](https://github.com/jirutka/nginx-testing#api).
[](https://github.com/jirutka/nginx-testing#cli).
This project is licensed under [MIT License](http://opensource.org/licenses/MIT/).
1. Yes, that’s right, you don’t need Docker to run a damn binary\!