Repository: kibertoad/toad-scheduler
Pull request #92 (2022-05-03 02:50:13)

Fix issue #77 Typescript compiler error 'TS1362: JobStatus can not be used as a value'
Hi, I'm using `toad-scheduler` in my personal project, I saw the issue #77 when I was learning about this repository, I am interested in this problem and have a try to fix it.

Maybe there is a better way to avoid this compilation error via custom `type` instead of `enum`, like this `export type JobStatus = 'running' | 'stopped'`, and when using it, as follows:
```
if (job.getStatus() === 'running') {
  console.log('Job is running!');
}
```
Users don't need to `import JobStatus` explicitly, and the IDE (I'm using Microsoft VS Code) can also give the code auto-complete in a right way. If you used other values carelessly (neither `running` nor `stopped`) in your code, IDE will also give an error tips to remind you to correct it.


Patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 086ea95..987074f 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -14,13 +14,13 @@ jobs:
 
     strategy:
       matrix:
-        node-version: [10.x, 12.x, 14.x, 16.x, 17.x]
+        node-version: [10.x, 12.x, 14.x, 16.x, 18.x]
 
     steps:
       - uses: actions/checkout@v3
 
       - name: Use Node.js
-        uses: actions/setup-node@v3.2.0
+        uses: actions/setup-node@v3
         with:
           node-version: ${{ matrix.node-version }}
 
@@ -38,6 +38,6 @@ jobs:
     needs: build
     runs-on: ubuntu-latest
     steps:
-      - uses: fastify/github-action-merge-dependabot@v2.7.1
+      - uses: fastify/github-action-merge-dependabot@v2
         with:
           github-token: ${{ secrets.GITHUB_TOKEN }}
diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml
index 6906033..236542e 100644
--- a/.github/workflows/coverage.yml
+++ b/.github/workflows/coverage.yml
@@ -23,7 +23,7 @@ jobs:
           fetch-depth: 1
 
       - name: Setup Node ${{ matrix.node-version }}
-        uses: actions/setup-node@v3.2.0
+        uses: actions/setup-node@v3
         with:
           always-auth: false
           node-version: ${{ matrix.node-version }}
diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml
index fee5ca9..44706b3 100644
--- a/.github/workflows/linting.yml
+++ b/.github/workflows/linting.yml
@@ -26,7 +26,7 @@ jobs:
           fetch-depth: 1
 
       - name: Setup Node ${{ matrix.node-version }}
-        uses: actions/setup-node@v3.2.0
+        uses: actions/setup-node@v3
         with:
           always-auth: false
           node-version: ${{ matrix.node-version }}
diff --git a/index.ts b/index.ts
index c6f51d6..a36c5bf 100644
--- a/index.ts
+++ b/index.ts
@@ -2,7 +2,7 @@ export { ToadScheduler } from './lib/toadScheduler'
 export { AsyncTask } from './lib/common/AsyncTask'
 export { Task } from './lib/common/Task'
 export { Job } from './lib/common/Job'
-export type { JobStatus } from './lib/common/Job'
+export { JobStatus } from './lib/common/Job'
 export { SimpleIntervalJob } from './lib/engines/simple-interval/SimpleIntervalJob'
 export { LongIntervalJob } from './lib/engines/simple-interval/LongIntervalJob'
 export type { SimpleIntervalSchedule } from './lib/engines/simple-interval/SimpleIntervalSchedule'
diff --git a/jest.config.json b/jest.config.json
index fd56dc3..338c754 100644
--- a/jest.config.json
+++ b/jest.config.json
@@ -6,7 +6,7 @@
   },
   "testEnvironment": "node",
   "reporters": ["default"],
