Skip to content

Commit 025ef3a

Browse files
committed
Add tests using Jest
1 parent d10d359 commit 025ef3a

File tree

4 files changed

+2138
-21
lines changed

4 files changed

+2138
-21
lines changed

jest.config.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
clearMocks: true,
3+
preset: 'ts-jest',
4+
testEnvironment: "jsdom",
5+
}

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,26 @@
77
"dev": "vite",
88
"build": "tsc && vite build",
99
"preview": "vite preview",
10-
"tauri": "tauri"
10+
"tauri": "tauri",
11+
"test": "jest"
1112
},
1213
"dependencies": {
1314
"@tauri-apps/api": "^1.2.0",
1415
"preact": "^10.11.3"
1516
},
1617
"devDependencies": {
17-
"@babel/core": "^7.12.10",
18+
"@babel/core": "^7.20.12",
1819
"@preact/preset-vite": "^2.4.0",
1920
"@tauri-apps/cli": "^1.2.2",
21+
"@types/jest": "^29.4.0",
2022
"@types/node": "^18.7.14",
2123
"autoprefixer": "^10.4.13",
24+
"jest": "^29.4.3",
25+
"jest-environment-jsdom": "^29.4.3",
2226
"postcss": "^8.4.21",
2327
"sass": "^1.58.0",
2428
"tailwindcss": "^3.2.6",
29+
"ts-jest": "^29.0.5",
2530
"typescript": "^4.6.4",
2631
"vite": "^4.0.0"
2732
}

src/utils/pingData.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {
2+
makePingDataSuccess,
3+
makePingDataTimeout,
4+
parsePingData,
5+
} from './pingData'
6+
7+
describe('parsePingData', () => {
8+
it('returns null for boilerplate lines', () => {
9+
[
10+
'PING 1.1.1.1 (1.1.1.1): 56 data bytes',
11+
'',
12+
'--- 1.1.1.1 ping statistics ---',
13+
'3 packets transmitted, 3 packets received, 0.0% packet loss',
14+
'round-trip min/avg/max/stddev = 18.643/26.401/32.371/5.745 ms',
15+
].forEach((line) => {
16+
expect(parsePingData(line)).toBeNull()
17+
})
18+
})
19+
20+
it('parses successful ping lines', () => {
21+
const line = '64 bytes from 1.1.1.1: icmp_seq=0 ttl=55 time=28.190 ms'
22+
expect(parsePingData(line)).toEqual(makePingDataSuccess(28.19))
23+
})
24+
25+
it('parses timeout ping lines', () => {
26+
const line = 'Request timeout for icmp_seq 0'
27+
expect(parsePingData(line)).toEqual(makePingDataTimeout())
28+
})
29+
})

0 commit comments

Comments
 (0)