Repository: kelektiv/node-cron (ts)

Issue:
`waitForCompletion` not working as expected
### Description

In my testing I can't get `waitForCompletion` to stop another instance of a job running concurrently as expected based on changes made and documented in 3.3.0.

### Expected Behavior

`waitForCompletion` should stop multiple instances running concurrently, as documented.

### Actual Behavior

Setting `waitForCompletion` true or false doesn't seem to make any difference.

### Possible Fix

_No response_

### Steps to Reproduce

``` js
import { CronJob } from "cron";
let c = 0;
CronJob.from({
  cronTime: "*/1 * * * * *",
  onTick: async () => {
    console.log(++c);
    await new Promise((r) => setTimeout(r, 1100));
    c--;
  },
  waitForCompletion: true,
  start: true,
});
```

# Output
```
1
2
2
...
```

# Expect
```
1
1
1
...
```

### Context

I had previously implemented my own solution to stop multiple instances of a job running concurrently. I thought with 3.3.0 I could simplify my code and instead use the new feature provided by `cron`.

### Your Environment

- **`cron` version**: 3.3.0
- **NodeJS version**: 22.12.0
- **Operating System and version**: Windows 11 Pro

Pull request:
fix: correct waitForCompletion behavior
<!--- Provide a general summary of your changes in the Title above (following the Conventional Commits standard) -->
<!-- More infos: https://www.conventionalcommits.org -->
<!-- Commit types: https://github.com/insurgent-lab/conventional-changelog-preset#commit-types-->

## Description

<!--- Describe your changes in detail -->
fixes #923

## Related Issue

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps to reproduce -->
<!--- Please link to the issue here: -->
#923

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->
it makes the new feature actually work

## How Has This Been Tested?

<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->
modified test case added by #894

## Screenshots (if appropriate):

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [x] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.
- [ ] If my change introduces a breaking change, I have added a `!` after the type/scope in the title (see the Conventional Commits standard).


Patch:
diff --git a/src/job.ts b/src/job.ts
index 59555ca..f051d1a 100644
--- a/src/job.ts
+++ b/src/job.ts
@@ -207,7 +207,8 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
 	}
 
 	async fireOnTick() {
-		if (!this.waitForCompletion && this._isCallbackRunning) return;
+		// skip job if previous callback is still running
+		if (this.waitForCompletion && this._isCallbackRunning) return;
 
 		this._isCallbackRunning = true;
 


Test patch:
diff --git a/tests/cron.test.ts b/tests/cron.test.ts
index c11e8f6..6202521 100644
--- a/tests/cron.test.ts
+++ b/tests/cron.test.ts
@@ -1231,10 +1231,12 @@ describe('cron', () => {
 		it('should track multiple running jobs correctly', async () => {
 			const clock = sinon.useFakeTimers();
 			const executionOrder: number[] = [];
+			let started = 0;
 
 			const job = new CronJob(
 				'* * * * * *',
 				async () => {
+					started++;
 					await new Promise<void>(resolve => {
 						setTimeout(() => {
 							callback();
@@ -1256,7 +1258,8 @@ describe('cron', () => {
 
 			await clock.tickAsync(3500);
 
-			expect(executionOrder).toEqual([1, 2]);
+			expect(started).toBe(2);
+			expect(executionOrder).toEqual([1]);
 			job.stop();
 		});