|
6 | 6 | 'use strict' |
7 | 7 |
|
8 | 8 | const test = require('node:test') |
| 9 | +const dc = require('node:diagnostics_channel') |
9 | 10 | const resolvePackageVersion = require('#agentlib/subscribers/resolve-package-version.js') |
10 | 11 |
|
| 12 | +const nrPkg = require('../../../../package.json') |
| 13 | +const Foo = require('./fixtures/foo/index.js') |
| 14 | +const Bar = require('./fixtures/foo/lib/bar.js') |
| 15 | +const Baz = require('./fixtures/baz/index.js') |
| 16 | + |
11 | 17 | test.beforeEach((ctx) => { |
12 | 18 | ctx.nr = { |
13 | 19 | logs: { |
@@ -36,96 +42,72 @@ test('logs if module cannot be found', (t) => { |
36 | 42 | ]) |
37 | 43 | }) |
38 | 44 |
|
39 | | -test('returns unknown if manifest does not have version field', (t) => { |
40 | | - t.plan(4) |
41 | | - const req = (specifier) => { |
42 | | - t.assert.equal(specifier, 'test/package.json') |
43 | | - return {} |
44 | | - } |
45 | | - req.resolve = (specifier) => { |
46 | | - t.assert.equal(specifier, 'foo') |
47 | | - return 'test/foo' |
48 | | - } |
| 45 | +test('iterates up the tree to the package.json', (t) => { |
| 46 | + t.plan(2) |
49 | 47 |
|
50 | | - const result = resolvePackageVersion('foo', { ...t.nr, req }) |
51 | | - t.assert.equal(result, 'unknown') |
52 | | - t.assert.deepStrictEqual(t.nr.logs.trace, [ |
53 | | - [ |
54 | | - { moduleSpecifier: 'foo', version: 'unknown' }, |
55 | | - 'Resolved package version.' |
56 | | - ] |
57 | | - ]) |
58 | | -}) |
| 48 | + t.after(() => { |
| 49 | + dc.unsubscribe('bar.test', handler) |
| 50 | + }) |
59 | 51 |
|
60 | | -test('iterates up the tree to the package.json', (t) => { |
61 | | - t.plan(3) |
62 | | - const req = (specifier) => { |
63 | | - if (specifier === 'test/lib/package.json') { |
64 | | - throw Error('not found') |
65 | | - } |
66 | | - if (specifier.endsWith('test/package.json')) { |
67 | | - return { version: '1.0.0' } |
68 | | - } |
69 | | - } |
70 | | - req.resolve = (specifier) => { |
71 | | - t.assert.equal(specifier, 'foo') |
72 | | - return 'test/lib/foo' |
73 | | - } |
| 52 | + dc.subscribe('bar.test', handler) |
| 53 | + const bar = new Bar() |
| 54 | + bar.bar() |
74 | 55 |
|
75 | | - const result = resolvePackageVersion('foo', { ...t.nr, req }) |
76 | | - t.assert.equal(result, '1.0.0') |
77 | | - t.assert.deepStrictEqual(t.nr.logs.trace, [ |
78 | | - [ |
79 | | - { moduleSpecifier: 'foo', version: '1.0.0' }, |
80 | | - 'Resolved package version.' |
81 | | - ] |
82 | | - ]) |
| 56 | + function handler() { |
| 57 | + const result = resolvePackageVersion('foo', t.nr) |
| 58 | + t.assert.equal(result, '1.0.0') |
| 59 | + t.assert.deepStrictEqual(t.nr.logs.trace, [ |
| 60 | + [ |
| 61 | + { moduleSpecifier: 'foo', version: '1.0.0' }, |
| 62 | + 'Resolved package version.' |
| 63 | + ] |
| 64 | + ]) |
| 65 | + } |
83 | 66 | }) |
84 | 67 |
|
85 | 68 | test('stops looking after reaching app root', (t) => { |
86 | | - t.plan(3) |
87 | | - const req = (specifier) => { |
88 | | - if (specifier === 'test/lib/package.json') { |
89 | | - throw Error('not found') |
90 | | - } |
91 | | - if (specifier.endsWith('test/package.json')) { |
92 | | - throw Error('try again') |
93 | | - } |
94 | | - throw Error('app root, no manifest') |
95 | | - } |
96 | | - req.resolve = (specifier) => { |
97 | | - t.assert.equal(specifier, 'foo') |
98 | | - return 'test/lib/foo' |
99 | | - } |
| 69 | + t.plan(2) |
100 | 70 |
|
101 | | - const result = resolvePackageVersion('foo', { ...t.nr, req }) |
102 | | - t.assert.equal(result, 'unknown') |
103 | | - t.assert.deepStrictEqual(t.nr.logs.trace, [ |
104 | | - [ |
105 | | - { moduleSpecifier: 'foo', version: 'unknown' }, |
106 | | - 'Resolved package version.' |
107 | | - ] |
108 | | - ]) |
| 71 | + t.after(() => { |
| 72 | + dc.unsubscribe('baz.test', handler) |
| 73 | + }) |
| 74 | + |
| 75 | + dc.subscribe('baz.test', handler) |
| 76 | + const baz = new Baz() |
| 77 | + baz.baz() |
| 78 | + |
| 79 | + function handler() { |
| 80 | + const result = resolvePackageVersion('baz', t.nr) |
| 81 | + t.assert.equal(result, nrPkg.version) |
| 82 | + t.assert.deepStrictEqual(t.nr.logs.trace, [ |
| 83 | + [ |
| 84 | + { moduleSpecifier: 'baz', version: nrPkg.version }, |
| 85 | + 'Resolved package version.' |
| 86 | + ] |
| 87 | + ]) |
| 88 | + } |
109 | 89 | }) |
110 | 90 |
|
111 | 91 | test('returns version', (t) => { |
112 | | - t.plan(5) |
113 | | - const req = (specifier) => { |
114 | | - t.assert.equal(specifier, 'test/package.json') |
115 | | - return { version: '1.0.0' } |
116 | | - } |
117 | | - req.resolve = (specifier) => { |
118 | | - t.assert.equal(specifier, 'foo') |
119 | | - return 'test/foo' |
120 | | - } |
| 92 | + t.plan(2) |
121 | 93 |
|
122 | | - const result = resolvePackageVersion('foo', { ...t.nr, req }) |
123 | | - t.assert.equal(result, '1.0.0') |
124 | | - t.assert.deepStrictEqual(t.nr.logs.trace, [ |
125 | | - [ |
126 | | - { moduleSpecifier: 'foo', version: '1.0.0' }, |
127 | | - 'Resolved package version.' |
128 | | - ] |
129 | | - ]) |
130 | | - t.assert.equal(t.nr.logs.warn.length, 0) |
| 94 | + t.after(() => { |
| 95 | + dc.unsubscribe('foo.test', handler) |
| 96 | + }) |
| 97 | + |
| 98 | + dc.subscribe('foo.test', handler) |
| 99 | + const foo = new Foo() |
| 100 | + foo.foo() |
| 101 | + |
| 102 | + // eslint-disable-next-line sonarjs/no-identical-functions |
| 103 | + function handler() { |
| 104 | + const result = resolvePackageVersion('foo', t.nr) |
| 105 | + t.assert.equal(result, '1.0.0') |
| 106 | + t.assert.deepStrictEqual(t.nr.logs.trace, [ |
| 107 | + [ |
| 108 | + { moduleSpecifier: 'foo', version: '1.0.0' }, |
| 109 | + 'Resolved package version.' |
| 110 | + ] |
| 111 | + ]) |
| 112 | + } |
131 | 113 | }) |
0 commit comments