From 70cb31476bbf4656613a7b892b9d80c651b98acf Mon Sep 17 00:00:00 2001 From: Charly Poly <1252066+charlypoly@users.noreply.github.com> Date: Wed, 6 Mar 2024 01:18:15 -0800 Subject: [PATCH] fix(build): remove need for `typesVersions` breaking VSCode imports (#130) * fix(build): remove need for `typesVersions` breaking VSCode imports * chore: changeset * revert * fix * remove bob * update `package-lock.json` * fix: `dist/` not necessary * fix * ESM fixes * typos * style * fixes * remove deprecated tests * fix test hanging up * fix tests --- .changeset/cyan-cycles-rhyme.md | 5 + .github/workflows/release.yml | 1 - .github/workflows/tests.yml | 2 - .gitignore | 1 - .prettierignore | 1 - package-lock.json | 269 ------------------ package.json | 47 ++- src/backend.ts | 2 +- src/backend/local.ts | 14 +- src/backend/remote.ts | 8 +- src/backend/remote/httpClient.ts | 6 +- src/index.ts | 12 +- src/logger.ts | 2 +- src/next/asNextRoute.ts | 8 +- src/next/index.ts | 4 +- src/next/useDeferRoute.ts | 4 +- tests/awaitResult.spec.ts | 80 ------ tests/backend/local.test.ts | 6 +- tests/backend/local/counter.test.ts | 2 +- tests/backend/local/kv.test.ts | 2 +- tests/backend/remote.test.ts | 6 +- tests/backend/remote/httpClient.test.ts | 2 +- tests/index.test.ts | 2 +- tests/jest.ts | 1 - tests/logger.test.ts | 2 +- ...sNextRoute.spec.ts => asNextRoute.test.ts} | 29 +- tests/next/useDeferRoute.spec.ts | 10 - tests/utils.test.ts | 2 +- tsconfig.build.json | 9 + tsconfig.json | 6 +- utils/fix-ts-sub-exports.mjs | 18 -- 31 files changed, 96 insertions(+), 467 deletions(-) create mode 100644 .changeset/cyan-cycles-rhyme.md delete mode 100644 tests/awaitResult.spec.ts rename tests/next/{asNextRoute.spec.ts => asNextRoute.test.ts} (91%) delete mode 100644 tests/next/useDeferRoute.spec.ts create mode 100644 tsconfig.build.json delete mode 100644 utils/fix-ts-sub-exports.mjs diff --git a/.changeset/cyan-cycles-rhyme.md b/.changeset/cyan-cycles-rhyme.md new file mode 100644 index 00000000..728ebe15 --- /dev/null +++ b/.changeset/cyan-cycles-rhyme.md @@ -0,0 +1,5 @@ +--- +"@defer/client": minor +--- + +Fix VSCode dynamic imports resolving on `@defer/client/index` diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 51ce6971..32d8dbcc 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -22,7 +22,6 @@ jobs: access_token: ${{ secrets.GITHUB_TOKEN }} - run: "npm ci --no-audit" - run: "npm run build" - - run: "npx bob check" env: DEFER_NO_LOCAL_SCHEDULER: 1 - name: "release / pull_request" diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e65ebade..99b418c6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -28,7 +28,6 @@ jobs: - run: "bunx prettier -c src/" - run: "bun run test" - run: "bun run build" - - run: "bunx bob check" env: DEFER_NO_LOCAL_SCHEDULER: 1 @@ -50,6 +49,5 @@ jobs: - run: "npx prettier -c src/" - run: "npm run test" - run: "npm run build" - - run: "npx bob check" env: DEFER_NO_LOCAL_SCHEDULER: 1 diff --git a/.gitignore b/.gitignore index a0f1737e..ce2de85d 100644 --- a/.gitignore +++ b/.gitignore @@ -103,4 +103,3 @@ dist # TernJS port file .tern-port -.bob diff --git a/.prettierignore b/.prettierignore index 2c81cf92..2975f3ad 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,4 +2,3 @@ node_modules/ coverage/ dist/ yarn.lock -.bob/ diff --git a/package-lock.json b/package-lock.json index 7f8729fd..07971135 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,6 @@ "@types/jest": "^29.5.11", "@types/react": "^18.2.48", "@typescript-eslint/eslint-plugin": "^6.20.0", - "bob-the-bundler": "^7.0.1", "eslint": "8.56.0", "eslint-config-prettier": "9.1.0", "jest": "^29.7", @@ -2966,159 +2965,6 @@ "node": ">=4" } }, - "node_modules/bob-the-bundler": { - "version": "7.0.1", - "dev": true, - "license": "MIT", - "dependencies": { - "consola": "^3.0.0", - "execa": "7.1.1", - "fs-extra": "^11.1.0", - "globby": "^13.1.3", - "js-yaml": "^4.1.0", - "lodash.get": "^4.4.2", - "p-limit": "^4.0.0", - "resolve.exports": "^2.0.0", - "tslib": "^2.0.0", - "yargs": "^17.6.2", - "zod": "^3.20.2" - }, - "bin": { - "bob": "dist/index.js" - }, - "engines": { - "node": ">=16", - "pnpm": ">=8" - }, - "peerDependencies": { - "typescript": "^5.0.0" - } - }, - "node_modules/bob-the-bundler/node_modules/argparse": { - "version": "2.0.1", - "dev": true, - "license": "Python-2.0" - }, - "node_modules/bob-the-bundler/node_modules/fast-glob": { - "version": "3.3.1", - "dev": true, - "license": "MIT", - "dependencies": { - "@nodelib/fs.stat": "^2.0.2", - "@nodelib/fs.walk": "^1.2.3", - "glob-parent": "^5.1.2", - "merge2": "^1.3.0", - "micromatch": "^4.0.4" - }, - "engines": { - "node": ">=8.6.0" - } - }, - "node_modules/bob-the-bundler/node_modules/fs-extra": { - "version": "11.1.1", - "dev": true, - "license": "MIT", - "dependencies": { - "graceful-fs": "^4.2.0", - "jsonfile": "^6.0.1", - "universalify": "^2.0.0" - }, - "engines": { - "node": ">=14.14" - } - }, - "node_modules/bob-the-bundler/node_modules/globby": { - "version": "13.2.2", - "dev": true, - "license": "MIT", - "dependencies": { - "dir-glob": "^3.0.1", - "fast-glob": "^3.3.0", - "ignore": "^5.2.4", - "merge2": "^1.4.1", - "slash": "^4.0.0" - }, - "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/bob-the-bundler/node_modules/js-yaml": { - "version": "4.1.0", - "dev": true, - "license": "MIT", - "dependencies": { - "argparse": "^2.0.1" - }, - "bin": { - "js-yaml": "bin/js-yaml.js" - } - }, - "node_modules/bob-the-bundler/node_modules/jsonfile": { - "version": "6.1.0", - "dev": true, - "license": "MIT", - "dependencies": { - "universalify": "^2.0.0" - }, - "optionalDependencies": { - "graceful-fs": "^4.1.6" - } - }, - "node_modules/bob-the-bundler/node_modules/p-limit": { - "version": "4.0.0", - "dev": true, - "license": "MIT", - "dependencies": { - "yocto-queue": "^1.0.0" - }, - "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/bob-the-bundler/node_modules/slash": { - "version": "4.0.0", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/bob-the-bundler/node_modules/universalify": { - "version": "2.0.0", - "dev": true, - "license": "MIT", - "engines": { - "node": ">= 10.0.0" - } - }, - "node_modules/bob-the-bundler/node_modules/yocto-queue": { - "version": "1.0.0", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=12.20" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/bob-the-bundler/node_modules/zod": { - "version": "3.22.2", - "dev": true, - "license": "MIT", - "funding": { - "url": "https://github.com/sponsors/colinhacks" - } - }, "node_modules/brace-expansion": { "version": "1.1.11", "dev": true, @@ -3399,14 +3245,6 @@ "dev": true, "license": "MIT" }, - "node_modules/consola": { - "version": "3.2.3", - "dev": true, - "license": "MIT", - "engines": { - "node": "^14.18.0 || >=16.10.0" - } - }, "node_modules/convert-source-map": { "version": "1.9.0", "dev": true, @@ -4002,28 +3840,6 @@ "node": ">=0.10.0" } }, - "node_modules/execa": { - "version": "7.1.1", - "dev": true, - "license": "MIT", - "dependencies": { - "cross-spawn": "^7.0.3", - "get-stream": "^6.0.1", - "human-signals": "^4.3.0", - "is-stream": "^3.0.0", - "merge-stream": "^2.0.0", - "npm-run-path": "^5.1.0", - "onetime": "^6.0.0", - "signal-exit": "^3.0.7", - "strip-final-newline": "^3.0.0" - }, - "engines": { - "node": "^14.18.0 || ^16.14.0 || >=18.0.0" - }, - "funding": { - "url": "https://github.com/sindresorhus/execa?sponsor=1" - } - }, "node_modules/exit": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/exit/-/exit-0.1.2.tgz", @@ -4472,14 +4288,6 @@ "integrity": "sha512-UNopramDEhHJD+VR+ehk8rOslwSfByxPIZyJRfV739NDhN5LF1fa1MqnzKm2lGTQRjNrjK19Q5fhkgIfjlVUKw==", "dev": true }, - "node_modules/human-signals": { - "version": "4.3.1", - "dev": true, - "license": "Apache-2.0", - "engines": { - "node": ">=14.18.0" - } - }, "node_modules/iconv-lite": { "version": "0.4.24", "dev": true, @@ -4766,17 +4574,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/is-stream": { - "version": "3.0.0", - "dev": true, - "license": "MIT", - "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/is-string": { "version": "1.0.7", "dev": true, @@ -5715,11 +5512,6 @@ "node": ">=8" } }, - "node_modules/lodash.get": { - "version": "4.4.2", - "dev": true, - "license": "MIT" - }, "node_modules/lodash.memoize": { "version": "4.1.2", "dev": true, @@ -5882,17 +5674,6 @@ "node": ">=8.6" } }, - "node_modules/mimic-fn": { - "version": "4.0.0", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/min-indent": { "version": "1.0.1", "dev": true, @@ -6058,31 +5839,6 @@ "node": ">=0.10.0" } }, - "node_modules/npm-run-path": { - "version": "5.1.0", - "dev": true, - "license": "MIT", - "dependencies": { - "path-key": "^4.0.0" - }, - "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/npm-run-path/node_modules/path-key": { - "version": "4.0.0", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/object-inspect": { "version": "1.12.2", "dev": true, @@ -6124,20 +5880,6 @@ "wrappy": "1" } }, - "node_modules/onetime": { - "version": "6.0.0", - "dev": true, - "license": "MIT", - "dependencies": { - "mimic-fn": "^4.0.0" - }, - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/optionator": { "version": "0.9.3", "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.3.tgz", @@ -7191,17 +6933,6 @@ "node": ">=4" } }, - "node_modules/strip-final-newline": { - "version": "3.0.0", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/strip-indent": { "version": "3.0.0", "dev": true, diff --git a/package.json b/package.json index bacc7d83..ff3428a6 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,8 @@ "name": "@defer/client", "version": "2.2.1", "description": "Zero infrastructure NodeJS background jobs", - "type": "module", - "main": "dist/cjs/index.js", - "module": "dist/esm/index.js", + "main": "index.js", + "module": "index.js", "repository": { "type": "git", "url": "git+ssh://git@github.com/defer-run/defer.client.git" @@ -15,7 +14,8 @@ "node": ">=18" }, "scripts": { - "build": "node utils/update-version.mjs && npx bob build && node utils/fix-ts-sub-exports.mjs", + "build": "npm run clean && node utils/update-version.mjs && tsc --project tsconfig.build.json && cp package.json LICENSE README.md CHANGELOG.md dist", + "clean": "rm -rf ./dist", "lint": "eslint src/**/*.ts", "release": "changeset publish", "test": "npx jest --passWithNoTests --config ./tests/jest.ts --silent --runInBand", @@ -31,7 +31,6 @@ "@types/jest": "^29.5.11", "@types/react": "^18.2.48", "@typescript-eslint/eslint-plugin": "^6.20.0", - "bob-the-bundler": "^7.0.1", "eslint": "8.56.0", "eslint-config-prettier": "9.1.0", "jest": "^29.7", @@ -42,51 +41,41 @@ "ts-node": "^10.9.2", "typescript": "^5.3" }, - "typings": "dist/typings/index.d.ts", + "typings": "index.d.ts", "typescript": { - "definition": "dist/typings/index.d.ts" + "definition": "index.d.ts" }, "exports": { ".": { "require": { - "types": "./dist/typings/index.d.cts", - "default": "./dist/cjs/index.js" + "types": "./index.d.cts", + "default": "./index.js" }, "import": { - "types": "./dist/typings/index.d.ts", - "default": "./dist/esm/index.js" + "types": "./index.d.ts", + "default": "./index.js" }, "default": { - "types": "./dist/typings/index.d.ts", - "default": "./dist/esm/index.js" + "types": "./index.d.ts", + "default": "./index.js" } }, "./next": { "require": { - "types": "./dist/typings/next/index.d.cts", - "default": "./dist/cjs/next/index.js" + "types": "./next/index.d.cts", + "default": "./next/index.js" }, "import": { - "types": "./dist/typings/next/index.d.ts", - "default": "./dist/esm/next/index.js" + "types": "./next/index.d.ts", + "default": "./next/index.js" }, "default": { - "types": "./dist/typings/next/index.d.ts", - "default": "./dist/esm/next/index.js" + "types": "./next/index.d.ts", + "default": "./next/index.js" } }, "./package.json": "./package.json" }, - "typesVersions": { - "*": { - "index": [ - "typings/index.d.ts" - ], - "next": [ - "typings/next/index.d.ts" - ] - } - }, "publishConfig": { "directory": "dist", "access": "public" diff --git a/src/backend.ts b/src/backend.ts index 9e2b2b65..3a5ae926 100644 --- a/src/backend.ts +++ b/src/backend.ts @@ -12,7 +12,7 @@ // OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. -import { DeferableFunction, DeferredFunction } from "./index.js"; +import { DeferableFunction, DeferredFunction } from "./index"; export type ExecutionState = | "created" diff --git a/src/backend/local.ts b/src/backend/local.ts index 17f25acc..8393923e 100644 --- a/src/backend/local.ts +++ b/src/backend/local.ts @@ -32,17 +32,17 @@ import { PageResult, ReRunExecutionResult, RescheduleExecutionResult, -} from "../backend.js"; +} from "../backend"; import { DeferableFunction, DeferredFunction, ExecutionMetadata, -} from "../index.js"; -import { error, info } from "../logger.js"; -import { getEnv, randomUUID, sleep, stringify } from "../utils.js"; -import version from "../version.js"; -import { Counter } from "./local/counter.js"; -import { KV } from "./local/kv.js"; +} from "../index"; +import { error, info } from "../logger"; +import { getEnv, randomUUID, sleep, stringify } from "../utils"; +import version from "../version"; +import { Counter } from "./local/counter"; +import { KV } from "./local/kv"; interface InternalExecution { id: string; diff --git a/src/backend/remote.ts b/src/backend/remote.ts index d2d7810f..6487a3a5 100644 --- a/src/backend/remote.ts +++ b/src/backend/remote.ts @@ -33,10 +33,10 @@ import { PageRequest, ReRunExecutionResult, RescheduleExecutionResult, -} from "../backend.js"; -import { DeferableFunction, DeferredFunction } from "../index.js"; -import { getEnv, stringify } from "../utils.js"; -import { HTTPClient, makeHTTPClient } from "./remote/httpClient.js"; +} from "../backend"; +import { DeferableFunction, DeferredFunction } from "../index"; +import { getEnv, stringify } from "../utils"; +import { HTTPClient, makeHTTPClient } from "./remote/httpClient"; export interface SingleObjectResponse { data: T; diff --git a/src/backend/remote/httpClient.ts b/src/backend/remote/httpClient.ts index 4da9592c..44c1fcf0 100644 --- a/src/backend/remote/httpClient.ts +++ b/src/backend/remote/httpClient.ts @@ -12,9 +12,9 @@ // OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. -import { DeferError } from "../../backend.js"; -import { errorMessage } from "../../utils.js"; -import VERSION from "../../version.js"; +import { DeferError } from "../../backend"; +import { errorMessage } from "../../utils"; +import VERSION from "../../version"; export type HTTPClient = ( method: string, diff --git a/src/index.ts b/src/index.ts index ef97ee9b..142915c0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,12 +24,12 @@ import { PageRequest, ReRunExecutionResult, RescheduleExecutionResult, -} from "./backend.js"; -import * as localBackend from "./backend/local.js"; -import * as remoteBackend from "./backend/remote.js"; -import { jitter } from "./jitter.js"; -import { info, warn } from "./logger.js"; -import { Duration, fromDurationToDate, getEnv, sleep } from "./utils.js"; +} from "./backend"; +import * as localBackend from "./backend/local"; +import * as remoteBackend from "./backend/remote"; +import { jitter } from "./jitter"; +import { info, warn } from "./logger"; +import { Duration, fromDurationToDate, getEnv, sleep } from "./utils"; const INTERNAL_VERSION = 6; const RETRY_MAX_ATTEMPTS_PLACEHOLDER = 13; diff --git a/src/logger.ts b/src/logger.ts index 54d6a48d..563a451d 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -12,7 +12,7 @@ // OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. -import { isDebugEnabled } from "./utils.js"; +import { isDebugEnabled } from "./utils"; // source: https://github.com/csquared/node-logfmt/blob/master/lib/stringify.js function fmtData(data: any): string { diff --git a/src/next/asNextRoute.ts b/src/next/asNextRoute.ts index 41ea3438..73c39e0c 100644 --- a/src/next/asNextRoute.ts +++ b/src/next/asNextRoute.ts @@ -1,10 +1,6 @@ import type { NextRequest, NextResponse } from "next/server"; -import { DeferError } from "../backend.js"; -import { - DeferredFunction, - getExecution, - getExecutionResult, -} from "../index.js"; +import { DeferError } from "../backend"; +import { DeferredFunction, getExecution, getExecutionResult } from "../index"; export interface DeferNextRoute { GetHandler: (request: NextRequest) => Promise; diff --git a/src/next/index.ts b/src/next/index.ts index 385d1a11..be1f574d 100644 --- a/src/next/index.ts +++ b/src/next/index.ts @@ -1,2 +1,2 @@ -export { DeferNextRoute, asNextRoute } from "./asNextRoute.js"; -export { UseDeferRoute, useDeferRoute } from "./useDeferRoute.js"; +export { DeferNextRoute, asNextRoute } from "./asNextRoute"; +export { UseDeferRoute, useDeferRoute } from "./useDeferRoute"; diff --git a/src/next/useDeferRoute.ts b/src/next/useDeferRoute.ts index ee809e90..aaf32709 100644 --- a/src/next/useDeferRoute.ts +++ b/src/next/useDeferRoute.ts @@ -1,7 +1,7 @@ "use client"; import { useCallback, useRef, useState } from "react"; -import type { ExecutionState } from "../backend.js"; -import type { DeferredFunction } from "../index.js"; +import type { ExecutionState } from "../backend"; +import type { DeferredFunction } from "../index"; export type UseDeferRoute = [ execute: (...args: ARA extends true ? any : A) => void, diff --git a/tests/awaitResult.spec.ts b/tests/awaitResult.spec.ts deleted file mode 100644 index 4b36c095..00000000 --- a/tests/awaitResult.spec.ts +++ /dev/null @@ -1,80 +0,0 @@ -import { awaitResult, defer } from "../src"; -import { DeferError } from "../src/errors"; -import { makeHTTPClient } from "../src/httpClient"; -import { jitter } from "../src/jitter"; - -jest.mock("../src/httpClient"); -jest.mock("../src/jitter"); -jest.setTimeout(20000); - -describe("awaitResult(deferFn)", () => { - describe("common", () => { - it("defer() raises a type error is function is not async", () => { - function myFunction(_customerId: string) {} - - // @ts-expect-error Type 'void' is not assignable to type 'Promise'.ts(2345) - const deferred = defer(myFunction); - }); - }); - - describe("when Defer is active (`DEFER_TOKEN` is set)", () => { - describe("with successful functions execution", () => { - beforeAll(() => { - const responseFn = jest - .fn() - // first `/exec` call - .mockImplementationOnce(() => { - return { id: "1" }; - }) - // first `/runs/:id` call - .mockImplementationOnce(() => { - return { id: "1", state: "created" }; - }) - // second `/runs/:id` call - .mockImplementationOnce(() => { - return { id: "1", state: "started" }; - }) - // third `/runs/:id` call - .mockImplementationOnce(() => { - return { id: "1", state: "succeed", result: "coucou" }; - }); - - jest.mocked(jitter).mockReturnValue(0); - jest - .mocked(makeHTTPClient) - // first `/exec` call - .mockImplementation(() => async () => { - return responseFn(); - }); - - process.env["DEFER_TOKEN"] = "test"; - process.env["DEFER_DEBUG"] = "1"; - }); - - it("should NOT call the wrapped function and return the function execution result", async () => { - const myFunction = jest.fn(async (_str: string) => "Hello World!"); - const deferred = defer(myFunction); - - const awaitable = awaitResult(deferred); - const result = await awaitable(""); - expect(result).toEqual("coucou"); - expect(myFunction).not.toHaveBeenCalled(); - }); - }); - }); - - describe("when Defer is inactive (`DEFER_TOKEN` is unset)", () => { - beforeAll(() => { - process.env["DEFER_TOKEN"] = ""; - }); - it("should rethrow any error as `DeferError`", async () => { - const myFunction = async (_str: string) => { - throw new Error("my function failed locally!"); - }; - const deferred = defer(myFunction); - const awaitable = awaitResult(deferred); - - await expect(awaitable("")).rejects.toThrow(DeferError); - }); - }); -}); diff --git a/tests/backend/local.test.ts b/tests/backend/local.test.ts index b915ee26..ea758023 100644 --- a/tests/backend/local.test.ts +++ b/tests/backend/local.test.ts @@ -1,15 +1,15 @@ process.env["DEFER_NO_LOCAL_SCHEDULER"] = "1"; process.env["DEFER_NO_BANNER"] = "1"; -import { ExecutionNotFound } from "../../src/backend.js"; +import { ExecutionNotFound } from "../../src/backend"; import { cancelExecution, enqueue, getExecution, reRunExecution, rescheduleExecution, -} from "../../src/backend/local.js"; -import { defer } from "../../src/index.js"; +} from "../../src/backend/local"; +import { defer } from "../../src/index"; const myFunc = async () => console.log("the cake is a lie"); const myDeferedFunc = defer(myFunc); diff --git a/tests/backend/local/counter.test.ts b/tests/backend/local/counter.test.ts index 3dad3871..980bf293 100644 --- a/tests/backend/local/counter.test.ts +++ b/tests/backend/local/counter.test.ts @@ -1,4 +1,4 @@ -import { Counter } from "../../../src/backend/local/counter.js"; +import { Counter } from "../../../src/backend/local/counter"; describe("incr/1", () => { describe("increment non existing key", () => { diff --git a/tests/backend/local/kv.test.ts b/tests/backend/local/kv.test.ts index 720c1499..b908938c 100644 --- a/tests/backend/local/kv.test.ts +++ b/tests/backend/local/kv.test.ts @@ -1,4 +1,4 @@ -import { KV } from "../../../src/backend/local/kv.js"; +import { KV } from "../../../src/backend/local/kv"; describe("set/2", () => { it("adds value in the store", async () => { diff --git a/tests/backend/remote.test.ts b/tests/backend/remote.test.ts index 014ce987..c27d3d0a 100644 --- a/tests/backend/remote.test.ts +++ b/tests/backend/remote.test.ts @@ -7,7 +7,7 @@ import { ExecutionNotCancellable, ExecutionNotFound, ExecutionNotReschedulable, -} from "../../src/backend.js"; +} from "../../src/backend"; import { cancelExecution, enqueue, @@ -16,8 +16,8 @@ import { listExecutions, reRunExecution, rescheduleExecution, -} from "../../src/backend/remote.js"; -import { defer } from "../../src/index.js"; +} from "../../src/backend/remote"; +import { defer } from "../../src/index"; global.fetch = jest.fn(); diff --git a/tests/backend/remote/httpClient.test.ts b/tests/backend/remote/httpClient.test.ts index 3428984b..8a576e0b 100644 --- a/tests/backend/remote/httpClient.test.ts +++ b/tests/backend/remote/httpClient.test.ts @@ -1,7 +1,7 @@ import { makeHTTPClient, ClientError, -} from "../../../src/backend/remote/httpClient.js"; +} from "../../../src/backend/remote/httpClient"; global.fetch = jest.fn(); diff --git a/tests/index.test.ts b/tests/index.test.ts index 55d2357d..166438ee 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -15,7 +15,7 @@ import { listExecutions, reRunExecution, rescheduleExecution, -} from "../src/index.js"; +} from "../src/index"; var stop: () => Promise | undefined; diff --git a/tests/jest.ts b/tests/jest.ts index 15ce2bcd..ab58de4f 100644 --- a/tests/jest.ts +++ b/tests/jest.ts @@ -15,7 +15,6 @@ const config: Config.InitialOptions = { }, coverageDirectory: "../coverage", testEnvironment: "node", - resolver: "bob-the-bundler/jest-resolver", }; export default config; diff --git a/tests/logger.test.ts b/tests/logger.test.ts index b4e593d6..59745938 100644 --- a/tests/logger.test.ts +++ b/tests/logger.test.ts @@ -1,4 +1,4 @@ -import { error, info, log, warn } from "../src/logger.js"; +import { error, info, log, warn } from "../src/logger"; describe("log/3", () => { it("test without data", () => { diff --git a/tests/next/asNextRoute.spec.ts b/tests/next/asNextRoute.test.ts similarity index 91% rename from tests/next/asNextRoute.spec.ts rename to tests/next/asNextRoute.test.ts index 4681531e..3c32208b 100644 --- a/tests/next/asNextRoute.spec.ts +++ b/tests/next/asNextRoute.test.ts @@ -1,9 +1,12 @@ +process.env["DEFER_NO_LOCAL_SCHEDULER"] = "1"; +process.env["DEFER_NO_BANNER"] = "1"; + import { NextRequest } from "next/server"; import helloWorld from "./fixtures/helloWorld"; import { POST, GET } from "./fixtures/route"; import { POST as POSTWithProxy } from "./fixtures/routeWithProxy"; -import { getExecution } from "../../src/index"; -import { APIError } from "../../src/errors"; +import { backend, getExecution } from "../../src/index"; +import { ExecutionNotFound } from "../../src/backend"; jest.mock("./fixtures/helloWorld"); jest.mock("../../src/index", () => { @@ -15,11 +18,22 @@ jest.mock("../../src/index", () => { }; }); +var stop: () => Promise | undefined; + +beforeAll(function () { + stop = (backend as any).start(); +}); + +afterAll(function () { + stop(); +}); + + describe("asNextRoute()", () => { describe("POST() - background function invocation", () => { describe("valid empty params, without proxy", () => { beforeEach(() => - jest.mocked(helloWorld).mockResolvedValue({ id: "test-id" }) + jest.mocked(helloWorld).mockResolvedValue({ id: "test-id" } as any) ); test("properly call the background function and forward the execution ID", async () => { @@ -42,7 +56,7 @@ describe("asNextRoute()", () => { describe("valid params, without proxy", () => { beforeEach(() => - jest.mocked(helloWorld).mockResolvedValue({ id: "test-id" }) + jest.mocked(helloWorld).mockResolvedValue({ id: "test-id" } as any) ); afterEach(() => { jest.mocked(helloWorld).mockReset(); @@ -68,7 +82,7 @@ describe("asNextRoute()", () => { describe("valid params, with proxy", () => { beforeEach(() => - jest.mocked(helloWorld).mockResolvedValue({ id: "test-id" }) + jest.mocked(helloWorld).mockResolvedValue({ id: "test-id" } as any) ); test("properly call the background function with params and forward the execution ID", async () => { @@ -104,7 +118,7 @@ describe("asNextRoute()", () => { "valid query params, invalid execution ID", "?id=not-found-id", "not-found-id", - new APIError("execution not found", ""), + new ExecutionNotFound("execution not found"), [500, { error: "Error: execution not found", id: "not-found-id" }], ], [ @@ -129,8 +143,7 @@ describe("asNextRoute()", () => { throw executionResult; }); } else { - // @ts-expect-error unflexible TS \o/ - jest.mocked(getExecution).mockResolvedValue(executionResult); + jest.mocked(getExecution).mockResolvedValue(executionResult as any); } }); afterEach(() => { diff --git a/tests/next/useDeferRoute.spec.ts b/tests/next/useDeferRoute.spec.ts deleted file mode 100644 index 8be58858..00000000 --- a/tests/next/useDeferRoute.spec.ts +++ /dev/null @@ -1,10 +0,0 @@ -// import { useDeferRoute } from "../../src/next/useDeferRoute"; -// import helloWorld from "./fixtures/helloWorld"; - -describe("useDeferRoute()", () => { - test("1+1", () => { - expect(1 + 1).toEqual(2); - }); -}); - -// const [coucou, { loading, result }] = useDeferRoute('') diff --git a/tests/utils.test.ts b/tests/utils.test.ts index bc9b221f..03728c47 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -3,7 +3,7 @@ import { getEnv, isDebugEnabled, randomUUID, -} from "../src/utils.js"; +} from "../src/utils"; describe("randomUUID/0", () => { describe("when globalThis.crypto exist", () => { diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 00000000..d01964e6 --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "exclude": [ + "src/**/*.spec.ts", + "src/**/*.test.ts", + "src/test/**/*", + "scripts/**/*" + ] +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 12d2a14f..8f84d06f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,11 +1,11 @@ { "$schema": "https://json.schemastore.org/tsconfig", "compilerOptions": { - "lib": ["es2022", "dom"], - "outDir": "./dist", + "target": "es2017", + "lib": ["es2022", "DOM"], "module": "commonjs", + "outDir": "./dist", "declaration": true, - "target": "es2021", "strict": true, "esModuleInterop": true, "skipLibCheck": true, diff --git a/utils/fix-ts-sub-exports.mjs b/utils/fix-ts-sub-exports.mjs deleted file mode 100644 index b4b57edd..00000000 --- a/utils/fix-ts-sub-exports.mjs +++ /dev/null @@ -1,18 +0,0 @@ -import fs from "fs"; -import { dirname, join } from "path"; -import { fileURLToPath } from "url"; - -const __dirname = dirname(fileURLToPath(import.meta.url)); -const file = fs.readFileSync(join(__dirname, "../dist/package.json"), "utf-8"); -const pkg = JSON.parse(file); - -(pkg.typesVersions = { - "*": { - index: ["typings/index.d.ts"], - next: ["typings/next/index.d.ts"], - }, -}), - fs.writeFileSync( - join(__dirname, "../dist/package.json"), - JSON.stringify(pkg, null, 2) - );