0.7.2 • Published 7 months ago

fruster-test-utils v0.7.2

Weekly downloads
308
License
-
Repository
-
Last release
7 months ago

Fruster test utils

Utils for jasmine tests.

Start and stop a service beforeEach/beforeAll

Convenient method to start nats, connect bus, start mongo db and a service before each or all tests.

describe("Foo spec", () => {

	// or use startBeforeAll
	testUtils.startBeforeEach({
		service: service,
		bus: bus
	});

});

If you need to do something after beforeAll/Each finished but before tests are started:

describe("Foo spec", () => {

	// or use startBeforeAll
	testUtils.startBeforeEach({
		service: service,
		bus: bus,
		afterStart: (connection) => {
			repo = new Repo(connection.db);
			return Promise.resolve();
		},
		beforeStop: (connection) => {
			// do something before connections are being stopped
			// such as closing a HTTP server for example
		}
	});

});

Mock a service

Mock sequence of responses

To mock a service and return different response on each invocation:

const mockHello = testUtils.mockService({
	// subject to listen on
	subject: "foo-service.hello",

	// array of responses, where first request will receive first response etc
	responses: [
		{
			data: {
				foo: "first response"
			}
		},
		{
			data: {
				foo: "second response"
			}
		}
	]
});

// Send requests
bus.request({
	subject: "foo-service.hello",
	req: {
		data: "first request"
	}
});
bus.request({
	subject: "foo-service.hello",
	req: {
		data: "second request"
	}
});

// Set expectations
expect(mockHello.invocations).toBe(2);
expect(mockHello.requests[0].data).tobe("first request");
expect(mockHello.requests[1].data).tobe("second request");

Mock a response

Perform expectations in callback:

const mockHelloWorld = testUtils.mockService({
	subject: "foo-service.hello-world",
	response: {
		data: "hello world"
	}
});

bus.request({
	subject: "foo-service.hello-world",
	req: {
		data: "hello?"
	}
});

expect(mockHelloWorld.requests[0].data).toBe("hello?")
0.7.2

7 months ago

0.7.1

9 months ago

0.7.0

9 months ago

0.6.9

4 years ago

0.6.5

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.1

4 years ago

0.5.0

6 years ago

0.4.0

7 years ago

0.3.22

7 years ago

0.3.21

7 years ago

0.3.20

8 years ago

0.3.19

8 years ago

0.3.18

8 years ago

0.3.17

8 years ago

0.3.16

8 years ago

0.3.15

8 years ago

0.3.14

8 years ago

0.3.13

8 years ago

0.3.12

8 years ago

0.3.11

8 years ago

0.3.10

8 years ago

0.3.9

8 years ago

0.3.8

8 years ago

0.3.7

8 years ago

0.3.6

8 years ago

0.3.5

8 years ago

0.3.4

8 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago