nginx-testing
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

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

See on GitHub.

CLI

See on GitHub.

License

This project is licensed under MIT License.

  1. Yes, that’s right, you don’t need Docker to run a damn binary!

Readme

Keywords

Package Sidebar

Install

npm i nginx-testing

Weekly Downloads

122

Version

0.4.0

License

MIT

Unpacked Size

161 kB

Total Files

65

Last publish

Collaborators

  • jirutka