Skip to content

Commit 75f7636

Browse files
outsleptmrstork
andauthored
test: replace sinon with tinyspy (#6580)
* fix: replace sinon with tinyspy * fix: apply suggestions from review * fix: formatting * fix: apply suggestions from the review --------- Co-authored-by: Mateusz Bocian <[email protected]>
1 parent a5e2746 commit 75f7636

File tree

7 files changed

+59
-154
lines changed

7 files changed

+59
-154
lines changed

package-lock.json

Lines changed: 1 addition & 102 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/build/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
"moize": "^6.0.0",
138138
"npm-run-all2": "^6.0.0",
139139
"process-exists": "^5.0.0",
140-
"sinon": "^21.0.0",
140+
"tinyspy": "^4.0.3",
141141
"tmp-promise": "^3.0.2",
142142
"tsd": "^0.32.0",
143143
"vitest": "^3.0.0",

packages/build/tests/core/tests.js

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import getNode from 'get-node'
99
import moize from 'moize'
1010
import { pathExists } from 'path-exists'
1111
import semver from 'semver'
12-
import sinon from 'sinon'
12+
import { spy, spyOn } from 'tinyspy'
1313
import { tmpName } from 'tmp-promise'
1414

1515
import { zipItAndShipIt } from '../../lib/plugins_core/functions/index.js'
@@ -425,8 +425,8 @@ test('Print warning when headers file is missing from publish directory', async
425425
})
426426

