Repository: kibertoad/toad-scheduler
Pull request #182 (2023-06-06 19:42:28)

Update dependencies


Patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index a9f9f8c..a244d81 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -14,7 +14,7 @@ jobs:
 
     strategy:
       matrix:
-        node-version: [12.x, 14.x, 16.x, 18.x]
+        node-version: [14.x, 16.x, 18.x, 20.x]
 
     steps:
       - uses: actions/checkout@v3
diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml
index 236542e..37a1bdf 100644
--- a/.github/workflows/coverage.yml
+++ b/.github/workflows/coverage.yml
@@ -14,7 +14,7 @@ jobs:
 
     strategy:
       matrix:
-        node-version: [16.x]
+        node-version: [20.x]
 
     steps:
       - name: Checkout Repository
diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml
index 44706b3..df1f72a 100644
--- a/.github/workflows/linting.yml
+++ b/.github/workflows/linting.yml
@@ -17,7 +17,7 @@ jobs:
 
     strategy:
       matrix:
-        node-version: [16.x]
+        node-version: [20.x]
 
     steps:
       - name: Checkout Repository
diff --git a/README.md b/README.md
index 9f1623d..815e4d6 100644
--- a/README.md
+++ b/README.md
@@ -160,7 +160,7 @@ to prevent parallel execution of jobs in clustered environment, it is highly rec
 Here is an example:
 
 ```ts
-import { randomUUID } from 'crypto'
+import { randomUUID } from 'node:crypto'
 
 import type { Redis } from 'ioredis'
 import type { LockOptions } from 'redis-semaphore'
@@ -303,6 +303,8 @@ function createTask(job: AbstractBackgroundJob): AsyncTask {
 * `stop(): void` - stops all jobs, registered in the scheduler;
 * `getById(id: string): Job` - returns the job with a given id.
 * `existsById(id: string): boolean` - returns true if job with given id exists, false otherwise.
+* `getAllJobs(): Job[]` - returns all registered jobs.
+* `getAllJobsByStatus(status: JobStatus): Job[]` - returns all registered jobs with a given status.
 * `stopById(id: string): void` - stops the job with a given id.
 * `removeById(id: string): Job | undefined` - stops the job with a given id and removes it from the scheduler. If no such job exists, returns `undefined`, otherwise returns the job.
 * `startById(id: string): void` - starts, or restarts (if it's already running) the job with a given id.
diff --git a/jest.config.json b/jest.config.json
index 338c754..b47aeb4 100644
--- a/jest.config.json
+++ b/jest.config.json
@@ -2,16 +2,21 @@
   "moduleFileExtensions": ["ts", "js", "json"],
   "testMatch": ["<rootDir>/test/**/*.(spec|test).ts|tsx"],
   "transform": {
-    "^.+\\.ts$": "ts-jest"
+    "^.+\\.ts$": [
+      "ts-jest",
+      {
+        "diagnostics": false
+      }
+    ]
   },
   "testEnvironment": "node",
   "reporters": ["default"],
-  "coveragePathIgnorePatterns": ["<rootDir>/node_modules/", "<rootDir>/coverage/", "<rootDir>/test/utils/", "<rootDir>/index.ts"],
-  "globals": {
-    "ts-jest": {
-      "diagnostics": false
-    }
-  },
+  "coveragePathIgnorePatterns": [
+    "<rootDir>/node_modules/",
+    "<rootDir>/coverage/",
+    "<rootDir>/test/utils/",
+    "<rootDir>/index.ts"
+  ],
   "coverageThreshold": {
     "global": {
       "statements": 100,
diff --git a/lib/toadScheduler.ts b/lib/toadScheduler.ts
index 654ff49..39cef3c 100644
--- a/lib/toadScheduler.ts
+++ b/lib/toadScheduler.ts
@@ -1,6 +1,6 @@
 import { SimpleIntervalEngine } from './engines/simple-interval/SimpleIntervalEngine'
 import { SimpleIntervalJob } from './engines/simple-interval/SimpleIntervalJob'
-import { Job } from './common/Job'
+import { Job, JobStatus } from './common/Job'
 import { LongIntervalJob } from './engines/simple-interval/LongIntervalJob'
 import { CronJob } from './engines/cron/CronJob'
 import { CronJobEngine } from './engines/cron/CronJobEngine'
@@ -96,4 +96,14 @@ export class ToadScheduler {
     const job = this.getById(id)
     job.start()
   }
+
+  getAllJobs(): Job[] {
+    return Object.values(this.jobRegistry)
+  }
+
+  getAllJobsByStatus(status: JobStatus): Job[] {
+    return Object.values(this.jobRegistry).filter((value) => {
+      return value.getStatus() === status
+    })
+  }
 }
diff --git a/package.json b/package.json
index 4540d7b..7409a4f 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "toad-scheduler",
-  "version": "2.2.0",
+  "version": "2.3.0",
   "license": "MIT",
   "description": "In-memory Node.js and browser job scheduler",
   "maintainers": [
@@ -21,23 +21,23 @@
     "prepublishOnly": "npm run build"
   },
   "devDependencies": {
-    "@types/jest": "^28.1.8",
-    "@types/node": "^18.11.17",
-    "@typescript-eslint/eslint-plugin": "^5.47.0",
-    "@typescript-eslint/parser": "^5.47.0",
+    "@types/jest": "^29.5.2",
+    "@types/node": "^18.16.16",
+    "@typescript-eslint/eslint-plugin": "^5.59.9",
+    "@typescript-eslint/parser": "^5.59.9",
     "croner": "^5.3.5",
-    "eslint": "^8.30.0",
+    "eslint": "^8.42.0",
     "eslint-config-prettier": "^8.5.0",
     "eslint-plugin-prettier": "^4.2.1",
-    "jasmine-core": "^4.5.0",
-    "jest": "^28.1.3",
-    "karma": "^6.4.1",
-    "karma-chrome-launcher": "^3.1.1",
+    "jasmine-core": "^4.6.0",
+    "jest": "^29.5.0",
+    "karma": "^6.4.2",
+    "karma-chrome-launcher": "^3.2.0",
     "karma-jasmine": "^5.1.0",
-    "karma-typescript": "^5.5.3",
+    "karma-typescript": "^5.5.4",
     "prettier": "^2.8.1",
-    "ts-jest": "^28.0.8",
-    "typescript": "4.9.4"
+    "ts-jest": "^29.1.0",
+    "typescript": "4.9.5"
   },
   "homepage": "https://github.com/kibertoad/toad-scheduler",
   "repository": {


Test patch:
diff --git a/test/toadScheduler.spec.ts b/test/toadScheduler.spec.ts
index 9d2ffed..74f2e7c 100644
--- a/test/toadScheduler.spec.ts
+++ b/test/toadScheduler.spec.ts
@@ -3,6 +3,8 @@ import { SimpleIntervalJob } from '../lib/engines/simple-interval/SimpleInterval
 import { Task } from '../lib/common/Task'
 import { NoopTask } from './utils/testTasks'
 import { advanceTimersByTime, mockTimers, unMockTimers } from './utils/timerUtils'
+import { JobStatus } from '../lib/common/Job'
+import { expectToMatchObject } from './utils/assertUtils'
 
 describe('ToadScheduler', () => {
   beforeEach(() => {
@@ -219,6 +221,71 @@ describe('ToadScheduler', () => {
     })
   })
 
+  describe('listJobs', () => {
+    it('returns all jobs', () => {
+      const scheduler = new ToadScheduler()
+      const task = new NoopTask()
+      const job = new SimpleIntervalJob(
+        {
+          seconds: 20,
+        },
+        task,
+        { id: 'id' }
+      )
+      const job2 = new SimpleIntervalJob(
+        {
+          seconds: 20,
+        },
+        task,
+        { id: 'id2' }
+      )
+      scheduler.addSimpleIntervalJob(job)
+      scheduler.addSimpleIntervalJob(job2)
+      job.stop()
+
+      const retrievedJobs = scheduler.getAllJobs()
+      expectToMatchObject(retrievedJobs, [
+        {
+          id: 'id',
+        },
+        {
+          id: 'id2',
+        },
+      ])
+    })
+  })
+
+  describe('listJobs', () => {
+    it('returns jobs filtered by status', () => {
+      const scheduler = new ToadScheduler()
+      const task = new NoopTask()
+      const job = new SimpleIntervalJob(
+        {
+          seconds: 20,
+        },
+        task,
+        { id: 'id' }
+      )
+      const job2 = new SimpleIntervalJob(
+        {
+          seconds: 20,
+        },
+        task,
+        { id: 'id2' }
+      )
+      scheduler.addSimpleIntervalJob(job)
+      scheduler.addSimpleIntervalJob(job2)
+      job.stop()
+
+      const retrievedJobs = scheduler.getAllJobsByStatus(JobStatus.STOPPED)
+      expectToMatchObject(retrievedJobs, [
+        {
+          id: 'id',
+        },
+      ])
+    })
+  })
+
   describe('common', () => {
     it('throws an error when duplicate id is registered', () => {
       const scheduler = new ToadScheduler()
diff --git a/test/utils/assertUtils.ts b/test/utils/assertUtils.ts
index ae90470..1757680 100644
--- a/test/utils/assertUtils.ts
+++ b/test/utils/assertUtils.ts
@@ -5,3 +5,14 @@ export function expectAssertions(count: number) {
     expect.assertions(count)
   }
 }
+
+export function expectToMatchObject(actual: unknown[], expected: any[]) {
+  if (isJest) {
+    expect(actual).toMatchObject(expected)
+  } else {
+    for (const element of expected) {
+      expect(actual.length).toBe(expected.length)
+      expect(actual).toContain(jasmine.objectContaining(element))
+    }
+  }
+}