From 98225fd5322721b041f3218f9490c31ee4a486fa Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 17 May 2024 16:46:58 +0300 Subject: [PATCH] implements a/b tests with vitest --- .github/workflows/deploy-production.yml | 17 +++++++++++++ package.json | 7 ++++-- src/explore/__tests__/explore.test.ts | 32 ++++++++++++++++++++++++ src/onboarding/discover/handle-assets.ts | 4 +-- tsconfig.json | 26 ++++++++++--------- vitest.config.js | 11 ++++++++ 6 files changed, 80 insertions(+), 17 deletions(-) create mode 100644 src/explore/__tests__/explore.test.ts create mode 100644 vitest.config.js diff --git a/.github/workflows/deploy-production.yml b/.github/workflows/deploy-production.yml index 4dce21a..548f4a9 100644 --- a/.github/workflows/deploy-production.yml +++ b/.github/workflows/deploy-production.yml @@ -8,10 +8,27 @@ on: jobs: deploy: runs-on: ubuntu-latest + name: Deploy steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Setup Node.js + uses: actions/setup-node@v2 + with: + node-version: "20" + + - name: Install dependencies + run: yarn + + - name: Run tests + id: run-tests + run: yarn test + - uses: actions/checkout@v3 - name: Deploy app + if: success() uses: cloudflare/wrangler-action@v3 with: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} diff --git a/package.json b/package.json index 0bacf86..d5ff5e9 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "private": true, "scripts": { "deploy": "wrangler publish", - "dev": "wrangler dev --env dev" + "dev": "wrangler dev --env dev", + "test": "vitest" }, "devDependencies": { "@cloudflare/workers-types": "^4.20231218.0", @@ -13,6 +14,8 @@ "wrangler": "^2.19.0" }, "dependencies": { - "tweetnacl": "^1.0.3" + "@cloudflare/vitest-pool-workers": "^0.2.11", + "tweetnacl": "^1.0.3", + "vitest": "1.3.0" } } diff --git a/src/explore/__tests__/explore.test.ts b/src/explore/__tests__/explore.test.ts new file mode 100644 index 0000000..6638963 --- /dev/null +++ b/src/explore/__tests__/explore.test.ts @@ -0,0 +1,32 @@ +import { createExecutionContext, env, waitOnExecutionContext } from "cloudflare:test"; +import { describe, expect, it } from "vitest"; +import { VERSIONS } from "../../utils/versioning"; +import worker from "../../worker"; +import { Env } from "../../../worker-configuration"; + +const LOCAL_BASE_URL = 'http://localhost:8787' +const SERVICES_BASE_URL = 'https://services.kukai.app' + +const ENDPOINTS_TO_TEST = [ + `${VERSIONS.V1}explore`, + `${VERSIONS.V2}explore`, +] + +describe("compares explore to production", () => { + ENDPOINTS_TO_TEST.forEach((endpoint) => { + it(`tests ${endpoint}`, async () => { + const localRequest = new Request(`${LOCAL_BASE_URL}${endpoint}`) + const externalRequest = fetch(`${SERVICES_BASE_URL}${endpoint}`) + + const ctx = createExecutionContext() + const localResponse = await worker.fetch(localRequest, env as Env, ctx) + const externalResponse = await externalRequest + await waitOnExecutionContext(ctx) + + const localJsonResponse = await localResponse.json() as any + const externalJsonResponse = await externalResponse.json() as any + + expect(localJsonResponse.data).toEqual(externalJsonResponse.data) + }) + }) +}) \ No newline at end of file diff --git a/src/onboarding/discover/handle-assets.ts b/src/onboarding/discover/handle-assets.ts index beb7386..59cba89 100644 --- a/src/onboarding/discover/handle-assets.ts +++ b/src/onboarding/discover/handle-assets.ts @@ -1,7 +1,5 @@ import { ROUTES } from "../../router" -const BASE_PATH = ROUTES.ONBOARDING_DISCOVER_ASSETS - export const REMAP: Record = { contract: 'contractAddresses', contractAddress: 'contractAddresses', @@ -18,7 +16,7 @@ const ASSETS = [ export async function handleAssets(request: Request): Promise { const { pathname } = new URL(request.url) - const assetIndex = pathname.substring(BASE_PATH.length - 1) + const assetIndex = pathname.substring(ROUTES.ONBOARDING_DISCOVER_ASSETS.length - 1) const index = Number(assetIndex) diff --git a/tsconfig.json b/tsconfig.json index 2cb9189..98a20cd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,6 @@ { "compilerOptions": { /* Visit https://aka.ms/tsconfig.json to read more about this file */ - /* Projects */ // "incremental": true, /* Enable incremental compilation */ // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ @@ -9,10 +8,11 @@ // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */ // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ - /* Language and Environment */ "target": "es2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, - "lib": ["es2021"] /* Specify a set of bundled library declaration files that describe the target runtime environment. */, + "lib": [ + "es2021" + ] /* Specify a set of bundled library declaration files that describe the target runtime environment. */, "jsx": "react" /* Specify what JSX code is generated. */, // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ @@ -22,7 +22,6 @@ // "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */ // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ - /* Modules */ "module": "es2022" /* Specify what module code is generated. */, // "rootDir": "./", /* Specify the root folder within your source files. */ @@ -31,16 +30,18 @@ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ // "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */ - "types": ["@cloudflare/workers-types"] /* Specify type package names to be included without being referenced in a source file. */, + "types": [ + "@cloudflare/workers-types", + "@cloudflare/workers-types/experimental", + "@cloudflare/vitest-pool-workers" + ] /* Specify type package names to be included without being referenced in a source file. */, // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ "resolveJsonModule": true /* Enable importing .json files */, // "noResolve": true, /* Disallow `import`s, `require`s or ``s from expanding the number of files TypeScript should add to a project. */ - /* JavaScript Support */ "allowJs": true /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */, "checkJs": false /* Enable error reporting in type-checked JavaScript files. */, // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */ - /* Emit */ // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ // "declarationMap": true, /* Create sourcemaps for d.ts files. */ @@ -65,14 +66,12 @@ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ - /* Interop Constraints */ "isolatedModules": true /* Ensure that each file can be safely transpiled without relying on other imports. */, "allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */, // "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */, // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, - /* Type Checking */ "strict": true /* Enable all strict type-checking options. */, // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */ @@ -93,9 +92,12 @@ // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */ // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ - /* Completeness */ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */ - } -} + }, + "include": [ + "./**/*.ts", + "../src/env.d.ts" + ] +} \ No newline at end of file diff --git a/vitest.config.js b/vitest.config.js new file mode 100644 index 0000000..9fdbf1d --- /dev/null +++ b/vitest.config.js @@ -0,0 +1,11 @@ +import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config"; + +export default defineWorkersConfig({ + test: { + poolOptions: { + workers: { + wrangler: { configPath: "./wrangler.toml" }, + }, + }, + }, +}); \ No newline at end of file