File: test.js

package info (click to toggle)
node-package-json 8.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 176 kB
  • sloc: javascript: 347; makefile: 3
file content (117 lines) | stat: -rw-r--r-- 3,696 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {promisify} from 'node:util';
import http from 'node:http';
import test from 'ava';
import privateRegistry from 'mock-private-registry/promise.js';
import packageJson, {PackageNotFoundError, VersionNotFoundError} from './index.js';

test('latest version', async t => {
	const json = await packageJson('ava');
	t.is(json.name, 'ava');
	t.falsy(json.versions);
});

test('full metadata', async t => {
	const json = await packageJson('pageres', {
		fullMetadata: true,
		version: '4.4.0',
	});
	t.is(json.name, 'pageres');
	t.is(json._id, 'pageres@4.4.0');
	t.is(json.time.created, '2014-02-07T18:17:46.737Z');
});

test('all version', async t => {
	const json = await packageJson('pageres', {allVersions: true});
	t.is(json.name, 'pageres');
	t.is(json.versions['0.1.0'].name, 'pageres');
});

test('specific version', async t => {
	const json = await packageJson('pageres', {version: '0.1.0'});
	t.is(json.version, '0.1.0');
});

test('incomplete version x', async t => {
	const json = await packageJson('pageres', {version: '0'});
	t.is(json.version.slice(0, 2), '0.');
});

// TODO: Find an alternative npm instance.
// test.failing('custom registry url', async t => {
// 	const json = await packageJson('ava', {registryUrl: 'https://npm.open-registry.dev/'});
// 	t.is(json.name, 'ava');
// 	t.falsy(json.versions);
// });

test('scoped - latest version', async t => {
	const json = await packageJson('@sindresorhus/df');
	t.is(json.name, '@sindresorhus/df');
});

test('scoped - full metadata', async t => {
	const json = await packageJson('@sindresorhus/df', {
		fullMetadata: true,
		version: '1.0.1',
	});
	t.is(json.name, '@sindresorhus/df');
	t.is(json._id, '@sindresorhus/df@1.0.1');
	t.is(json.time.created, '2015-05-04T18:10:02.416Z');
	t.is(json.time.modified, '2022-06-12T23:49:38.166Z');
});

test('scoped - all version', async t => {
	const json = await packageJson('@sindresorhus/df', {allVersions: true});
	t.is(json.name, '@sindresorhus/df');
	t.is(json.versions['1.0.1'].name, '@sindresorhus/df');
});

test('scoped - specific version', async t => {
	const json = await packageJson('@sindresorhus/df', {version: '1.0.1'});
	t.is(json.version, '1.0.1');
});

test('scoped - dist tag', async t => {
	const json = await packageJson('@rexxars/npmtest', {version: 'next'});
	t.is(json.version, '2.0.0');
});

test('reject when package doesn\'t exist', async t => {
	await t.throwsAsync(packageJson('nnnope'), {instanceOf: PackageNotFoundError});
});

test('reject when version doesn\'t exist', async t => {
	await t.throwsAsync(packageJson('hapi', {version: '6.6.6'}), {instanceOf: VersionNotFoundError});
});

/* fail with autopkgtest
test('does not send any auth token for unconfigured registries', async t => {
	const server = http.createServer((request, response) => {
		response.end(JSON.stringify({headers: request.headers, 'dist-tags': {}}));
	});

	await promisify(server.listen.bind(server))(63_144, '127.0.0.1');
	const json = await packageJson('@mockscope3/foobar', {allVersions: true});
	t.is(json.headers.host, 'localhost:63144');
	t.is(json.headers.authorization, undefined);
	await promisify(server.close.bind(server))();
});

test('private registry (bearer token)', async t => {
	const server = await privateRegistry();
	const json = await packageJson('@mockscope/foobar');
	t.is(json.name, '@mockscope/foobar');
	server.close();
});

test('private registry (basic token)', async t => {
	const server = await privateRegistry({
		port: 63_143,
		pkgName: '@mockscope2/foobar',
		token: 'QWxhZGRpbjpPcGVuU2VzYW1l',
		tokenType: 'Basic',
	});
	const json = await packageJson('@mockscope2/foobar');
	t.is(json.name, '@mockscope2/foobar');
	server.close();
});*/