-  "coveragePathIgnorePatterns": ["<rootDir>/node_modules/", "<rootDir>/coverage/", "<rootDir>/test/utils/"],
+  "coveragePathIgnorePatterns": ["<rootDir>/node_modules/", "<rootDir>/coverage/", "<rootDir>/test/utils/", "<rootDir>/index.ts"],
   "globals": {
     "ts-jest": {
       "diagnostics": false
diff --git a/lib/engines/simple-interval/LongIntervalJob.ts b/lib/engines/simple-interval/LongIntervalJob.ts
index a0c8510..7aa4a52 100644
--- a/lib/engines/simple-interval/LongIntervalJob.ts
+++ b/lib/engines/simple-interval/LongIntervalJob.ts
@@ -1,9 +1,9 @@
 import Timeout = NodeJS.Timeout
+import { AsyncTask } from '../../common/AsyncTask'
 import { Job, JobStatus } from '../../common/Job'
-import { SimpleIntervalSchedule, toMsecs } from './SimpleIntervalSchedule'
 import { Task } from '../../common/Task'
-import { AsyncTask } from '../../common/AsyncTask'
 import { SimpleIntervalJob } from './SimpleIntervalJob'
+import { SimpleIntervalSchedule, toMsecs } from './SimpleIntervalSchedule'
 
 const MAX_TIMEOUT_DURATION_MS = 2147483647
 
diff --git a/lib/engines/simple-interval/SimpleIntervalJob.ts b/lib/engines/simple-interval/SimpleIntervalJob.ts
index c42eab8..38b074f 100644
--- a/lib/engines/simple-interval/SimpleIntervalJob.ts
+++ b/lib/engines/simple-interval/SimpleIntervalJob.ts
@@ -1,8 +1,8 @@
 import Timeout = NodeJS.Timeout
+import { AsyncTask } from '../../common/AsyncTask'
 import { Job, JobStatus } from '../../common/Job'
-import { SimpleIntervalSchedule, toMsecs } from './SimpleIntervalSchedule'
 import { Task } from '../../common/Task'
-import { AsyncTask } from '../../common/AsyncTask'
+import { SimpleIntervalSchedule, toMsecs } from './SimpleIntervalSchedule'
 
 export class SimpleIntervalJob extends Job {
   private timer?: Timeout
diff --git a/package.json b/package.json
index 4f293ad..a12d292 100644
--- a/package.json
+++ b/package.json
@@ -21,21 +21,21 @@
     "prepublishOnly": "npm run build"
   },
   "devDependencies": {
-    "@types/jest": "^27.0.3",
-    "@types/node": "^17.0.0",
-    "@typescript-eslint/eslint-plugin": "^5.6.0",
-    "@typescript-eslint/parser": "^5.6.0",
-    "eslint": "^8.4.1",
-    "eslint-config-prettier": "^8.3.0",
+    "@types/jest": "^27.5.1",
+    "@types/node": "^17.0.33",
+    "@typescript-eslint/eslint-plugin": "^5.24.0",
+    "@typescript-eslint/parser": "^5.24.0",
+    "eslint": "^8.15.0",
+    "eslint-config-prettier": "^8.5.0",
     "eslint-plugin-prettier": "^4.0.0",
-    "jasmine-core": "^4.0.0",
-    "jest": "^27.3.1",
-    "karma": "^6.3.9",
-    "karma-chrome-launcher": "^3.1.0",
-    "karma-jasmine": "^5.0.0",
-    "karma-typescript": "^5.5.2",
-    "prettier": "^2.5.1",
-    "ts-jest": "^27.1.1",
+    "jasmine-core": "^4.1.1",
+    "jest": "^27.5.1",
+    "karma": "^6.3.20",
+    "karma-chrome-launcher": "^3.1.1",
+    "karma-jasmine": "^5.0.1",
+    "karma-typescript": "^5.5.3",
+    "prettier": "^2.6.2",
+    "ts-jest": "^27.1.4",
     "typescript": "4.6.4"
   },
   "homepage": "https://github.com/kibertoad/toad-scheduler",
diff --git a/tsconfig.json b/tsconfig.json
index afde6f8..debccb6 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -3,7 +3,7 @@
     "outDir": "dist",
     "module": "commonjs",
     "target": "es2017",
-    "sourceMap": false,
+    "sourceMap": true,
     "declaration": true,
     "declarationMap": false,
     "types": ["node", "jest"],


Test patch:
diff --git a/test/import.spec.ts b/test/import.spec.ts
new file mode 100644
index 0000000..e3ed9cf
--- /dev/null
+++ b/test/import.spec.ts
@@ -0,0 +1,30 @@
+import { isJest } from './utils/assertUtils'
+
+describe('import', () => {
+  it('JobStatus', async () => {
+    if (!isJest) {
+      return
+    }
+    const { JobStatus, SimpleIntervalJob, Task, ToadScheduler } = await import('../index')
+
+    const scheduler = new ToadScheduler()
+    let hasRun = false
+    let isRunning = false
+
+    const task = new Task('Sample task', () => {
+      hasRun = true
+    })
+
+    const job = new SimpleIntervalJob({ seconds: 5 }, task)
+
+    scheduler.addSimpleIntervalJob(job)
+
+    if (job.getStatus() === JobStatus.RUNNING) {
+      isRunning = true
+    }
+    expect(isRunning).toEqual(true)
+    expect(hasRun).toEqual(false)
+
+    scheduler.stop()
+  })
+})
diff --git a/test/utils/assertUtils.ts b/test/utils/assertUtils.ts
index fc23ffb..ae90470 100644
--- a/test/utils/assertUtils.ts
+++ b/test/utils/assertUtils.ts
@@ -1,4 +1,4 @@
-const isJest = process.env.JEST_WORKER_ID !== undefined
+export const isJest = process.env.JEST_WORKER_ID !== undefined
 
 export function expectAssertions(count: number) {
   if (isJest) {