Skip to content

Commit d9eefc0

Browse files
authored
fix: merge src and tests (#283)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Tests** * Enforced 100% statement coverage and added coverage reporters. * Enabled automatic mock cleanup and restoration for more reliable tests. * Consolidated and relocated many tests to a narrower discovery scope; added new middleware tests and removed legacy duplicates. * **Chores** * Narrowed TypeScript test compilation to exclude runtime test files and streamlined test-related config for faster, more isolated runs. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent fb81703 commit d9eefc0

19 files changed

+109
-114
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dotenv from 'dotenv'
22
import { describe, expect, it, vi } from 'vitest'
3-
import type { Config } from '../src/config.js'
3+
import type { Config } from './config.js'
44

55
vi.mock('dotenv', () => ({
66
default: {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dotenv from 'dotenv'
22
import { describe, expect, it, vi } from 'vitest'
3-
import type { Config } from '../src/config.js'
3+
import type { Config } from './config.js'
44

55
vi.mock('dotenv', () => ({
66
default: {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, it, vi } from 'vitest'
2-
import postgres from '../../src/database/postgres.js'
2+
import postgres from './postgres.js'
33

4-
vi.mock('../../src/database/postgres.js', () => ({
4+
vi.mock('./postgres.js', () => ({
55
default: vi.fn(),
66
}))
77

@@ -11,7 +11,7 @@ describe('index', () => {
1111
it('should export databases', async () => {
1212
expect.assertions(2)
1313

14-
const actual = await import('../../src/database/index.js')
14+
const actual = await import('./index.js')
1515

1616
const expected = { postgres: mockPostgres }
1717
expect.soft(actual).toMatchObject(expected)
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pg from 'pg'
22
import { describe, expect, it, vi } from 'vitest'
3-
import config from '../../src/config.js'
3+
import config from '../config.js'
44

55
vi.mock('pg', () => {
66
return {
@@ -9,7 +9,7 @@ vi.mock('pg', () => {
99
},
1010
}
1111
})
12-
vi.mock('../../src/config', () => ({
12+
vi.mock('../config', () => ({
1313
default: {
1414
database: { connectionString: 'connectionString' },
1515
},
@@ -22,7 +22,7 @@ describe('postgres', () => {
2222
it('should be tested', async () => {
2323
expect.assertions(2)
2424

25-
const { default: actual } = await import('../../src/database/postgres.js')
25+
const { default: actual } = await import('./postgres.js')
2626

2727
expect.soft(actual).toStrictEqual(new pg.Pool(mockConfig.database))
2828
expect.soft(mockPg.Pool).toHaveBeenCalledWith(mockConfig.database)
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import koa from 'koa'
2-
import { describe, expect, it, vi } from 'vitest'
3-
import config from '../src/config.js'
2+
import { beforeEach, describe, expect, it, vi } from 'vitest'
3+
import config from './config.js'
44
import {
55
bodyParser,
66
compress,
77
helmet,
88
postGraphile,
9-
} from '../src/middleware/index.js'
10-
import { healthRouter } from '../src/router/index.js'
9+
} from './middleware/index.js'
10+
import { healthRouter } from './router/index.js'
1111

1212
vi.mock('koa', () => ({
1313
default: vi.fn(
@@ -19,30 +19,33 @@ vi.mock('koa', () => ({
1919
},
2020
),
2121
}))
22-
vi.mock('../src/config.js', () => ({
22+
vi.mock('./config.js', () => ({
2323
default: {
2424
port: 1234,
2525
},
2626
}))
27-
vi.mock('../src/middleware/index.js', () => ({
27+
vi.mock('./middleware/index.js', () => ({
2828
bodyParser: vi.fn().mockName('bodyParser'),
2929
compress: vi.fn().mockName('compress'),
3030
helmet: vi.fn().mockName('helmet'),
3131
postGraphile: vi.fn().mockName('postGraphile'),
3232
}))
33-
vi.mock('../src/router/index.js', () => ({
33+
vi.mock('./router/index.js', () => ({
3434
healthRouter: {
3535
routes: vi.fn().mockName('healthRouter.routes'),
3636
allowedMethods: vi.fn().mockName('healthRouter.allowedMethods'),
3737
},
3838
}))
39-
vi.spyOn(globalThis.console, 'log').mockImplementation(() => {})
4039

4140
describe('index', () => {
41+
beforeEach(() => {
42+
vi.spyOn(globalThis.console, 'log').mockImplementation(() => {})
43+
})
44+
4245
it('should be tested', async () => {
4346
expect.assertions(12)
4447

45-
await import('../src/index.js')
48+
await import('./index.js')
4649

4750
expect.soft(vi.mocked(koa)).toHaveBeenCalledTimes(1)
4851
const mockKoaInstance = vi.mocked(koa).mock.results[0]?.value
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ describe('bodyparser', () => {
88
it('should export bodyparser', async () => {
99
expect.assertions(1)
1010

11-
const { default: actual } = await import(
12-
'../../src/middleware/bodyparser.js'
13-
)
11+
const { default: actual } = await import('./bodyparser.js')
1412

1513
expect(actual).toBe(mockBodyParser())
1614
})

src/middleware/compress.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import compress from 'koa-compress'
2+
import { describe, expect, it, vi } from 'vitest'
3+
4+
vi.mock('koa-compress')
5+
6+
describe('compress', () => {
7+
it('should export compress', async () => {
8+
expect.assertions(3)
9+
10+
const { default: actual } = await import('./compress.js')
11+
12+
expect.soft(compress).toHaveBeenCalledOnce()
13+
expect.soft(compress).toHaveBeenCalledWith({ threshold: 0 })
14+
expect.soft(actual).toBe(compress({ threshold: 0 }))
15+
})
16+
})

src/middleware/helmet.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import helmet from 'koa-helmet'
2+
import { describe, expect, it, vi } from 'vitest'
3+
4+
vi.mock('koa-helmet')
5+
6+
describe('helmet', () => {
7+
it('should export helmet', async () => {
8+
expect.assertions(2)
9+
10+
const { default: actual } = await import('./helmet.js')
11+
12+
expect(helmet.default).toHaveBeenCalledOnce()
13+
expect(actual).toStrictEqual(helmet.default())
14+
})
15+
})

src/middleware/index.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { describe, expect, it, vi } from 'vitest'
2+
import bodyParser from './bodyparser.js'
3+
import compress from './compress.js'
4+
import helmet from './helmet.js'
5+
import postGraphile from './postgraphile.js'
6+
7+
vi.mock('./bodyparser', () => ({
8+
default: vi.fn().mockName('bodyparser'),
9+
}))
10+
vi.mock('./compress', () => ({
11+
default: vi.fn().mockName('compress'),
12+
}))
13+
vi.mock('./helmet', () => ({
14+
default: vi.fn().mockName('helmet'),
15+
}))
16+
vi.mock('./postgraphile', () => ({
17+
default: vi.fn().mockName('postgraphile'),
18+
}))
19+
20+
describe('index', () => {
21+
it('should export middlewares', async () => {
22+
expect.assertions(2)
23+
24+
const actual = await import('./index.js')
25+
26+
const expected = {
27+
bodyParser,
28+
compress,
29+
helmet,
30+
postGraphile,
31+
}
32+
expect.soft(actual).toMatchObject(expected)
33+
expect.soft(Object.keys(actual)).toEqual(Object.keys(expected))
34+
})
35+
})

0 commit comments

Comments
 (0)