This project provides support for easy integration/acceptance testing of nginx configuration using TypeScript or JavaScript, primarily for testing 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.
Installation
# using npm:
npm install --save-dev nginx-testing
# or using yarn:
yarn add --dev nginx-testing
Usage Examples
Testing with Mocha
example.test.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:.
events {
}
http {
server {
listen localhost:__PORT__;
location / {
return 200 "OK";
}
}
}
API
CLI
License
This project is licensed under MIT License.
- Yes, that’s right, you don’t need Docker to run a damn binary!