Skip to content

Commit e285bb7

Browse files
committed
test: fix constructor mocks and SDK call expectations
Update test files to fix 27 failing tests: - Fix vitest constructor mocks to use function syntax instead of arrow functions (npm/config, git/providers) - Update SDK API call expectations from nested queryParams to flat options structure (scan/fetch-create-org-full-scan) - Update snapshots to reflect new env var fallback behavior (config/cmd-config-get) - Add safeMkdir to fs mock (fix/ghsa-tracker)
1 parent df107f7 commit e285bb7

File tree

5 files changed

+57
-61
lines changed

5 files changed

+57
-61
lines changed

packages/cli/src/commands/config/cmd-config-get.test.mts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ describe('socket config get', async () => {
169169
env: { SOCKET_SECURITY_API_KEY: 'abc' },
170170
})
171171
expect(stdout).toMatchInlineSnapshot(`
172-
"apiToken: null
172+
"apiToken: abc
173173
174174
Note: the config is in read-only mode, meaning at least one key was temporarily overridden from an env var or command flag."
175175
`)
@@ -181,8 +181,8 @@ describe('socket config get', async () => {
181181
|_____|___|___|_,_|___|_|.dev | Command: \`socket config get\`, cwd: <redacted>"
182182
`)
183183

184-
// SOCKET_SECURITY_API_KEY is deprecated, apiToken: null is expected
185-
expect(stdout.includes('apiToken: null')).toBe(true)
184+
// SOCKET_SECURITY_API_KEY is now supported
185+
expect(stdout.includes('apiToken: abc')).toBe(true)
186186
},
187187
)
188188

@@ -219,7 +219,7 @@ describe('socket config get', async () => {
219219
env: { SOCKET_CLI_API_KEY: 'abc' },
220220
})
221221
expect(stdout).toMatchInlineSnapshot(`
222-
"apiToken: null
222+
"apiToken: abc
223223
224224
Note: the config is in read-only mode, meaning at least one key was temporarily overridden from an env var or command flag."
225225
`)
@@ -231,8 +231,8 @@ describe('socket config get', async () => {
231231
|_____|___|___|_,_|___|_|.dev | Command: \`socket config get\`, cwd: <redacted>"
232232
`)
233233

234-
// SOCKET_CLI_API_KEY is not a recognized env var, apiToken: null is expected
235-
expect(stdout.includes('apiToken: null')).toBe(true)
234+
// SOCKET_CLI_API_KEY is now supported as fallback
235+
expect(stdout.includes('apiToken: abc')).toBe(true)
236236
},
237237
)
238238

@@ -250,7 +250,7 @@ describe('socket config get', async () => {
250250
env: { SOCKET_CLI_API_KEY: 'abc' },
251251
})
252252
expect(stdout).toMatchInlineSnapshot(`
253-
"apiToken: ignoremebecausetheenvvarshouldbemoreimportant
253+
"apiToken: abc
254254
255255
Note: the config is in read-only mode, meaning at least one key was temporarily overridden from an env var or command flag."
256256
`)
@@ -262,12 +262,8 @@ describe('socket config get', async () => {
262262
|_____|___|___|_,_|___|_|.dev | Command: \`socket config get\`, cwd: <redacted>"
263263
`)
264264

265-
// Config flag takes precedence over deprecated env var
266-
expect(
267-
stdout.includes(
268-
'apiToken: ignoremebecausetheenvvarshouldbemoreimportant',
269-
),
270-
).toBe(true)
265+
// Env var fallback now takes precedence
266+
expect(stdout.includes('apiToken: abc')).toBe(true)
271267
},
272268
)
273269

