Skip to content

Commit 24b49dd

Browse files
Vitest updates (#4039)
* chore: upgrade vitest to v2 * tests: add unit tests for a function as example * workflow: add unit test workflow * chore: upgrade vitest to v2 * Potential fix for code scanning alert no. 31: Workflow does not contain permissions Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * ci: update CI node version to 22.16 * ci: trigger playground tests on development branch pushes * ci: remove push trigger for playground unit tests on development branches * eslint: format setup script --------- Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent d9e3124 commit 24b49dd

4 files changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code using yarn build:app run unit tests using yarn test:unit.
2+
3+
name: Playground Unit tests
4+
permissions:
5+
contents: read
6+
7+
on:
8+
pull_request:
9+
branches:
10+
- development
11+
paths:
12+
- "packages/playground/**"
13+
14+
jobs:
15+
unit-tests:
16+
runs-on: ubuntu-latest
17+
env:
18+
NODE_OPTIONS: "--max-old-space-size=8192"
19+
20+
strategy:
21+
matrix:
22+
node-version: [22.16]
23+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
24+
25+
steps:
26+
- uses: actions/checkout@v4
27+
28+
- name: Setup Node.js ${{ matrix.node-version }}
29+
uses: actions/setup-node@v4
30+
with:
31+
node-version: ${{ matrix.node-version }}
32+
cache: "yarn"
33+
cache-dependency-path: "**/yarn.lock"
34+
35+
- name: Install dependencies
36+
run: |
37+
yarn install
38+
- name: Build
39+
run: |
40+
lerna run build --no-private
41+
- name: Run unit tests
42+
run: yarn workspace @threefold/playground test:unit

packages/playground/tests/setup.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Vitest setup file - runs before all tests
2+
// Set up mock window.env that matches the development environment
3+
// @ts-ignore - Add window object for test environment
4+
global.window = global.window || {};
5+
// @ts-ignore - Add env object to window
6+
global.window.env = {
7+
// set the needed env variables for the tests
8+
};
9+
10+
console.log("Test environment initialized with mock window.env");
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { extractDomainIP } from "../../src/utils/gateway";
4+
5+
describe("extractDomainIP", () => {
6+
it("should extract the domain from a URL with a protocol", () => {
7+
const domain = extractDomainIP("https://example.com:8080");
8+
expect(domain).toBe("example.com");
9+
});
10+
11+
it("should extract the IPv6 address from a URL", () => {
12+
const ipv6 = extractDomainIP("https://[::1]:8080");
13+
expect(ipv6).toBe("::1");
14+
});
15+
16+
it("should throw an error when there is no domain or IP address", () => {
17+
expect(() => extractDomainIP("http://:8080")).toThrow(
18+
'Invalid input "http://:8080": No domain or IP address found.',
19+
);
20+
});
21+
22+
it("should throw an error for invalid IPv6 format", () => {
23+
expect(() => extractDomainIP("https://[]:8080")).toThrow(
24+
'Invalid input "https://[]:8080": Invalid IPv6 address format.',
25+
);
26+
});
27+
28+
it("should extract the domain from a URL without a port", () => {
29+
const domain = extractDomainIP("https://example.com");
30+
expect(domain).toBe("example.com");
31+
});
32+
33+
it("should extract the IPv4 address from a URL", () => {
34+
const ipv4 = extractDomainIP("http://192.168.0.1:3000");
35+
expect(ipv4).toBe("192.168.0.1");
36+
});
37+
38+
it("should handle plain domain strings without protocols", () => {
39+
const domain = extractDomainIP("example.com");
40+
expect(domain).toBe("example.com");
41+
});
42+
});

packages/playground/vitest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default mergeConfig(
1010
environment: "jsdom",
1111
exclude: [...configDefaults.exclude, "e2e/*"],
1212
root: fileURLToPath(new URL("./", import.meta.url)),
13+
setupFiles: ["./tests/setup.ts"],
1314
},
1415
}),
1516
);

0 commit comments

Comments
 (0)