427427
test.serial('Passes the right properties to zip-it-and-ship-it', async (t) => {
428-
const mockZipFunctions = sinon.stub().resolves()
429-
const stub = sinon.stub(zipItAndShipIt, 'zipFunctions').get(() => mockZipFunctions)
428+
const mockZipFunctions = spy(() => Promise.resolve())
429+
const stub = spyOn(zipItAndShipIt, 'zipFunctions', mockZipFunctions)
430430
const fixtureDir = join(FIXTURES_DIR, 'core')
431431

432432
await new Fixture('./fixtures/core').runWithBuild()
@@ -439,7 +439,7 @@ test.serial('Passes the right properties to zip-it-and-ship-it', async (t) => {
439439

440440
t.is(mockZipFunctions.callCount, 2)
441441

442-
const params1 = mockZipFunctions.firstCall.args[2]
442+
const params1 = mockZipFunctions.calls[0][2]
443443

444444
t.is(params1.basePath, fixtureDir)
445445
t.true(params1.config['*'].zipGo)
@@ -453,15 +453,15 @@ test.serial('Passes the right properties to zip-it-and-ship-it', async (t) => {
453453
t.is(params1.config['*'].nodeVersion, undefined)
454454
}
455455

456-
const params2 = mockZipFunctions.secondCall.args[2]
456+
const params2 = mockZipFunctions.calls[1][2]
457457

458458
t.is(params2.config['*'].nodeVersion, 'nodejs00.x')
459459
t.is(params2.config['*'].zipGo, undefined)
460460
})
461461

462462
test.serial('Passes functions generated by build plugins to zip-it-and-ship-it', async (t) => {
463-
const mockZipFunctions = sinon.stub().resolves([])
464-
const stub = sinon.stub(zipItAndShipIt, 'zipFunctions').get(() => mockZipFunctions)
463+
const mockZipFunctions = spy(() => Promise.resolve([]))
464+
const stub = spyOn(zipItAndShipIt, 'zipFunctions', mockZipFunctions)
465465
const fixtureName = 'functions_generated_from_steps'
466466
const fixtureDir = join(FIXTURES_DIR, fixtureName)
467467

@@ -474,7 +474,7 @@ test.serial('Passes functions generated by build plugins to zip-it-and-ship-it',
474474
t.true(success)
475475
t.is(mockZipFunctions.callCount, 1)
476476

477-
const { generated, user } = mockZipFunctions.firstCall.args[0]
477+
const { generated, user } = mockZipFunctions.calls[0][0]
478478

479479
t.is(generated.directories.length, 2)
480480
t.true(generated.directories.includes(resolve(fixtureDir, '.netlify/functions-internal')))
@@ -501,8 +501,8 @@ test.serial('Passes functions generated by build plugins to zip-it-and-ship-it',
501501
})
502502

503503
test.serial('Passes the right feature flags to zip-it-and-ship-it', async (t) => {
504-
const mockZipFunctions = sinon.stub().resolves()
505-
const stub = sinon.stub(zipItAndShipIt, 'zipFunctions').get(() => mockZipFunctions)
504+
const mockZipFunctions = spy(() => Promise.resolve())
505+
const stub = spyOn(zipItAndShipIt, 'zipFunctions', mockZipFunctions)
506506

507507
await new Fixture('./fixtures/schedule').runWithBuild()
508508
await new Fixture('./fixtures/schedule').withFlags({ featureFlags: { buildbot_zisi_trace_nft: true } }).runWithBuild()
@@ -517,16 +517,16 @@ test.serial('Passes the right feature flags to zip-it-and-ship-it', async (t) =>
517517

518518
t.is(mockZipFunctions.callCount, 4)
519519

520-
t.false(mockZipFunctions.getCall(0).args[2].featureFlags.traceWithNft)
521-
t.false(mockZipFunctions.getCall(0).args[2].featureFlags.parseWithEsbuild)
522-
t.is(mockZipFunctions.getCall(0).args[2].config.test.schedule, '@daily')
523-
t.is(mockZipFunctions.getCall(0).args[2].featureFlags.this_is_a_mock_flag, undefined)
524-
t.is(mockZipFunctions.getCall(0).args[2].featureFlags.and_another_one, undefined)
520+
t.false(mockZipFunctions.calls[0][2].featureFlags.traceWithNft)
521+
t.false(mockZipFunctions.calls[0][2].featureFlags.parseWithEsbuild)
522+
t.is(mockZipFunctions.calls[0][2].config.test.schedule, '@daily')
523+
t.is(mockZipFunctions.calls[0][2].featureFlags.this_is_a_mock_flag, undefined)
524+
t.is(mockZipFunctions.calls[0][2].featureFlags.and_another_one, undefined)
525525

526-
t.true(mockZipFunctions.getCall(1).args[2].featureFlags.traceWithNft)
527-
t.true(mockZipFunctions.getCall(2).args[2].featureFlags.parseWithEsbuild)
528-
t.true(mockZipFunctions.getCall(3).args[2].featureFlags.this_is_a_mock_flag)
529-
t.true(mockZipFunctions.getCall(3).args[2].featureFlags.and_another_one)
526+
t.true(mockZipFunctions.calls[1][2].featureFlags.traceWithNft)
527+
t.true(mockZipFunctions.calls[2][2].featureFlags.parseWithEsbuild)
528+
t.true(mockZipFunctions.calls[3][2].featureFlags.this_is_a_mock_flag)
529+
t.true(mockZipFunctions.calls[3][2].featureFlags.and_another_one)
530530
})
531531

532532
test('Print warning on lingering processes', async (t) => {
@@ -597,8 +597,8 @@ test('Removes duplicate function names from the list of processed functions', as
597597

598598
test.serial('`rustTargetDirectory` is passed to zip-it-and-ship-it only when running in buildbot', async (t) => {
599599
const runCount = 4
600-
const mockZipFunctions = sinon.stub().resolves()
601-
const stub = sinon.stub(zipItAndShipIt, 'zipFunctions').get(() => mockZipFunctions)
600+
const mockZipFunctions = spy(() => Promise.resolve())
601+
const stub = spyOn(zipItAndShipIt, 'zipFunctions', mockZipFunctions)
602602

603603
await new Fixture('./fixtures/functions_config_1').withFlags({ mode: 'buildbot' }).runWithBuild()
604604
await new Fixture('./fixtures/functions_config_1').runWithBuild()
@@ -609,10 +609,7 @@ test.serial('`rustTargetDirectory` is passed to zip-it-and-ship-it only when run
609609

610610
t.is(mockZipFunctions.callCount, runCount)
611611

612-
const { args: call1Args } = mockZipFunctions.getCall(0)
613-
const { args: call2Args } = mockZipFunctions.getCall(1)
614-
const { args: call3Args } = mockZipFunctions.getCall(2)
615-
const { args: call4Args } = mockZipFunctions.getCall(3)
612+
const [call1Args, call2Args, call3Args, call4Args] = mockZipFunctions.calls
616613

617614
t.is(
618615
call1Args[2].config['*'].rustTargetDirectory,
@@ -628,23 +625,23 @@ test.serial('`rustTargetDirectory` is passed to zip-it-and-ship-it only when run
628625

629626
test.serial('configFileDirectories is passed to zip-it-and-ship-it', async (t) => {
630627
const runCount = 1
631-
const mockZipFunctions = sinon.stub().resolves()
632-
const stub = sinon.stub(zipItAndShipIt, 'zipFunctions').get(() => mockZipFunctions)
628+
const mockZipFunctions = spy(() => Promise.resolve())
629+
const stub = spyOn(zipItAndShipIt, 'zipFunctions', mockZipFunctions)
633630

634631
await new Fixture('./fixtures/functions_config_json').withFlags({ mode: 'buildbot' }).runWithBuild()
635632
stub.restore()
636633

637634
t.is(mockZipFunctions.callCount, runCount)
638635

639-
const { args: call1Args } = mockZipFunctions.getCall(0)
636+
const call1Args = mockZipFunctions.calls[0]
640637

641638
t.deepEqual(call1Args[2].configFileDirectories, [
642639
join(FIXTURES_DIR, 'functions_config_json/.netlify/functions-internal'),
643640
])
644641
})
645642

646643
test.serial('functions can have a config with different parameters passed to zip-it-and-ship-it', async (t) => {
647-
const zipItAndShipItSpy = sinon.spy(zipItAndShipIt, 'zipFunctions')
644+
const zipItAndShipItSpy = spyOn(zipItAndShipIt, 'zipFunctions')
648645
const output = await new Fixture('./fixtures/functions_config_json')
649646
.withFlags({
650647
mode: 'buildbot',
@@ -653,7 +650,7 @@ test.serial('functions can have a config with different parameters passed to zip
653650

654651
zipItAndShipItSpy.restore()
655652

656-
const { args: call1Args } = zipItAndShipItSpy.getCall(0)
653+
const call1Args = zipItAndShipItSpy.calls[0]
657654
const { functions: functions } = await importJsonFile(call1Args[2].manifest)
658655

659656
t.is(functions[0].displayName, 'Function One')
@@ -664,11 +661,11 @@ test.serial('functions can have a config with different parameters passed to zip
664661
})
665662

666663
test.serial('internalSrcFolder is passed to zip-it-and-ship-it and helps prefill the generator field', async (t) => {
667-
const zipItAndShipItSpy = sinon.spy(zipItAndShipIt, 'zipFunctions')
664+
const zipItAndShipItSpy = spyOn(zipItAndShipIt, 'zipFunctions')
668665

669666
await new Fixture('./fixtures/functions_internal_src_folder').withFlags({ mode: 'buildbot' }).runWithBuild()
670667
zipItAndShipItSpy.restore()
671-
const { args: call1Args } = zipItAndShipItSpy.getCall(0)
668+
const call1Args = zipItAndShipItSpy.calls[0]
672669

673670
const [paths, , options] = call1Args
674671

packages/build/tests/error_reporting/tests.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import dns from 'dns'
33
import { intercept, cleanAll } from '@netlify/nock-udp'
44
import { Fixture } from '@netlify/testing'
55
import test from 'ava'
6-
import sinon from 'sinon'
6+
import { spyOn } from 'tinyspy'
7+
8+
let dnsLookupSpy
79

810
test.before(() => {
911
const origLookup = dns.lookup
1012
// we have to stub dns lookup as hot-shots is caching dns and therefore calling dns.lookup directly
11-
sinon.stub(dns, 'lookup').callsFake((host, options, cb = options) => {
13+
dnsLookupSpy = spyOn(dns, 'lookup', (host, options, cb = options) => {
1214
if (options === cb) {
1315
options = {}
1416
}
@@ -21,7 +23,7 @@ test.before(() => {
2123
})
2224

2325
test.after(() => {
24-
dns.lookup.restore()
26+
dnsLookupSpy.restore()
2527
cleanAll()
2628
})
2729

packages/build/tests/monitor/tests.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { platform } from 'process'
33
import { Fixture, normalizeOutput } from '@netlify/testing'
44
import test from 'ava'
55
import hasAnsi from 'has-ansi'
6-
import sinon from 'sinon'
6+
import { spyOn } from 'tinyspy'
77

88
import { CUSTOM_ERROR_KEY } from '../../lib/error/info.js'
99
import { zipItAndShipIt } from '../../lib/plugins_core/functions/index.js'
@@ -205,7 +205,9 @@ test.serial('Normalizes error messages resulting from bundling TypeScript server
205205
type: 'functionsBundling',
206206
}
207207

208-
const stub = sinon.stub(zipItAndShipIt, 'zipFunctions').throws(customError)
208+
const stub = spyOn(zipItAndShipIt, 'zipFunctions', () => {
209+
throw customError
210+
})
209211

210212
const output = await new Fixture('./fixtures/serverless_function')
211213
.withFlags({ testOpts: { errorMonitor: true }, bugsnagKey: BUGSNAG_TEST_KEY })
@@ -239,7 +241,9 @@ error: expected one of \`!\` or \`::\`, found keyword \`use\`
239241
type: 'functionsBundling',
240242
}
241243

242-
const stub = sinon.stub(zipItAndShipIt, 'zipFunctions').throws(customError)
244+
const stub = spyOn(zipItAndShipIt, 'zipFunctions', () => {
245+
throw customError
246+
})
243247

244248
const output = await new Fixture('./fixtures/serverless_function')
245249
.withFlags({ testOpts: { errorMonitor: true }, bugsnagKey: BUGSNAG_TEST_KEY })
@@ -258,7 +262,9 @@ test.serial('When an error has a `normalizedMessage` property, its value is used
258262
type: 'functionsBundling',
259263
}
260264

261-
const stub = sinon.stub(zipItAndShipIt, 'zipFunctions').throws(customError)
265+
const stub = spyOn(zipItAndShipIt, 'zipFunctions', () => {
266+
throw customError
267+
})
262268

263269
const output = await new Fixture('./fixtures/serverless_function')
264270
.withFlags({ testOpts: { errorMonitor: true }, bugsnagKey: BUGSNAG_TEST_KEY })

0 commit comments

Comments
 (0)