File: test.js

package info (click to toggle)
node-p-finally 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 128 kB
  • sloc: javascript: 113; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 1,558 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const test = require('tape');
const pFinally = require('.');

const fixture = Symbol('fixture');
const fixtureError = new Error('error');

test('does nothing when nothing is passed', async t => {
	t.is(await pFinally(Promise.resolve(fixture)), fixture);
	t.end();
});

test('callback is called when promise is fulfilled', async t => {
	let isCalled = false;

	const value = await pFinally(Promise.resolve(fixture), () => {
		isCalled = true;
	});

	t.is(value, fixture);
	t.true(isCalled);
	t.end();
});

test('callback is called when promise is rejected', async t => {
	let isCalled = false;

	await pFinally(Promise.reject(fixtureError), () => {
		isCalled = true;
	}).catch(error => {
		t.is(error, fixtureError);
	});

	t.true(isCalled);
	t.end();
});

test('returning a rejected promise in the callback rejects the promise', async t => {
	await pFinally(Promise.resolve(fixture), () => Promise.reject(fixtureError)).then(() => {
		t.fail();
	}, error => {
		t.is(error, fixtureError);
	});
	t.end();
});

test('returning a rejected promise in the callback for an already rejected promise changes the rejection reason', async t => {
	await pFinally(Promise.reject(new Error('original error')), () => Promise.reject(fixtureError)).catch(error => {
		t.is(error, fixtureError);
	});
	t.end();
});

test('the onFinally callback is only called once no matter what', async t => {
	t.plan(1);

	await pFinally(
		Promise.resolve(),
		() => {
			t.pass();
			throw new Error(); // eslint-disable-line unicorn/error-message
		}
	).catch(() => {});
	t.end();
});