Skip to content

Commit

Permalink
chore(deps): update dependency sinon to v19 (#5028)
Browse files Browse the repository at this point in the history
* chore(deps): update dependency sinon to v19

* Fix sinon test based on breaking changes in fake timers.

* update fake timers in jasmine

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Nico Jansen <jansennico@gmail.com>
  • Loading branch information
renovate[bot] and nicojs authored Sep 27, 2024
1 parent 88caf82 commit 099a031
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 52 deletions.
91 changes: 53 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"mocha": "10.7.3",
"prettier": "3.3.3",
"rimraf": "5.0.10",
"sinon": "18.0.1",
"sinon": "19.0.2",
"sinon-chai": "4.0.0",
"source-map-support": "0.5.21",
"ts-node": "10.9.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ describe(ChildProcessProxy.name, () => {
let childProcessMock: ChildProcessMock;
let killStub: sinon.SinonStub;
let logMock: Mock<Logger>;
let clock: sinon.SinonFakeTimers;
const workerId = 5;

beforeEach(() => {
clock = sinon.useFakeTimers();
childProcessMock = new ChildProcessMock();
forkStub = sinon.stub(childProcess, 'fork');
killStub = sinon.stub(objectUtils, 'kill');
Expand Down Expand Up @@ -263,6 +261,7 @@ describe(ChildProcessProxy.name, () => {

it('should proxy the message', async () => {
// Arrange
const clock = sinon.useFakeTimers();
receiveMessage({ kind: ParentMessageKind.Initialized });
const workerResponse: ParentMessage = {
correlationId: 0,
Expand All @@ -289,6 +288,7 @@ describe(ChildProcessProxy.name, () => {

it('should use a unique correlation id for each call', async () => {
// Arrange
const clock = sinon.useFakeTimers();
receiveMessage({ kind: ParentMessageKind.Initialized });

// Act
Expand Down Expand Up @@ -317,6 +317,7 @@ describe(ChildProcessProxy.name, () => {

it('should resolve correct promises when receiving responses', async () => {
// Arrange
const clock = sinon.useFakeTimers();
receiveMessage({ kind: ParentMessageKind.Initialized });
const delayedEcho = sut.proxy.say('echo');
const delayedHello = sut.proxy.sayHello();
Expand All @@ -334,6 +335,7 @@ describe(ChildProcessProxy.name, () => {

it('should resolve correct promises when receiving responses out-of-order', async () => {
// Arrange
const clock = sinon.useFakeTimers();
receiveMessage({ kind: ParentMessageKind.Initialized });
const delayedEcho = sut.proxy.say('echo');
const delayedHello = sut.proxy.sayHello();
Expand Down Expand Up @@ -368,8 +370,10 @@ describe(ChildProcessProxy.name, () => {
});

describe('dispose', () => {
let clock: sinon.SinonFakeTimers;
beforeEach(() => {
sut = createSut();
clock = sinon.useFakeTimers();
receiveMessage({ kind: ParentMessageKind.Ready });
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sinon from 'sinon';
import { expect } from 'chai';
import { testInjector, factory, tick } from '@stryker-mutator/test-helpers';
import { testInjector, factory, tick, createFakeTick } from '@stryker-mutator/test-helpers';
import { Reporter } from '@stryker-mutator/api/report';
import { TestRunner, MutantRunOptions, MutantRunResult, MutantRunStatus, CompleteDryRunResult, TestResult } from '@stryker-mutator/api/test-runner';
import { CheckResult, CheckStatus } from '@stryker-mutator/api/check';
Expand Down Expand Up @@ -187,6 +187,7 @@ describe(MutationTestExecutor.name, () => {
it('should group mutants buffered by time', async () => {
// Arrange
const clock = sinon.useFakeTimers();
const fakeTick = createFakeTick(clock);
const plan = mutantRunPlan({ id: '1' });
const plan2 = mutantRunPlan({ id: '2' });
arrangeMutationTestReportHelper();
Expand Down Expand Up @@ -217,7 +218,8 @@ describe(MutationTestExecutor.name, () => {
const onGoingAct = sut.execute();

// Assert
await tick();
await fakeTick();

// Assert that checker is called for the first 2 groups
expect(checker.group).calledOnce;
expect(checker.check).calledTwice;
Expand All @@ -226,7 +228,7 @@ describe(MutationTestExecutor.name, () => {

// Assert first check resolved, now tick the clock 10s in the future
clock.tick(10_001);
await tick();
await fakeTick();
// Now the second grouping should have happened
expect(checker.group).calledTwice;
expect(checker.check).calledThrice;
Expand Down
8 changes: 5 additions & 3 deletions packages/jasmine-runner/test/unit/jasmine-test-runner.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sinon from 'sinon';
import { expect } from 'chai';
import { factory, assertions, testInjector, tick } from '@stryker-mutator/test-helpers';
import { factory, assertions, testInjector, createFakeTick } from '@stryker-mutator/test-helpers';
import { TestStatus, CompleteDryRunResult, DryRunStatus, TestRunnerCapabilities } from '@stryker-mutator/api/test-runner';
import jasmine from 'jasmine';
import { MutantCoverage } from '@stryker-mutator/api/core';
Expand All @@ -18,13 +18,15 @@ describe(JasmineTestRunner.name, () => {
let jasmineEnvStub: sinon.SinonStubbedInstance<jasmine.Env>;
let sut: JasmineTestRunner;
let clock: sinon.SinonFakeTimers;
let fakeTick: ReturnType<typeof createFakeTick>;

beforeEach(() => {
jasmineStub = sinon.createStubInstance(jasmine);
jasmineEnvStub = createEnvStub();
jasmineStub.env = jasmineEnvStub as unknown as jasmine.Env;
sinon.stub(helpers, 'createJasmine').returns(jasmineStub);
clock = sinon.useFakeTimers();
fakeTick = createFakeTick(clock);
jasmineEnvStub.addReporter.callsFake((rep: jasmine.CustomReporter) => (reporter = rep));
testInjector.options.jasmineConfigFile = 'jasmineConfFile';
sut = testInjector.injector.provideValue(pluginTokens.globalNamespace, '__stryker2__' as const).injectClass(JasmineTestRunner);
Expand Down Expand Up @@ -95,7 +97,7 @@ describe(JasmineTestRunner.name, () => {

// Act
const onGoingAct = sut.mutantRun(factory.mutantRunOptions({ activeMutant: factory.mutant({ id: '23' }), mutantActivation: 'static' }));
await tick();
await fakeTick();

// Assert
expect(global.__stryker2__?.activeMutant).eq('23');
Expand All @@ -119,7 +121,7 @@ describe(JasmineTestRunner.name, () => {

// Act
const onGoingAct = sut.mutantRun(factory.mutantRunOptions({ activeMutant: factory.mutant({ id: '23' }), mutantActivation: 'runtime' }));
await tick();
await fakeTick();

// Assert
expect(global.__stryker2__?.activeMutant).undefined;
Expand Down
22 changes: 17 additions & 5 deletions packages/test-helpers/src/tick.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { promisify } from 'util';
import type Sinon from 'sinon';

/**
* Wait `n` ticks. This allows async tasks to progress.
* @param n The number of ticks to wait
*/
export async function tick(n = 1): Promise<void> {
const nextTickAsPromised = promisify(process.nextTick);

for await (const _ of Array.from({ length: n })) {
await nextTickAsPromised();
for (let i = 0; i < n; i++) {
await nextTick();
}
}

export function createFakeTick(clock: Sinon.SinonFakeTimers) {
return async function fakeTick(n = 1) {
const onGoingTick = tick(n);
clock.tick(n);
await onGoingTick;
};
}

function nextTick() {
return new Promise<void>((resolve) => {
process.nextTick(() => resolve());
});
}

0 comments on commit 099a031

Please sign in to comment.