Skip to content

Commit 0bead0d

Browse files
committed
chore: add a test for ignored option and switch to vitest
1 parent 4b77491 commit 0bead0d

File tree

6 files changed

+165
-115
lines changed

6 files changed

+165
-115
lines changed

.github/workflows/ci.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ jobs:
1010
bun-version: latest
1111
- run: bun install
1212
- run: bun test:coverage
13-
- run: bun test:report
1413
- name: Coveralls
1514
uses: coverallsapp/github-action@master
1615
with:
1716
github-token: ${{ secrets.GITHUB_TOKEN }}
18-
path-to-lcov: ./lcov.info
17+
path-to-lcov: ./coverage/lcov.info
1918
build:
2019
runs-on: ubuntu-latest
2120
steps:

bun.lock

+121-81
Large diffs are not rendered by default.

package.json

+6-7
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434
},
3535
"scripts": {
3636
"build": "tsc",
37-
"test": "tsx tests/index.test.ts",
38-
"test:coverage": "c8 tsx --test tests/index.test.ts",
39-
"test:report": "c8 report --reporter=text-lcov > lcov.info",
37+
"test": "vitest run --dir tests",
38+
"test:coverage": "vitest run --dir tests --coverage",
4039
"check": "biome check",
4140
"check:fix": "biome check --write",
4241
"prepare": "husky"
@@ -47,11 +46,11 @@
4746
"@commitlint/config-conventional": "^19.7.1",
4847
"@tinyhttp/app": "^2.5.2",
4948
"@types/node": "^20.17.22",
50-
"c8": "^10.1.3",
51-
"expect": "^29.7.0",
49+
"@vitest/coverage-v8": "^3.0.7",
50+
"@vitest/spy": "^3.0.7",
5251
"husky": "^9.1.7",
5352
"supertest-fetch": "^2.0.0",
54-
"tsx": "^4.19.3",
55-
"typescript": "^5.8.2"
53+
"typescript": "^5.8.2",
54+
"vitest": "^3.0.7"
5655
}
5756
}

src/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ export const logger = (options: LoggerOptions = {}) => {
7373
return (req: Request, res: Response, next?: () => void) => {
7474
res.on('finish', () => {
7575
const args: (string | number)[] = []
76-
7776
if (methods.includes(req.method) && !ignore.some((url) => req.url.startsWith(url))) {
7877
const s = res.statusCode.toString()
7978
let stringToLog = ''

tests/index.test.ts

+26-24
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import * as assert from 'node:assert/strict'
22
import { readFileSync } from 'node:fs'
33
import { access, rm } from 'node:fs/promises'
44
import type { AddressInfo } from 'node:net'
5-
import { describe, it, mock } from 'node:test'
65
import { App } from '@tinyhttp/app'
6+
import * as mock from '@vitest/spy'
77
import { bold, cyan, magenta, red } from 'colorette'
8-
import { expect } from 'expect'
98
import { makeFetch } from 'supertest-fetch'
9+
import { describe, expect, it, vi } from 'vitest'
1010
import { LogLevel, logger } from '../src/index.ts'
1111

1212
async function checkFileExists(file: string) {
@@ -99,35 +99,37 @@ it('should ignore route paths listed in the "ignore" option', async () => {
9999

100100
const logs: string[] = []
101101

102-
const callback = mock.fn((path: string) => {
102+
const callback = vi.fn((path: string) => {
103103
logs.push(path)
104104
})
105105

106-
app.use(
107-
logger({
108-
output: {
109-
color: false,
110-
level: undefined,
111-
callback
112-
},
113-
ignore: ['/ignore']
114-
})
115-
)
106+
app.use(logger({ output: { color: false, level: undefined, callback }, ignore: ['/ignore'] }))
116107

117108
const server = app.listen()
118109

119-
const address = (server.address() as AddressInfo).port
120-
121-
const baseUrl = `http://localhost:${address}`
122-
123-
await fetch(new URL('/ignor', baseUrl))
124-
await fetch(new URL('/ignore', baseUrl))
125-
await fetch(new URL('/ignore/path', baseUrl))
126-
await fetch(new URL('/ignore?query=string', baseUrl))
127-
await fetch(new URL('/path/ignore', baseUrl))
110+
const fetch = makeFetch(server)
128111

129-
expect(callback.mock.callCount()).toEqual(2)
130-
expect(logs).toEqual(['GET 404 Not Found /ignor', 'GET 404 Not Found /path/ignore'])
112+
fetch('/ignor')
113+
.expect(404)
114+
.then(() => {
115+
fetch('/ignore')
116+
.expect(404)
117+
.then(() => {
118+
fetch('/path/ignore')
119+
.expect(404)
120+
.then(() => {
121+
fetch('/ignore?query=string')
122+
.expect(404)
123+
.then(() => {
124+
expect(callback).toHaveBeenCalledTimes(2)
125+
expect(logs).toEqual(['GET 404 Not Found /ignor', 'GET 404 Not Found /path/ignore'])
126+
127+
vi.clearAllMocks()
128+
server.close()
129+
})
130+
})
131+
})
132+
})
131133
})
132134

133135
describe('Log file tests', () => {

vitest.config.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
coverage: {
6+
provider: 'v8',
7+
include: ['src/**/*.ts'],
8+
reporter: ['lcov', 'text']
9+
}
10+
}
11+
})

0 commit comments

Comments
 (0)