Skip to content

Commit a30fdd1

Browse files
committed
Mock many more tests.
1 parent 4f63895 commit a30fdd1

File tree

10 files changed

+100
-58
lines changed

10 files changed

+100
-58
lines changed

test/deep.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import path from 'path'
66
import spawn from 'spawn-please'
77
import ncu from '../src/'
88
import mergeOptions from '../src/lib/mergeOptions'
9+
import stubNpmView from './helpers/stubNpmView'
910

1011
chai.should()
1112
chai.use(chaiAsPromised)
@@ -48,6 +49,10 @@ const setupDeepTest = async () => {
4849
describe('--deep', function () {
4950
this.timeout(60000)
5051

52+
let stub: { restore: () => void }
53+
before(() => (stub = stubNpmView('99.9.9', { spawn: true })))
54+
after(() => stub.restore())
55+
5156
it('do not allow --packageFile and --deep together', async () => {
5257
await ncu({ packageFile: './package.json', deep: true }).should.eventually.be.rejectedWith('Cannot specify both')
5358
})
@@ -123,6 +128,10 @@ describe('--deep with nested ncurc files', function () {
123128

124129
this.timeout(60000)
125130

131+
let stub: { restore: () => void }
132+
before(() => (stub = stubNpmView('99.9.9', { spawn: true })))
133+
after(() => stub.restore())
134+
126135
it('use ncurc of nested packages', async () => {
127136
const deepJsonOut = await spawn('node', [bin, '--jsonUpgraded', '--deep'], { cwd }).then(JSON.parse)
128137

test/filter.test.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,29 @@ const bin = path.join(__dirname, '../build/src/bin/cli.js')
1515

1616
describe('filter', () => {
1717
describe('module', () => {
18+
let stub: { restore: () => void }
19+
before(() => (stub = stubNpmView('99.9.9')))
20+
after(() => stub.restore())
21+
1822
it('filter by package name with one arg', async () => {
19-
const stub = stubNpmView('99.9.9')
2023
const upgraded = (await ncu({
2124
packageData: await fs.readFile(path.join(__dirname, 'test-data/ncu/package2.json'), 'utf-8'),
2225
filter: ['lodash.map'],
2326
})) as Index<string>
2427
upgraded.should.have.property('lodash.map')
2528
upgraded.should.not.have.property('lodash.filter')
26-
stub.restore()
2729
})
2830

2931
it('filter by package name with multiple args', async () => {
30-
const stub = stubNpmView('99.9.9')
3132
const upgraded = (await ncu({
3233
packageData: await fs.readFile(path.join(__dirname, 'test-data/ncu/package2.json'), 'utf-8'),
3334
filter: ['lodash.map', 'lodash.filter'],
3435
})) as Index<string>
3536
upgraded.should.have.property('lodash.map')
3637
upgraded.should.have.property('lodash.filter')
37-
stub.restore()
3838
})
3939

4040
it('filter with wildcard', async () => {
41-
const stub = stubNpmView('99.9.9')
4241
const upgraded = (await ncu({
4342
packageData: {
4443
dependencies: {
@@ -51,11 +50,9 @@ describe('filter', () => {
5150
})) as Index<string>
5251
upgraded.should.have.property('lodash.map')
5352
upgraded.should.have.property('lodash.filter')
54-
stub.restore()
5553
})
5654

5755
it('filter with wildcard for scoped package', async () => {
58-
const stub = stubNpmView('99.9.9')
5956
const pkg = {
6057
dependencies: {
6158
vite: '1.0.0',
@@ -98,12 +95,9 @@ describe('filter', () => {
9895
upgraded!.should.not.have.property('@vitejs/plugin-react')
9996
upgraded!.should.have.property('@vitejs/plugin-vue')
10097
}
101-
102-
stub.restore()
10398
})
10499

105100
it('filter with negated wildcard', async () => {
106-
const stub = stubNpmView('99.9.9')
107101
const upgraded = (await ncu({
108102
packageData: {
109103
dependencies: {
@@ -115,11 +109,9 @@ describe('filter', () => {
115109
filter: ['!lodash.*'],
116110
})) as Index<string>
117111
upgraded.should.have.property('lodash')
118-
stub.restore()
119112
})
120113

121114
it('filter with regex string', async () => {
122-
const stub = stubNpmView('99.9.9')
123115
const upgraded = (await ncu({
124116
packageData: {
125117
dependencies: {
@@ -132,11 +124,9 @@ describe('filter', () => {
132124
})) as Index<string>
133125
upgraded.should.have.property('lodash.map')
134126
upgraded.should.have.property('lodash.filter')
135-
stub.restore()
136127
})
137128

138129
it('filter with array of strings', async () => {
139-
const stub = stubNpmView('99.9.9')
140130
const upgraded = (await ncu({
141131
packageData: {
142132
dependencies: {
@@ -149,11 +139,9 @@ describe('filter', () => {
149139
})) as Index<string>
150140
upgraded.should.have.property('lodash.map')
151141
upgraded.should.have.property('lodash.filter')
152-
stub.restore()
153142
})
154143

155144
it('filter with array of regex', async () => {
156-
const stub = stubNpmView('99.9.9')
157145
const upgraded = (await ncu({
158146
packageData: {
159147
dependencies: {
@@ -168,11 +156,9 @@ describe('filter', () => {
168156
upgraded.should.have.property('lodash.map')
169157
upgraded.should.have.property('lodash.filter')
170158
upgraded.should.have.property('fp-and-or')
171-
stub.restore()
172159
})
173160

174161
it('filter with array of regex strings', async () => {
175-
const stub = stubNpmView('99.9.9')
176162
const upgraded = (await ncu({
177163
packageData: {
178164
dependencies: {
@@ -187,22 +173,23 @@ describe('filter', () => {
187173
upgraded.should.have.property('lodash.map')
188174
upgraded.should.have.property('lodash.filter')
189175
upgraded.should.have.property('fp-and-or')
190-
stub.restore()
191176
})
192177

193178
it('trim and ignore empty filter', async () => {
194-
const stub = stubNpmView('99.9.9')
195179
const upgraded = (await ncu({
196180
packageData: await fs.readFile(path.join(__dirname, 'test-data/ncu/package2.json'), 'utf-8'),
197181
filter: [],
198182
})) as Index<string>
199183
upgraded.should.have.property('lodash.map')
200184
upgraded.should.have.property('lodash.filter')
201-
stub.restore()
202185
})
203186
})
204187

205188
describe('cli', () => {
189+
let stub: { restore: () => void }
190+
before(() => (stub = stubNpmView('99.9.9', { spawn: true })))
191+
after(() => stub.restore())
192+
206193
it('filter by package name with --filter', async () => {
207194
const output = await spawn(
208195
'node',
@@ -295,6 +282,10 @@ describe('filter', () => {
295282

296283
describe('reject', () => {
297284
describe('cli', () => {
285+
let stub: { restore: () => void }
286+
before(() => (stub = stubNpmView('99.9.9', { spawn: true })))
287+
after(() => stub.restore())
288+
298289
it('reject by package name with --reject', async () => {
299290
const output = await spawn(
300291
'node',

test/filterVersion.test.ts

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@ const bin = path.join(__dirname, '../build/src/bin/cli.js')
1313

1414
describe('filterVersion', () => {
1515
describe('module', () => {
16-
it('filter by package version with string', async () => {
17-
const stub = stubNpmView({
16+
let stub: { restore: () => void }
17+
before(() => {
18+
stub = stubNpmView({
1819
'ncu-test-v2': '2.0.0',
1920
'ncu-test-return-version': '2.0.0',
2021
})
22+
})
23+
after(() => {
24+
stub.restore()
25+
})
2126

27+
it('filter by package version with string', async () => {
2228
const pkg = {
2329
dependencies: {
2430
'ncu-test-v2': '1.0.0',
@@ -38,12 +44,6 @@ describe('filterVersion', () => {
3844
})
3945

4046
it('filter by package version with space-delimited list of strings', async () => {
41-
const stub = stubNpmView({
42-
'ncu-test-v2': '2.0.0',
43-
'ncu-test-return-version': '2.0.0',
44-
'fp-and-or': '0.1.3',
45-
})
46-
4747
const pkg = {
4848
dependencies: {
4949
'ncu-test-v2': '1.0.0',
@@ -60,17 +60,9 @@ describe('filterVersion', () => {
6060
upgraded!.should.have.property('ncu-test-v2')
6161
upgraded!.should.not.have.property('ncu-test-return-version')
6262
upgraded!.should.have.property('fp-and-or')
63-
64-
stub.restore()
6563
})
6664

6765
it('filter by package version with comma-delimited list of strings', async () => {
68-
const stub = stubNpmView({
69-
'ncu-test-v2': '2.0.0',
70-
'ncu-test-return-version': '2.0.0',
71-
'fp-and-or': '0.1.3',
72-
})
73-
7466
const pkg = {
7567
dependencies: {
7668
'ncu-test-v2': '1.0.0',
@@ -87,17 +79,9 @@ describe('filterVersion', () => {
8779
upgraded!.should.have.property('ncu-test-v2')
8880
upgraded!.should.not.have.property('ncu-test-return-version')
8981
upgraded!.should.have.property('fp-and-or')
90-
91-
stub.restore()
9282
})
9383

9484
it('filter by package version with RegExp', async () => {
95-
const stub = stubNpmView({
96-
'ncu-test-v2': '2.0.0',
97-
'ncu-test-return-version': '2.0.0',
98-
'fp-and-or': '0.1.3',
99-
})
100-
10185
const pkg = {
10286
dependencies: {
10387
'ncu-test-v2': '1.0.0',
@@ -114,17 +98,9 @@ describe('filterVersion', () => {
11498
upgraded!.should.have.property('ncu-test-v2')
11599
upgraded!.should.have.property('ncu-test-return-version')
116100
upgraded!.should.not.have.property('fp-and-or')
117-
118-
stub.restore()
119101
})
120102

121103
it('filter by package version with RegExp string', async () => {
122-
const stub = stubNpmView({
123-
'ncu-test-v2': '2.0.0',
124-
'ncu-test-return-version': '2.0.0',
125-
'fp-and-or': '0.1.3',
126-
})
127-
128104
const pkg = {
129105
dependencies: {
130106
'ncu-test-v2': '1.0.0',
@@ -141,13 +117,12 @@ describe('filterVersion', () => {
141117
upgraded!.should.have.property('ncu-test-v2')
142118
upgraded!.should.have.property('ncu-test-return-version')
143119
upgraded!.should.not.have.property('fp-and-or')
144-
145-
stub.restore()
146120
})
147121
})
148122

149123
describe('cli', () => {
150124
it('allow multiple --filterVersion options', async () => {
125+
const stub = stubNpmView('99.9.9', { spawn: true })
151126
const pkgData = {
152127
dependencies: {
153128
'ncu-test-v2': '1.0.0',
@@ -163,13 +138,15 @@ describe('filterVersion', () => {
163138
const upgraded = JSON.parse(output)
164139
upgraded.should.have.property('ncu-test-v2')
165140
upgraded.should.have.property('ncu-test-10')
141+
stub.restore()
166142
})
167143
})
168144
})
169145

170146
describe('rejectVersion', () => {
171147
describe('cli', () => {
172148
it('allow multiple --rejectVersion options', async () => {
149+
const stub = stubNpmView('99.9.9', { spawn: true })
173150
const pkgData = {
174151
dependencies: {
175152
'ncu-test-v2': '1.0.0',
@@ -185,6 +162,7 @@ describe('rejectVersion', () => {
185162
const upgraded = JSON.parse(output)
186163
upgraded.should.not.have.property('ncu-test-v2')
187164
upgraded.should.not.have.property('ncu-test-10')
165+
stub.restore()
188166
})
189167
})
190168
})

test/format.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import chai, { expect } from 'chai'
22
import chaiString from 'chai-string'
33
import path from 'path'
44
import spawn from 'spawn-please'
5+
import stubNpmView from './helpers/stubNpmView'
56

67
chai.should()
78
chai.use(chaiString)
@@ -12,12 +13,23 @@ const bin = path.join(__dirname, '../build/src/bin/cli.js')
1213

1314
describe('--format time', () => {
1415
it('show publish time', async () => {
16+
const timestamp = '2020-04-27T21:48:11.660Z'
17+
const stub = stubNpmView(
18+
{
19+
version: '99.9.9',
20+
time: {
21+
'99.9.9': timestamp,
22+
},
23+
},
24+
{ spawn: true },
25+
)
1526
const packageData = {
1627
dependencies: {
1728
'ncu-test-v2': '^1.0.0',
1829
},
1930
}
2031
const output = await spawn('node', [bin, '--format', 'time', '--stdin'], JSON.stringify(packageData))
21-
expect(output).contains('2020-04-27T21:48:11.660Z')
32+
expect(output).contains(timestamp)
33+
stub.restore()
2234
})
2335
})

test/group.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import os from 'os'
66
import path from 'path'
77
import spawn from 'spawn-please'
88
import { GroupFunction } from '../src/types/GroupFunction'
9+
import stubNpmView from './helpers/stubNpmView'
910

1011
chai.should()
1112
chai.use(chaiAsPromised)
@@ -21,6 +22,15 @@ async function groupTestScaffold(
2122
groupFn: GroupFunction,
2223
expectedOutput: string,
2324
): Promise<void> {
25+
const stub = stubNpmView(
26+
{
27+
'ncu-test-v2': '2.0.0',
28+
'ncu-test-tag': '1.1.0',
29+
'ncu-test-return-version': '2.0.0',
30+
},
31+
{ spawn: true },
32+
)
33+
2434
// use dynamic import for ESM module
2535
const { default: stripAnsi } = await import('strip-ansi')
2636
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
@@ -41,6 +51,7 @@ async function groupTestScaffold(
4151
stripAnsi(stdout).should.containIgnoreCase(expectedOutput)
4252
} finally {
4353
await fs.rm(tempDir, { recursive: true, force: true })
54+
stub.restore()
4455
}
4556
}
4657

test/interactive.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import fs from 'fs/promises'
55
import os from 'os'
66
import path from 'path'
77
import spawn from 'spawn-please'
8+
import stubNpmView from './helpers/stubNpmView'
89

910
const should = chai.should()
1011
chai.use(chaiAsPromised)
@@ -13,6 +14,21 @@ chai.use(chaiString)
1314
const bin = path.join(__dirname, '../build/src/bin/cli.js')
1415

1516
describe('--interactive', () => {
17+
let stub: { restore: () => void }
18+
before(() => {
19+
stub = stubNpmView(
20+
{
21+
'ncu-test-v2': '2.0.0',
22+
'ncu-test-tag': '1.1.0',
23+
'ncu-test-return-version': '2.0.0',
24+
},
25+
{ spawn: true },
26+
)
27+
})
28+
after(() => {
29+
stub.restore()
30+
})
31+
1632
it('prompt for each upgraded dependency', async () => {
1733
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
1834
const pkgFile = path.join(tempDir, 'package.json')

0 commit comments

Comments
 (0)