6.0.0 • Published 8 months ago

@janiscommerce/api-test v6.0.0

Weekly downloads
42
License
ISC
Repository
github
Last release
8 months ago

api-test

Build Status Coverage Status npm version

A package for testing APIs developed with @janiscommerce/api. api-test should be used for testing purposes only, because allows you to test your APIs.

Installation

npm install @janiscommerce/api-test --save-dev

Usage

api-test is called with a function that receives an API class and an Array of rules. For each rule, the module will create an it() block for creating an individual test.

const APITest = require('@janiscommerce/api-test');
const MyAPIClass = require('path/to/my-api');

describe('MyAPI Tests', () => {

	APITest(MyAPIClass, [{
		description: 'should do something when other thing happend',
		request: {
			data: { foo: 123 }
		},
		response: {
			code: 200, // default
			body: { result: 1 }
		}
	}]);

});

API Parameters

The api tester can receive 3 parameters MyAPIClass[, endpoint], rules

  • MyAPIClass object The class to test

  • endpoint string Optional The endpoint that should use to run the test

  • rules array Array of test that will run individual.

const APITest = require('@janiscommerce/api-test');
const MyAPIClass = require('path/to/my-api');

describe('MyAPI Tests', () => {

	APITest(MyAPIClass, '/path/to/my-api', [{
		description: 'should do something when other thing happend',
		request: {
			data: { foo: 123 },
			endpoint: '/custom/endpoint/for/current/test'
		},
		response: {
			code: 200, // default
			body: { result: 1 }
		}
	}]);

});

Rule components

A rule is each test that will run individual. These are the components of a rule.

  • description string The text that will be added in the it() function. This fields is required.

  • only boolean If it's set to true, only this rule will be executed. Useful to debug when a test fails.

  • session object || boolean An object with the API session, or true to set a default session. If it's falsy, it won't inject the session. For session details see @janiscommerce/api-session

  • client object An object with the client that the session would fetch from BM in client getter. It it's not set, a default client is used. For session getters details see @janiscommerce/api-session

  • request object An object with the request data. This field is

  • request.data object Optional data that the API will received.

  • request.endpoint string Optional endpoint that the execution has to use for that specific rule.

  • request.pathParameters object Optional pathParameters that the API will received.

  • request.headers object Optional headers that the API will received.

  • request.cookies object Optional cookies that the API will received.

  • response object The response. This field is required.

  • response.code number The response code expected. The default value is 200.

  • response.headers object The response headers expected. This field is not strict. Will check each header individually.

  • response.strictHeaders object The response headers expected. This field is strict. Will check all headers given against all headers that the API will respond.

  • response.cookies object The response cookies expected. This field is not strict. Will check each cookie individually.

  • response.strictCookies object The response cookies expected. This field is strict. Will check all cookies given against all headers that the API will respond.

  • before function An optional function for configuring mocks or stubbing some things. Receives a sinon sandbox, this sanbox will be restored after each test (with afterEach() :wink:). Example:

{
	description: 'should get using the model and return 200 http response code',
	before: sandbox => {
		sandbox.stub(SomeModel.prototype, 'get');
	},
	response: { code: 200 }
};
  • getResponse function An optional function. Returns the response before the assertions. This is helpful for debugging reasons. Example:
{
	description: 'should return 200',
	getResponse: response => {
		console.log(response); // use this code for debugging if your test fails!
	},
	response: { code: 200 }
};
  • after function An optional function for testing additional stuff. Receives the API response object and the sinon sandbox. Example:
{
	description: 'should get using the model and return the formatted items',
	request: {
		data: { id: 1 }
	},
	before: sandbox => {
		sandbox.stub(SomeModel.prototype, 'get')
			.returns([{ foo: 1 }, { bar: 2 }]);
	},
	response: {
		body: [{
			foo: 1,
			formattedByAPI: true
		}, {
			bar: 2,
			formattedByAPI: true
		}]
	},
	after: (response, sandbox) => {
		sandbox.assert.calledOnce(SomeModel.prototype.get);
		sandbox.assert.calledWithExactly(SomeModel.prototype.get.getCall(0), { id: 1 });

		// do somthing nice with response...
	}
};

Rule validation errors

When you configure a rule, api-test will throw an APITestError if there are a validation error. These are the possible validation errors.

CodeDescription
1Invalid Rules. It means that you didn't sent an array of rules.
2Invalid Rule format. It means that you didn't sent an object for a rule.
3Invalid Rule description. It means that the description is missing or isn't a string.
4Invalid Rule request. It means that the request you sent was not an object.
5Invalid Rule response. It means that the response is missing or was not an object.
6Invalid Rule response code. It means that the response code you sent was not a number.
7Invalid Rule response headers. It means that the response headers you sent was not an object.
8Invalid Rule response cookies. It means that the response cookies you sent was not an object.
9Invalid Rule session. It means that the API Session you sent was not an object nor a boolean.
10Invalid Rule client. It means that the Client you sent was not an object.
6.0.0

8 months ago

5.0.0

1 year ago

4.3.0

1 year ago

4.2.1

2 years ago

4.2.0

2 years ago

4.1.0

2 years ago

4.1.1

2 years ago

4.0.0

4 years ago

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.3.0

4 years ago

2.3.1

4 years ago

2.2.0

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago