Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add test framework #5

Merged
merged 5 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/build-lint-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Build Lint & Test

on:
pull_request:
push:
branches:
- main

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build-lint-test:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- uses: pnpm/action-setup@v4
name: Install pnpm

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm

- name: Install dependencies
run: pnpm install

- name: Run CI
run: pnpm run ci
7 changes: 7 additions & 0 deletions apps/example/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": true,
"singleQuote": false,
"trailingComma": "all",
"printWidth": 80,
"tabWidth": 2
}
2 changes: 1 addition & 1 deletion apps/example/components/ConnectButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const ConnectButton = ({ className }: ConnectButton) => {
<div
className={cn(
!ready && "pointer-events-none hidden select-none opacity-0",
className
className,
)}
aria-hidden={ready ? "false" : "true"}
>
Expand Down
2 changes: 1 addition & 1 deletion apps/example/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const Header = ({
<div
className={cn(
"fixed top-0 z-20 flex w-full items-center justify-between bg-transparent p-4 md:p-5",
className
className,
)}
{...props}
>
Expand Down
4 changes: 2 additions & 2 deletions apps/example/components/ui/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const buttonVariants = cva(
variant: "ghost",
size: "default",
},
}
},
);

export type ButtonProps = {
Expand All @@ -50,7 +50,7 @@ const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
{...props}
/>
);
}
},
);
Button.displayName = "Button";

Expand Down
20 changes: 10 additions & 10 deletions apps/example/components/ui/popover.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use client"
"use client";

import * as React from "react"
import * as PopoverPrimitive from "@radix-ui/react-popover"
import * as React from "react";
import * as PopoverPrimitive from "@radix-ui/react-popover";

import { cn } from "@/lib/utils"
import { cn } from "@/lib/utils";

const Popover = PopoverPrimitive.Root
const Popover = PopoverPrimitive.Root;

const PopoverTrigger = PopoverPrimitive.Trigger
const PopoverTrigger = PopoverPrimitive.Trigger;

const PopoverContent = React.forwardRef<
React.ElementRef<typeof PopoverPrimitive.Content>,
Expand All @@ -20,12 +20,12 @@ const PopoverContent = React.forwardRef<
sideOffset={sideOffset}
className={cn(
"z-50 w-72 rounded-md border border-slate-200 bg-white p-4 text-slate-950 shadow-md outline-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 dark:border-slate-800 dark:bg-slate-950 dark:text-slate-50",
className
className,
)}
{...props}
/>
</PopoverPrimitive.Portal>
))
PopoverContent.displayName = PopoverPrimitive.Content.displayName
));
PopoverContent.displayName = PopoverPrimitive.Content.displayName;

export { Popover, PopoverTrigger, PopoverContent }
export { Popover, PopoverTrigger, PopoverContent };
22 changes: 11 additions & 11 deletions apps/example/components/ui/tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"use client"
"use client";

import * as React from "react"
import * as TooltipPrimitive from "@radix-ui/react-tooltip"
import * as React from "react";
import * as TooltipPrimitive from "@radix-ui/react-tooltip";

import { cn } from "@/lib/utils"
import { cn } from "@/lib/utils";

const TooltipProvider = TooltipPrimitive.Provider
const TooltipProvider = TooltipPrimitive.Provider;

const Tooltip = TooltipPrimitive.Root
const Tooltip = TooltipPrimitive.Root;

const TooltipTrigger = TooltipPrimitive.Trigger
const TooltipTrigger = TooltipPrimitive.Trigger;

const TooltipContent = React.forwardRef<
React.ElementRef<typeof TooltipPrimitive.Content>,
Expand All @@ -20,11 +20,11 @@ const TooltipContent = React.forwardRef<
sideOffset={sideOffset}
className={cn(
"z-50 overflow-hidden rounded-md border border-slate-200 bg-white px-3 py-1.5 text-sm text-slate-950 shadow-md animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 dark:border-slate-800 dark:bg-slate-950 dark:text-slate-50",
className
className,
)}
{...props}
/>
))
TooltipContent.displayName = TooltipPrimitive.Content.displayName
));
TooltipContent.displayName = TooltipPrimitive.Content.displayName;

export { Tooltip, TooltipTrigger, TooltipContent, TooltipProvider }
export { Tooltip, TooltipTrigger, TooltipContent, TooltipProvider };
4 changes: 3 additions & 1 deletion apps/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"build": "next build",
"start": "next start",
"lint": "next lint",
"sdk": "tsx ./scripts/sdk.ts"
"sdk": "tsx ./scripts/sdk.ts",
"ci": "pnpm run build && pnpm run lint"
},
"dependencies": {
"@across-toolkit/sdk": "workspace:*",
Expand Down Expand Up @@ -37,6 +38,7 @@
"eslint": "^8",
"eslint-config-next": "14.2.7",
"postcss": "^8",
"prettier": "^3.2.5",
"tailwindcss": "^3.4.1",
"tsx": "^4.19.0",
"typescript": "^5"
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"build": "turbo build",
"dev": "turbo dev",
"lint": "turbo lint",
"test": "turbo test",
"ci": "turbo ci",
"clean": "turbo clean && rm -rf node_modules",
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
"changeset": "changeset",
Expand Down
7 changes: 7 additions & 0 deletions packages/sdk/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": true,
"singleQuote": false,
"trailingComma": "all",
"printWidth": 80,
"tabWidth": 2
}
36 changes: 30 additions & 6 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
{
"name": "@across-toolkit/sdk",
"version": "0.0.0",
"main": "./dist/index.js",
"main": "./dist/index.mjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"types": "./dist/index.d.mts",
"description": "The official SDK for integrating Across bridge into your dapp.",
"keywords": [
"bridge",
"sdk",
"crypto",
"blockchain"
],
"homepage": "https://github.com/across-protocol/toolkit",
"bugs": {
"url": "https://github.com/across-protocol/toolkit/issues"
},
"author": "Across Protocol <[email protected]> (https://across.to)",
"repository": {
"type": "git",
"url": "git+https://github.com/across-protocol/toolkit.git"
},
"sideEffects": false,
"license": "MIT",
"files": [
Expand All @@ -13,20 +29,28 @@
"node": ">=18.0.0"
},
"scripts": {
"build": "tsup src/index.ts --format esm,cjs --dts",
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
"build": "tsup src/index.ts --format esm --dts",
"dev": "tsup src/index.ts --format esm --watch --dts",
"lint": "pnpm run type-check && eslint \"src/**/*.ts*\"",
"format": "prettier --write .",
"check-format": "prettier --check .",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"type-check": "tsc --noEmit"
"type-check": "tsc",
"check-exports": "attw --pack . --ignore-rules=cjs-resolves-to-esm",
"test": "vitest run",
"ci": "pnpm run build && pnpm run check-exports pnpm npm run lint && pnpm run test"
},
"devDependencies": {
"@across-toolkit/eslint-config": "workspace:*",
"@across-toolkit/typescript-config": "workspace:*",
"@arethetypeswrong/cli": "^0.15.4",
"@types/node": "^20",
"eslint": "^8.57.0",
"prettier": "^3.2.5",
"tsup": "^8.0.2",
"typescript": "^5.3.3",
"viem": "^2.20.1"
"viem": "^2.20.1",
"vitest": "^2.0.5"
},
"publishConfig": {
"access": "public"
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/actions/getAvailableRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function getAvailableRoutes(params?: AvailableRoutesParams) {
const searchParams = params ? buildSearchParams(params) : "";

const res = await fetchAcross(
`${client.apiUrl}/available-routes?${searchParams}`
`${client.apiUrl}/available-routes?${searchParams}`,
);
return (await res.json()) as AvailableRoutesResponse;
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/actions/getSuggestedFees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function getSuggestedFees(params: SuggestedFeesParams) {
try {
const searchParams = buildSearchParams(params);
const res = await fetchAcross(
`${client.apiUrl}/suggested-fees?${searchParams}`
`${client.apiUrl}/suggested-fees?${searchParams}`,
);
return (await res.json()) as SuggestedFeesResponse;
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class AcrossClient {

this.log.debug(
"Client created with args: \n",
JSON.stringify(args, null, 2)
JSON.stringify(args, null, 2),
);
}

Expand All @@ -63,7 +63,7 @@ export class AcrossClient {
public static getInstance(): AcrossClient {
if (this.instance === null) {
throw new Error(
"AcrossClient has not been initialized. Call create() first."
"AcrossClient has not been initialized. Call create() first.",
);
}
return this.instance;
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const fetchAcross = globalThis.fetch.bind(globalThis);
*/

export function buildSearchParams(
params: Record<string, number | string | Array<number | string>>
params: Record<string, number | string | Array<number | string>>,
): string {
const searchParams = new URLSearchParams();
for (const key in params) {
Expand Down
8 changes: 8 additions & 0 deletions packages/sdk/test/actions/getAvailableRoutes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { expect, test } from "vitest";

function sum(a: number, b: number) {
return a + b;
}
test("adds 1 + 2 to equal 3", () => {
expect(sum(1, 2)).toBe(3);
});
30 changes: 30 additions & 0 deletions packages/sdk/test/utils/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect, test, vi } from "vitest";
import { AcrossClient } from "../../src/client";

const client = AcrossClient.create({
useTestnet: true,
integratorId: "TEST_ID",
logLevel: "WARN",
});

const consoleErrorSpy = vi.spyOn(console, "error");
const consoleWarnSpy = vi.spyOn(console, "warn");
const consoleDebugSpy = vi.spyOn(console, "debug");

test("Lower log level not logged", () => {
client.log.error("Should not be logged");

expect(consoleErrorSpy).not.toHaveBeenCalled();
});

test("Equal log level is logged", () => {
client.log.warn("Should be logged");

expect(consoleWarnSpy).toHaveBeenCalled();
});

test("Higher log level is logged", () => {
client.log.debug("Should be logged");

expect(consoleDebugSpy).toHaveBeenCalled();
});
2 changes: 1 addition & 1 deletion packages/sdk/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"include": ["."],
"exclude": ["dist", "build", "node_modules"],
"compilerOptions": {
"module": "NodeNext",
"noEmit": true
}
}
9 changes: 9 additions & 0 deletions packages/sdk/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from "tsup";

export default defineConfig({
entryPoints: ["src/index.ts"],
format: ["esm"],
dts: true,
outDir: "dist",
clean: true,
});
8 changes: 5 additions & 3 deletions packages/typescript-config/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
"allowImportingTsExtensions": true,
"inlineSources": false,
"isolatedModules": true,
"module": "ESNext",
"module": "NodeNext",
"sourceMap": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"preserveWatchOutput": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"strictNullChecks": true
"strictNullChecks": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true
},
"exclude": ["node_modules"]
}
Loading