Skip to content

Commit 156f5df

Browse files
authored
🧪 add testcode (#10)
1 parent 9b7dc90 commit 156f5df

File tree

9 files changed

+745
-35
lines changed

9 files changed

+745
-35
lines changed

‎.github/workflows/ci.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
pull_request:
7+
types: [opened, synchronize]
8+
9+
jobs:
10+
build:
11+
name: Build and Test
12+
timeout-minutes: 1
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Check out code
17+
uses: actions/checkout@v3
18+
with:
19+
fetch-depth: 2
20+
21+
- name: Setup Node.js environment
22+
uses: actions/setup-node@v3
23+
with:
24+
node-version: 16
25+
cache: "yarn"
26+
27+
- name: Install dependencies
28+
run: yarn
29+
30+
- name: Build
31+
run: yarn build
32+
33+
- name: Test
34+
run: yarn test

‎package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"scripts": {
99
"build": "turbo run build",
1010
"dev": "turbo run dev",
11+
"test": "turbo run test",
1112
"lint": "turbo run lint",
1213
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
1314
"publish-packages": "turbo run build lint test && changeset version && changeset publish"

‎packages/truffle-browser/package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "@wafflestudio/truffle-browser",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"main": "./dist/index.js",
55
"types": "./dist/index.d.ts",
66
"license": "MIT",
77
"scripts": {
88
"dev": "yarn build -- --watch",
9-
"build": "tsup src/index.ts --format cjs --dts"
9+
"build": "tsup src/index.ts --format cjs --dts",
10+
"test": "vitest run"
1011
},
1112
"files": [
1213
"dist/index.js",
@@ -17,8 +18,11 @@
1718
"error-stack-parser": "^2.1.4"
1819
},
1920
"devDependencies": {
21+
"happy-dom": "8.2.6",
2022
"tsup": "6.5.0",
21-
"typescript": "4.9.5"
23+
"typescript": "4.9.5",
24+
"vite": "4.1.1",
25+
"vitest": "0.28.4"
2226
},
2327
"publishConfig": {
2428
"access": "public"
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { describe, it, expect, vi, afterEach, beforeEach } from "vitest";
2+
import { getTruffleClient, TruffleClient } from ".";
3+
import * as postEvent from "./postEvent";
4+
5+
describe("getTruffleClient (disabled)", () => {
6+
let client: TruffleClient;
7+
8+
beforeEach(() => {
9+
client = getTruffleClient({
10+
enabled: false,
11+
app: { name: "1", phase: "2" },
12+
apiKey: "3",
13+
});
14+
});
15+
16+
afterEach(() => {
17+
vi.clearAllMocks();
18+
});
19+
20+
it("capture", () => {
21+
const spy = vi.spyOn(postEvent, "postEvent");
22+
client.capture(new Error("test"));
23+
expect(spy).not.toBeCalled();
24+
});
25+
});
26+
27+
describe("getTruffleClient (enabled)", () => {
28+
let client: TruffleClient;
29+
30+
beforeEach(() => {
31+
client = getTruffleClient({
32+
enabled: true,
33+
app: { name: "1", phase: "2" },
34+
apiKey: "3",
35+
});
36+
});
37+
38+
afterEach(() => {
39+
vi.clearAllMocks();
40+
});
41+
42+
it("capture", () => {
43+
const spy = vi.spyOn(postEvent, "postEvent");
44+
client.capture(new Error("test"));
45+
expect(spy).toBeCalledWith("3", {
46+
app: { name: "1", phase: "2" },
47+
description: "about:blank",
48+
exception: {
49+
className: "Error",
50+
message: "test",
51+
elements: expect.any(Array),
52+
},
53+
runtime: { name: "browser", version: "" },
54+
version: "v1",
55+
});
56+
});
57+
58+
it("capture (sdk error)", () => {
59+
const spy = vi.spyOn(postEvent, "postEvent");
60+
client.capture("test" as unknown as Error);
61+
expect(spy).toBeCalledWith("3", {
62+
app: { name: "1", phase: "2" },
63+
description: "Cannot parse given Error object",
64+
exception: {
65+
className: "sdk error",
66+
message: "sdk error",
67+
elements: expect.any(Array),
68+
},
69+
runtime: { name: "browser", version: "" },
70+
version: "v1",
71+
});
72+
});
73+
});

‎packages/truffle-browser/src/index.ts

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ErrorStackParser from "error-stack-parser";
2+
import { postEvent } from "./postEvent";
23

34
export interface TruffleClient {
45
capture(message: Error): void;
@@ -10,61 +11,54 @@ export interface TruffleConfig {
1011
apiKey: string;
1112
}
1213

13-
const baseUrl = "https://truffle-api.wafflestudio.com";
1414
const runtime = { name: "browser", version: "" };
1515

1616
export const getTruffleClient = ({
1717
enabled = true,
1818
app,
1919
apiKey,
2020
}: TruffleConfig): TruffleClient => {
21-
const send = (body: unknown) =>
22-
fetch(`${baseUrl}/events`, {
23-
method: "POST",
24-
headers: { "x-api-key": apiKey, "content-type": "application/json" },
25-
body: JSON.stringify(body),
26-
});
27-
2821
return {
2922
capture: (error: Error) => {
3023
try {
3124
if (!enabled) return;
3225

3326
const message = error.message;
3427
const description = window.location.href;
35-
const fallbackString = "__fail__";
3628
const fallbackNumber = 99999;
3729
const elements = ErrorStackParser.parse(error).map((e) => ({
3830
className: "",
39-
methodName: e.functionName ?? fallbackString,
31+
methodName: e.functionName ?? "",
4032
lineNumber: e.lineNumber ?? fallbackNumber,
41-
fileName: e.fileName ?? fallbackString,
33+
fileName: e.fileName ?? "",
4234
isInAppInClude: e.isNative ?? true,
4335
}));
4436

4537
const body = {
46-
version: "v1",
4738
app,
48-
runtime,
4939
description,
5040
exception: { className: error.name, message, elements },
41+
runtime,
42+
version: "v1",
5143
};
5244

53-
send(body);
45+
postEvent(apiKey, body);
5446
} catch (err) {
55-
send(
56-
JSON.stringify({
57-
version: "v1",
58-
app,
59-
runtime,
60-
description: "",
61-
exception: {
62-
className: "sdk error",
63-
message: "sdk error",
64-
elements: [],
65-
},
66-
})
67-
);
47+
const body = {
48+
app,
49+
description:
50+
err && typeof err === "object"
51+
? (err as { message?: string }).message
52+
: "",
53+
exception: {
54+
className: "sdk error",
55+
message: "sdk error",
56+
elements: [],
57+
},
58+
runtime,
59+
version: "v1",
60+
};
61+
postEvent(apiKey, body);
6862
}
6963
},
7064
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const baseUrl = "https://truffle-api.wafflestudio.com";
2+
3+
export const postEvent = (apiKey: string, body: unknown) =>
4+
fetch(`${baseUrl}/events`, {
5+
method: "POST",
6+
headers: { "x-api-key": apiKey, "content-type": "application/json" },
7+
body: JSON.stringify(body),
8+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from "vite";
2+
3+
export default defineConfig({
4+
test: {
5+
environment: "happy-dom",
6+
},
7+
});

‎turbo.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"pipeline": {
55
"build": { "dependsOn": ["^build"], "outputs": ["dist/**"] },
66
"lint": { "outputs": [] },
7-
"test": { "outputs": [] },
7+
"test": { "dependsOn": ["^build"], "outputs": [] },
88
"dev": { "cache": false }
99
}
1010
}

0 commit comments

Comments
 (0)