packages/cli/src/commands/fix/ghsa-tracker.test.mts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ vi.mock('node:fs', async () => {
2929

3030
vi.mock('@socketsecurity/lib/fs', () => ({
3131
readJson: vi.fn(),
32+
safeMkdir: vi.fn(),
3233
writeJson: vi.fn(),
3334
}))
3435

@@ -90,7 +91,7 @@ describe('ghsa-tracker', () => {
9091

9192
describe('saveGhsaTracker', () => {
9293
it('saves tracker to file', async () => {
93-
const { writeJson } = await import('@socketsecurity/lib/fs')
94+
const { safeMkdir, writeJson } = await import('@socketsecurity/lib/fs')
9495
const tracker: GhsaTracker = {
9596
version: 1,
9697
fixed: [
@@ -105,7 +106,7 @@ describe('ghsa-tracker', () => {
105106

106107
await saveGhsaTracker(mockCwd, tracker)
107108

108-
expect(fs.mkdir).toHaveBeenCalledWith(path.dirname(trackerPath), {
109+
expect(safeMkdir).toHaveBeenCalledWith(path.dirname(trackerPath), {
109110
recursive: true,
110111
})
111112
expect(writeJson).toHaveBeenCalledWith(trackerPath, tracker, {

packages/cli/src/commands/scan/fetch-create-org-full-scan.test.mts

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,15 @@ describe('fetchCreateOrgFullScan', () => {
4949
['/path/to/package.json'],
5050
{
5151
pathsRelativeTo: process.cwd(),
52-
queryParams: {
53-
branch: 'main',
54-
commit_hash: 'abc123',
55-
commit_message: 'Initial commit',
56-
committers: '[email protected]',
57-
make_default_branch: 'undefined',
58-
pull_request: '42',
59-
repo: 'test-repo',
60-
set_as_pending_head: 'undefined',
61-
tmp: 'undefined',
62-
},
52+
branch: 'main',
53+
commit_hash: 'abc123',
54+
commit_message: 'Initial commit',
55+
committers: '[email protected]',
56+
make_default_branch: 'undefined',
57+
pull_request: '42',
58+
repo: 'test-repo',
59+
set_as_pending_head: 'undefined',
60+
tmp: 'undefined',
6361
},
6462
)
6563
expect(mockHandleApi).toHaveBeenCalledWith(expect.any(Promise), {
@@ -169,17 +167,15 @@ describe('fetchCreateOrgFullScan', () => {
169167
['/path/to/package.json'],
170168
{
171169
pathsRelativeTo: '/custom/path',
172-
queryParams: {
173-
branch: 'develop',
174-
commit_hash: 'xyz789',
175-
commit_message: 'Feature commit',
176-
committers: '[email protected]',
177-
make_default_branch: 'true',
178-
pull_request: '123',
179-
repo: 'feature-repo',
180-
set_as_pending_head: 'false',
181-
tmp: 'true',
182-
},
170+
branch: 'develop',
171+
commit_hash: 'xyz789',
172+
commit_message: 'Feature commit',
173+
committers: '[email protected]',
174+
make_default_branch: 'true',
175+
pull_request: '123',
176+
repo: 'feature-repo',
177+
set_as_pending_head: 'false',
178+
tmp: 'true',
183179
},
184180
)
185181
})
@@ -207,12 +203,10 @@ describe('fetchCreateOrgFullScan', () => {
207203
['/path/to/package.json'],
208204
{
209205
pathsRelativeTo: process.cwd(),
210-
queryParams: {
211-
make_default_branch: 'undefined',
212-
repo: 'test-repo',
213-
set_as_pending_head: 'undefined',
214-
tmp: 'undefined',
215-
},
206+
make_default_branch: 'undefined',
207+
repo: 'test-repo',
208+
set_as_pending_head: 'undefined',
209+
tmp: 'undefined',
216210
},
217211
)
218212
})
@@ -246,7 +240,6 @@ describe('fetchCreateOrgFullScan', () => {
246240
packagePaths,
247241
expect.objectContaining({
248242
pathsRelativeTo: process.cwd(),
249-
queryParams: expect.any(Object),
250243
}),
251244
)
252245
})
@@ -305,9 +298,7 @@ describe('fetchCreateOrgFullScan', () => {
305298
['/path/to/package.json'],
306299
expect.objectContaining({
307300
pathsRelativeTo: process.cwd(),
308-
queryParams: expect.objectContaining({
309-
repo,
310-
}),
301+
repo,
311302
}),
312303
)
313304
}

packages/cli/src/utils/git/providers.test.mts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@ vi.mock('./operations.mts', () => ({
2424
}))
2525

2626
vi.mock('@gitbeaker/rest', () => ({
27-
Gitlab: vi.fn(() => ({
28-
MergeRequests: {
29-
create: vi.fn(),
30-
show: vi.fn(),
31-
rebase: vi.fn(),
32-
all: vi.fn(),
33-
},
34-
MergeRequestNotes: {
35-
create: vi.fn(),
36-
},
37-
})),
27+
Gitlab: vi.fn(function () {
28+
return {
29+
MergeRequests: {
30+
create: vi.fn(),
31+
show: vi.fn(),
32+
rebase: vi.fn(),
33+
all: vi.fn(),
34+
},
35+
MergeRequestNotes: {
36+
create: vi.fn(),
37+
},
38+
}
39+
}),
3840
}))
3941

4042
describe('provider-factory', () => {

packages/cli/src/utils/npm/config.test.mts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ const mockNpmConfigInstance = {
1212

1313
// Mock @npmcli/config.
1414
vi.mock('@npmcli/config', () => ({
15-
default: vi.fn(() => mockNpmConfigInstance),
15+
default: vi.fn(function () {
16+
return mockNpmConfigInstance
17+
}),
1618
}))
1719

1820
import { getNpmConfig } from './config.mts'
@@ -37,7 +39,9 @@ describe('npm-config utilities', () => {
3739
beforeEach(() => {
3840
// Clear mock calls and restore original implementation
3941
MockNpmConfig.mockClear()
40-
MockNpmConfig.mockImplementation(() => mockNpmConfigInstance)
42+
MockNpmConfig.mockImplementation(function () {
43+
return mockNpmConfigInstance
44+
})
4145
vi.mocked(mockNpmConfigInstance.load).mockClear()
4246
})
4347

@@ -138,7 +142,9 @@ describe('npm-config utilities', () => {
138142
flat: { test: 'value' },
139143
}
140144
vi.mocked((await import('@npmcli/config')).default).mockImplementation(
141-
() => mockConfigInstance,
145+
function () {
146+
return mockConfigInstance
147+
},
142148
)
143149

144150
await getNpmConfig()

0 commit comments

Comments
 (0)