6.0.0 • Published 6 months ago

integratify v6.0.0

Weekly downloads
10,452
License
ISC
Repository
github
Last release
6 months ago

Integratify

Make Node.js integration testing easy!

npm version Dependencies API Continuous Integration

Installation

Install via npm

npm install integratify

or via yarn

yarn add integratify

Usage

const integratify = require('integratify');
import * as integratify from 'integratify';

Configuration

Global

You can set a global configuration that will used for all tests. Adviced to set this in a configuration file running before each test file. This is optional

import { setConfiguration } from 'integratify';

setConfiguration({
  dataPath: 'data',
  schemaValidator: (data, schema) => myValidationFunc(data, schema),
});

Local

You can always overwrite any global configuration per test suite when initializing your Integratify test runner.

const express = require('express');
const app = express();

const testRunner = integratify(app, {
  prefix: '/api/auth', // Optional prefix for every url path
  dataPath: 'data', // Optional path where actual data will be placed on.
  schemaValidator: (data, schema) => myValidationFunc(data, schema), // Function to validate data against a schema
});

Getting Started

Run api tests

// test.config.ts
import { setConfiguration } from 'integratify';

setConfiguration({
  dataPath: 'data',
  schemaValidator: (data, schema) => myValidationFunc(data, schema),
});


// login.test.ts
const testRunner = integratify(app, {
  prefix: '/auth',
  dataPath: 'customDataPath', // Will overwrite global setting
});

describe('POST /login', () => {
  it('Should successfully log in existing user', async () => {
    const { status, header, body } = await testRunner
      .post("/login")
      .send({
        payload: { username, password }, // Optional body
        query: { ... }, // Optional query parameters
        headers: { ... }, // Optional headers
        file: { name: 'fileName.png', value: '...' }, // Optional file
      })
      .expect({
        status: 200,
        schema: loginOutsputSchema,
        matchObject: { username: expect.any(String)}
        matchObjectInArray:  { username: expect.any(String)},
        toEqual:  { username: expect.any(String)};
        length: 1,
        spies: [
          fetchSpy, // Will validate whether called once
          { spy: firebaseSpy, amount: 1 },
        ],
        error: new BadRequestError(errors.THIS_WAS_NOT_OK),
        paths: [
          { 'meta.count': 10 },
          { 'meta.totalCount': 250 },
        ]
      });
  });
});

Available Options

.post .get .put .patch .delete

KeyTypeDescriptionAllowed value(s)
payloadstring/objectBody payloadany string/object
querystring/objectQuery payloadany string/object
headersobjectCustom headersany object
fileobjectFile payload{ name: string, value: any }

.expect

KeyTypeDescriptionAllowed value(s)
statusnumberHTTP statusvalid HTTP status code
schemaanyValidation schemaExpected schema for schemaValidator configuration property
matchObjectobject.toMatchObjectany object
matchObjectInArrayobjectMatches an object within an arrayany object
toEqualany.toEqualany
lengthnumberMatches length of responsenumber
spiesobjectChecks whether specific spies got called[mySpy] or [{ spy: mySpy, amount: 2 }]
errorErrorPotential error to validate againstJavascript Error
pathsobjectValidate custom return keys besides dataPath{'meta.count': 1}

Error support for @tree-house/errors.

To Do

Unfortunately no time could be found yet to add automated tests. This is the first thing planned now we have a stable version 3 released.

Tests

You can run npm run test to run all tests You can run npm run test:coverage to run all tests with coverage report

Authors

See the list of contributors who participated in this project.

License

This project is licensed under the ISC License - see the LICENSE.md file for details

6.0.0

6 months ago

5.1.0

1 year ago

5.0.0

1 year ago

4.0.0

3 years ago

3.0.0

3 years ago

2.0.6-2

3 years ago

2.0.6-1

3 years ago

2.0.6-0

4 years ago

2.0.5-0

4 years ago

2.0.4-0

4 years ago

2.0.3-0

4 years ago

2.0.2-0

4 years ago

2.0.1-0

4 years ago

2.0.0-0

4 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago