diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml new file mode 100644 index 0000000..587745f --- /dev/null +++ b/.github/workflows/node.js.yml @@ -0,0 +1,23 @@ +name: Run tests +on: + push: + branches: ["main"] + pull_request: + branches: ["main"] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Use Node.js + uses: actions/setup-node@v4 + with: + cache-dependency-path: | + example/package.json + package.json + node-version: "18.x" + cache: "npm" + - run: npm i + - run: npm ci + - run: cd example && npm i && cd .. + - run: npm test diff --git a/package.json b/package.json index 9453ed3..4197418 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,10 @@ "build:cjs": "tsc --project ./commonjs.json && echo '{\\n \"type\": \"commonjs\"\\n}' > dist/commonjs/package.json", "dev": "cd example; npm run dev", "typecheck": "tsc --noEmit", - "prepare": "npm run build" + "prepare": "npm run build", + "test": "vitest", + "test:debug": "vitest --inspect-brk --no-file-parallelism", + "test:coverage": "vitest run --coverage --coverage.reporter=text" }, "files": [ "dist", diff --git a/src/client/index.test.ts b/src/client/index.test.ts new file mode 100644 index 0000000..3a49cf8 --- /dev/null +++ b/src/client/index.test.ts @@ -0,0 +1,15 @@ +import { expect, test } from "vitest"; +import { ConvexError } from "convex/values"; +import { isRateLimitError, RateLimitError } from "./index.js"; +test("isRateLimitError", () => { + expect( + isRateLimitError( + new ConvexError({ + kind: "RateLimited", + name: "foo", + retryAfter: 1, + } as RateLimitError) + ) + ).toBe(true); + expect(isRateLimitError(new ConvexError({ kind: "foo" }))).toBe(false); +}); diff --git a/src/client/index.ts b/src/client/index.ts index 121d5ac..7745822 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -27,11 +27,7 @@ export const HOUR = 60 * MINUTE; export function isRateLimitError( error: unknown ): error is { data: RateLimitError } { - return ( - error instanceof ConvexError && - "kind" in error.data && - error.data.kind === "RateLimited" - ); + return error instanceof ConvexError && error.data["kind"] === "RateLimited"; } /**