Skip to content

Commit

Permalink
Add browser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kigawas committed Nov 1, 2024
1 parent c2b32fc commit 8ca91e1
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 35 deletions.
29 changes: 24 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}

- name: Set up browser tests
working-directory: tests-browser
run: pnpm install && pnpm exec playwright install chromium firefox

- name: Install webkit browser on macos
working-directory: tests-browser
if: matrix.os == 'macos-latest'
run: pnpm exec playwright install

- name: Run browser tests (chromium, firefox)
run: |
pnpm test:browser
pnpm test:browser -- --browser=firefox
- name: Run browser tests (webkit)
if: matrix.os == 'macos-latest'
run: |
pnpm test:browser -- --browser=webkit
check-runtimes:
runs-on: ubuntu-latest
steps:
Expand All @@ -50,8 +69,8 @@ jobs:
with:
deno-version: v2.x

- run: pnpm install && pnpm build
- run: cd example && pnpm install
- run: bun run example/main.js
- run: deno run --allow-read example/main.js
- run: node example/main.js
- run: pnpm install && pnpm build && cd example && pnpm install

- name: check main.js
run: |
./scripts/check-runtime.sh example/main.js
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ node_modules/
coverage/
dist/

.DS_Store

# example
example/bun.lockb
example/deno.lock
example/pnpm-lock.yaml

.DS_Store
# browser tests
tests-browser/pnpm-lock.yaml
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@
},
"scripts": {
"build": "npx tsc",
"test": "vitest"
"test": "vitest",
"test:browser": "cd tests-browser && pnpm test"
},
"peerDependencies": {
"@noble/ciphers": "^1.0.0"
},
"devDependencies": {
"@types/node": "^22.8.4",
"@types/node": "^22.8.6",
"@vitest/coverage-v8": "^2.1.4",
"typescript": "^5.6.3",
"vitest": "^2.1.4"
Expand Down
48 changes: 24 additions & 24 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions scripts/check-runtime.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
bun run $1
deno run --allow-read $1
node $1
18 changes: 18 additions & 0 deletions tests-browser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "tests-browser",
"description": "Test browser compatibility",
"private": true,
"version": "0.1.0",
"scripts": {
"test": "vitest"
},
"dependencies": {
"@ecies/ciphers": "file:.."
},
"devDependencies": {
"@vitest/browser": "^2.1.4",
"playwright": "^1.48.2",
"vitest": "^2.1.4"
},
"packageManager": "pnpm@9.12.3+sha512.cce0f9de9c5a7c95bef944169cc5dfe8741abfb145078c0d508b868056848a87c81e626246cb60967cbd7fd29a6c062ef73ff840d96b3c86c40ac92cf4a813ee"
}
39 changes: 39 additions & 0 deletions tests-browser/random.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { aes256cbc, aes256gcm } from "@ecies/ciphers/aes";
import { xchacha20 } from "@ecies/ciphers/chacha";
import { randomBytes } from "@noble/ciphers/webcrypto";

import { describe, expect, it } from "vitest";

const TEXT = "hello browser🌍!";
const encoder = new TextEncoder();
const data = encoder.encode(TEXT);

describe("test random", () => {
function checkCipher(
callback: typeof aes256gcm | typeof aes256cbc | typeof xchacha20,
key: Uint8Array,
nonce: Uint8Array,
aad?: Uint8Array
) {
const cipher = callback(key, nonce, aad);
expect(cipher.decrypt(cipher.encrypt(data))).toStrictEqual(data);
}

it("tests aes gcm", () => {
checkCipher(aes256gcm, randomBytes(32), randomBytes(16), randomBytes(8));
checkCipher(aes256gcm, randomBytes(32), randomBytes(12), randomBytes(8));
});

it("tests aes cbc", () => {
const key = randomBytes();
const nonce = randomBytes(16);
checkCipher(aes256cbc, key, nonce);
});

it("tests xchacha20", () => {
const key = randomBytes();
const nonce = randomBytes(24);
const aad = randomBytes(8);
checkCipher(xchacha20, key, nonce, aad);
});
});
7 changes: 4 additions & 3 deletions vitest.config.ts → tests-browser/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
coverage: {
include: ["src/**"],
browser: {
enabled: true,
provider: "v8",
name: "chromium",
provider: "playwright",
providerOptions: {},
},
},
});
12 changes: 12 additions & 0 deletions vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { configDefaults, defineConfig } from "vitest/config";

export default defineConfig({
test: {
exclude: [...configDefaults.exclude, "tests-browser/**"],
coverage: {
include: ["src/**"],
enabled: true,
provider: "v8",
},
},
});

0 comments on commit 8ca91e1

Please sign in to comment.