Skip to content

Commit f6376db

Browse files
committed
Fix mocked tests
1 parent 90df407 commit f6376db

File tree

1 file changed

+108
-93
lines changed

1 file changed

+108
-93
lines changed

test/path-resolve.test.ts

Lines changed: 108 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,18 @@ import { normalizePath } from '@socketsecurity/registry/lib/path'
99
import { getPackageFilesFullScans } from './dist/path-resolve'
1010

1111
const testPath = __dirname
12-
const mockPath = normalizePath(path.join(testPath, 'mock'))
12+
const rootNmPath = path.join(testPath, '../node_modules')
13+
14+
const mockFixturePath = normalizePath(path.join(testPath, 'mock'))
15+
const mockNmPath = normalizePath(rootNmPath)
16+
const mockedNmCallback = mockFs.load(rootNmPath)
17+
18+
function mockTestFs(config) {
19+
return mockFs({
20+
...config,
21+
[mockNmPath]: mockedNmCallback
22+
})
23+
}
1324

1425
const globPatterns = {
1526
general: {
@@ -68,7 +79,7 @@ const sortedPromise =
6879
const result = await fn(...args)
6980
return result.sort()
7081
}
71-
const sortedGetPackageFiles = sortedPromise(getPackageFilesFullScans)
82+
const sortedGetPackageFilesFullScans = sortedPromise(getPackageFilesFullScans)
7283

7384
describe('Path Resolve', () => {
7485
beforeEach(() => {
@@ -83,31 +94,33 @@ describe('Path Resolve', () => {
8394
}
8495
})
8596

86-
describe('getPackageFiles()', () => {
97+
describe('getPackageFilesFullScans()', () => {
8798
it('should handle a "." inputPath', async () => {
88-
mockFs({
89-
[`${mockPath}/package.json`]: '{}'
99+
mockTestFs({
100+
[`${mockFixturePath}/package.json`]: '{}'
90101
})
91102

92-
const actual = await sortedGetPackageFiles(
93-
mockPath,
103+
const actual = await sortedGetPackageFilesFullScans(
104+
mockFixturePath,
94105
['.'],
95106
globPatterns,
96107
undefined
97108
)
98-
expect(actual.map(normalizePath)).toEqual([`${mockPath}/package.json`])
109+
expect(actual.map(normalizePath)).toEqual([
110+
`${mockFixturePath}/package.json`
111+
])
99112
})
100113

101114
it('should respect ignores from socket config', async () => {
102-
mockFs({
103-
[`${mockPath}/bar/package-lock.json`]: '{}',
104-
[`${mockPath}/bar/package.json`]: '{}',
105-
[`${mockPath}/foo/package-lock.json`]: '{}',
106-
[`${mockPath}/foo/package.json`]: '{}'
115+
mockTestFs({
116+
[`${mockFixturePath}/bar/package-lock.json`]: '{}',
117+
[`${mockFixturePath}/bar/package.json`]: '{}',
118+
[`${mockFixturePath}/foo/package-lock.json`]: '{}',
119+
[`${mockFixturePath}/foo/package.json`]: '{}'
107120
})
108121

109-
const actual = await sortedGetPackageFiles(
110-
mockPath,
122+
const actual = await sortedGetPackageFilesFullScans(
123+
mockFixturePath,
111124
['**/*'],
112125
globPatterns,
113126
{
@@ -118,91 +131,91 @@ describe('Path Resolve', () => {
118131
}
119132
)
120133
expect(actual.map(normalizePath)).toEqual([
121-
`${mockPath}/bar/package.json`,
122-
`${mockPath}/foo/package-lock.json`,
123-
`${mockPath}/foo/package.json`
134+
`${mockFixturePath}/bar/package.json`,
135+
`${mockFixturePath}/foo/package-lock.json`,
136+
`${mockFixturePath}/foo/package.json`
124137
])
125138
})
126139

127140
it('should respect .gitignore', async () => {
128-
mockFs({
129-
[`${mockPath}/.gitignore`]: 'bar/*\n!bar/package.json',
130-
[`${mockPath}/bar/package-lock.json`]: '{}',
131-
[`${mockPath}/bar/package.json`]: '{}',
132-
[`${mockPath}/foo/package-lock.json`]: '{}',
133-
[`${mockPath}/foo/package.json`]: '{}'
141+
mockTestFs({
142+
[`${mockFixturePath}/.gitignore`]: 'bar/*\n!bar/package.json',
143+
[`${mockFixturePath}/bar/package-lock.json`]: '{}',
144+
[`${mockFixturePath}/bar/package.json`]: '{}',
145+
[`${mockFixturePath}/foo/package-lock.json`]: '{}',
146+
[`${mockFixturePath}/foo/package.json`]: '{}'
134147
})
135148

136-
const actual = await sortedGetPackageFiles(
137-
mockPath,
149+
const actual = await sortedGetPackageFilesFullScans(
150+
mockFixturePath,
138151
['**/*'],
139152
globPatterns,
140153
undefined
141154
)
142155
expect(actual.map(normalizePath)).toEqual([
143-
`${mockPath}/bar/package.json`,
144-
`${mockPath}/foo/package-lock.json`,
145-
`${mockPath}/foo/package.json`
156+
`${mockFixturePath}/bar/package.json`,
157+
`${mockFixturePath}/foo/package-lock.json`,
158+
`${mockFixturePath}/foo/package.json`
146159
])
147160
})
148161

149162
it('should always ignore some paths', async () => {
150-
mockFs({
163+
mockTestFs({
151164
// Mirrors the list from
152165
// https://github.com/novemberborn/ignore-by-default/blob/v2.1.0/index.js
153-
[`${mockPath}/.git/some/dir/package.json`]: '{}',
154-
[`${mockPath}/.log/some/dir/package.json`]: '{}',
155-
[`${mockPath}/.nyc_output/some/dir/package.json`]: '{}',
156-
[`${mockPath}/.sass-cache/some/dir/package.json`]: '{}',
157-
[`${mockPath}/.yarn/some/dir/package.json`]: '{}',
158-
[`${mockPath}/bower_components/some/dir/package.json`]: '{}',
159-
[`${mockPath}/coverage/some/dir/package.json`]: '{}',
160-
[`${mockPath}/node_modules/socket/package.json`]: '{}',
161-
[`${mockPath}/foo/package-lock.json`]: '{}',
162-
[`${mockPath}/foo/package.json`]: '{}'
166+
[`${mockFixturePath}/.git/some/dir/package.json`]: '{}',
167+
[`${mockFixturePath}/.log/some/dir/package.json`]: '{}',
168+
[`${mockFixturePath}/.nyc_output/some/dir/package.json`]: '{}',
169+
[`${mockFixturePath}/.sass-cache/some/dir/package.json`]: '{}',
170+
[`${mockFixturePath}/.yarn/some/dir/package.json`]: '{}',
171+
[`${mockFixturePath}/bower_components/some/dir/package.json`]: '{}',
172+
[`${mockFixturePath}/coverage/some/dir/package.json`]: '{}',
173+
[`${mockFixturePath}/node_modules/socket/package.json`]: '{}',
174+
[`${mockFixturePath}/foo/package-lock.json`]: '{}',
175+
[`${mockFixturePath}/foo/package.json`]: '{}'
163176
})
164177

165-
const actual = await sortedGetPackageFiles(
166-
mockPath,
178+
const actual = await sortedGetPackageFilesFullScans(
179+
mockFixturePath,
167180
['**/*'],
168181
globPatterns,
169182
undefined
170183
)
171184
expect(actual.map(normalizePath)).toEqual([
172-
`${mockPath}/foo/package-lock.json`,
173-
`${mockPath}/foo/package.json`
185+
`${mockFixturePath}/foo/package-lock.json`,
186+
`${mockFixturePath}/foo/package.json`
174187
])
175188
})
176189

177190
it('should ignore irrelevant matches', async () => {
178-
mockFs({
179-
[`${mockPath}/foo/package-foo.json`]: '{}',
180-
[`${mockPath}/foo/package-lock.json`]: '{}',
181-
[`${mockPath}/foo/package.json`]: '{}',
182-
[`${mockPath}/foo/random.json`]: '{}'
191+
mockTestFs({
192+
[`${mockFixturePath}/foo/package-foo.json`]: '{}',
193+
[`${mockFixturePath}/foo/package-lock.json`]: '{}',
194+
[`${mockFixturePath}/foo/package.json`]: '{}',
195+
[`${mockFixturePath}/foo/random.json`]: '{}'
183196
})
184197

185-
const actual = await sortedGetPackageFiles(
186-
mockPath,
198+
const actual = await sortedGetPackageFilesFullScans(
199+
mockFixturePath,
187200
['**/*'],
188201
globPatterns,
189202
undefined
190203
)
191204
expect(actual.map(normalizePath)).toEqual([
192-
`${mockPath}/foo/package-lock.json`,
193-
`${mockPath}/foo/package.json`
205+
`${mockFixturePath}/foo/package-lock.json`,
206+
`${mockFixturePath}/foo/package.json`
194207
])
195208
})
196209

197210
it('should be lenient on oddities', async () => {
198-
mockFs({
199-
[`${mockPath}/package.json`]: {
211+
mockTestFs({
212+
[`${mockFixturePath}/package.json`]: {
200213
/* Empty directory */
201214
}
202215
})
203216

204-
const actual = await sortedGetPackageFiles(
205-
mockPath,
217+
const actual = await sortedGetPackageFilesFullScans(
218+
mockFixturePath,
206219
['**/*'],
207220
globPatterns,
208221
undefined
@@ -211,80 +224,82 @@ describe('Path Resolve', () => {
211224
})
212225

213226
it('should resolve package and lock file', async () => {
214-
mockFs({
215-
[`${mockPath}/package-lock.json`]: '{}',
216-
[`${mockPath}/package.json`]: '{}'
227+
mockTestFs({
228+
[`${mockFixturePath}/package-lock.json`]: '{}',
229+
[`${mockFixturePath}/package.json`]: '{}'
217230
})
218231

219-
const actual = await sortedGetPackageFiles(
220-
mockPath,
232+
const actual = await sortedGetPackageFilesFullScans(
233+
mockFixturePath,
221234
['**/*'],
222235
globPatterns,
223236
undefined
224237
)
225238
expect(actual.map(normalizePath)).toEqual([
226-
`${mockPath}/package-lock.json`,
227-
`${mockPath}/package.json`
239+
`${mockFixturePath}/package-lock.json`,
240+
`${mockFixturePath}/package.json`
228241
])
229242
})
230243

231244
it('should resolve package without lock file', async () => {
232-
mockFs({
233-
[`${mockPath}/package.json`]: '{}'
245+
mockTestFs({
246+
[`${mockFixturePath}/package.json`]: '{}'
234247
})
235248

236-
const actual = await sortedGetPackageFiles(
237-
mockPath,
249+
const actual = await sortedGetPackageFilesFullScans(
250+
mockFixturePath,
238251
['**/*'],
239252
globPatterns,
240253
undefined
241254
)
242-
expect(actual.map(normalizePath)).toEqual([`${mockPath}/package.json`])
255+
expect(actual.map(normalizePath)).toEqual([
256+
`${mockFixturePath}/package.json`
257+
])
243258
})
244259

245260
it('should support alternative lock files', async () => {
246-
mockFs({
247-
[`${mockPath}/yarn.lock`]: '{}',
248-
[`${mockPath}/package.json`]: '{}'
261+
mockTestFs({
262+
[`${mockFixturePath}/yarn.lock`]: '{}',
263+
[`${mockFixturePath}/package.json`]: '{}'
249264
})
250265

251-
const actual = await sortedGetPackageFiles(
252-
mockPath,
266+
const actual = await sortedGetPackageFilesFullScans(
267+
mockFixturePath,
253268
['**/*'],
254269
globPatterns,
255270
undefined
256271
)
257272
expect(actual.map(normalizePath)).toEqual([
258-
`${mockPath}/package.json`,
259-
`${mockPath}/yarn.lock`
273+
`${mockFixturePath}/package.json`,
274+
`${mockFixturePath}/yarn.lock`
260275
])
261276
})
262277

263278
it('should handle all variations', async () => {
264-
mockFs({
265-
[`${mockPath}/package-lock.json`]: '{}',
266-
[`${mockPath}/package.json`]: '{}',
267-
[`${mockPath}/foo/package-lock.json`]: '{}',
268-
[`${mockPath}/foo/package.json`]: '{}',
269-
[`${mockPath}/bar/yarn.lock`]: '{}',
270-
[`${mockPath}/bar/package.json`]: '{}',
271-
[`${mockPath}/abc/package.json`]: '{}'
279+
mockTestFs({
280+
[`${mockFixturePath}/package-lock.json`]: '{}',
281+
[`${mockFixturePath}/package.json`]: '{}',
282+
[`${mockFixturePath}/foo/package-lock.json`]: '{}',
283+
[`${mockFixturePath}/foo/package.json`]: '{}',
284+
[`${mockFixturePath}/bar/yarn.lock`]: '{}',
285+
[`${mockFixturePath}/bar/package.json`]: '{}',
286+
[`${mockFixturePath}/abc/package.json`]: '{}'
272287
})
273288

274-
const actual = await sortedGetPackageFiles(
275-
mockPath,
289+
const actual = await sortedGetPackageFilesFullScans(
290+
mockFixturePath,
276291
['**/*'],
277292
globPatterns,
278293
undefined
279294
)
280295
expect(actual.map(normalizePath)).toEqual([
281-
`${mockPath}/abc/package.json`,
282-
`${mockPath}/bar/package.json`,
283-
`${mockPath}/bar/yarn.lock`,
284-
`${mockPath}/foo/package-lock.json`,
285-
`${mockPath}/foo/package.json`,
286-
`${mockPath}/package-lock.json`,
287-
`${mockPath}/package.json`
296+
`${mockFixturePath}/abc/package.json`,
297+
`${mockFixturePath}/bar/package.json`,
298+
`${mockFixturePath}/bar/yarn.lock`,
299+
`${mockFixturePath}/foo/package-lock.json`,
300+
`${mockFixturePath}/foo/package.json`,
301+
`${mockFixturePath}/package-lock.json`,
302+
`${mockFixturePath}/package.json`
288303
])
289304
})
290305
})

0 commit comments

Comments
 (0)