Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dotenv from 'dotenv'
import { describe, expect, it, vi } from 'vitest'
import type { Config } from '../src/config.js'
import type { Config } from './config.js'

vi.mock('dotenv', () => ({
default: {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dotenv from 'dotenv'
import { describe, expect, it, vi } from 'vitest'
import type { Config } from '../src/config.js'
import type { Config } from './config.js'

vi.mock('dotenv', () => ({
default: {
Expand Down
6 changes: 3 additions & 3 deletions tests/database/index.test.ts → src/database/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it, vi } from 'vitest'
import postgres from '../../src/database/postgres.js'
import postgres from './postgres.js'

vi.mock('../../src/database/postgres.js', () => ({
vi.mock('./postgres.js', () => ({
default: vi.fn(),
}))

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

const actual = await import('../../src/database/index.js')
const actual = await import('./index.js')

const expected = { postgres: mockPostgres }
expect.soft(actual).toMatchObject(expected)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pg from 'pg'
import { describe, expect, it, vi } from 'vitest'
import config from '../../src/config.js'
import config from '../config.js'

vi.mock('pg', () => {
return {
Expand All @@ -9,7 +9,7 @@ vi.mock('pg', () => {
},
}
})
vi.mock('../../src/config', () => ({
vi.mock('../config', () => ({
default: {
database: { connectionString: 'connectionString' },
},
Expand All @@ -22,7 +22,7 @@ describe('postgres', () => {
it('should be tested', async () => {
expect.assertions(2)

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

expect.soft(actual).toStrictEqual(new pg.Pool(mockConfig.database))
expect.soft(mockPg.Pool).toHaveBeenCalledWith(mockConfig.database)
Expand Down
21 changes: 12 additions & 9 deletions tests/index.test.ts → src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import koa from 'koa'
import { describe, expect, it, vi } from 'vitest'
import config from '../src/config.js'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import config from './config.js'
import {
bodyParser,
compress,
helmet,
postGraphile,
} from '../src/middleware/index.js'
import { healthRouter } from '../src/router/index.js'
} from './middleware/index.js'
import { healthRouter } from './router/index.js'

vi.mock('koa', () => ({
default: vi.fn(
Expand All @@ -19,30 +19,33 @@ vi.mock('koa', () => ({
},
),
}))
vi.mock('../src/config.js', () => ({
vi.mock('./config.js', () => ({
default: {
port: 1234,
},
}))
vi.mock('../src/middleware/index.js', () => ({
vi.mock('./middleware/index.js', () => ({
bodyParser: vi.fn().mockName('bodyParser'),
compress: vi.fn().mockName('compress'),
helmet: vi.fn().mockName('helmet'),
postGraphile: vi.fn().mockName('postGraphile'),
}))
vi.mock('../src/router/index.js', () => ({
vi.mock('./router/index.js', () => ({
healthRouter: {
routes: vi.fn().mockName('healthRouter.routes'),
allowedMethods: vi.fn().mockName('healthRouter.allowedMethods'),
},
}))
vi.spyOn(globalThis.console, 'log').mockImplementation(() => {})

describe('index', () => {
beforeEach(() => {
vi.spyOn(globalThis.console, 'log').mockImplementation(() => {})
})

it('should be tested', async () => {
expect.assertions(12)

await import('../src/index.js')
await import('./index.js')

expect.soft(vi.mocked(koa)).toHaveBeenCalledTimes(1)
const mockKoaInstance = vi.mocked(koa).mock.results[0]?.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ describe('bodyparser', () => {
it('should export bodyparser', async () => {
expect.assertions(1)

const { default: actual } = await import(
'../../src/middleware/bodyparser.js'
)
const { default: actual } = await import('./bodyparser.js')

expect(actual).toBe(mockBodyParser())
})
Expand Down
16 changes: 16 additions & 0 deletions src/middleware/compress.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import compress from 'koa-compress'
import { describe, expect, it, vi } from 'vitest'

vi.mock('koa-compress')

describe('compress', () => {
it('should export compress', async () => {
expect.assertions(3)

const { default: actual } = await import('./compress.js')

expect.soft(compress).toHaveBeenCalledOnce()
expect.soft(compress).toHaveBeenCalledWith({ threshold: 0 })
expect.soft(actual).toBe(compress({ threshold: 0 }))
})
})
15 changes: 15 additions & 0 deletions src/middleware/helmet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import helmet from 'koa-helmet'
import { describe, expect, it, vi } from 'vitest'

vi.mock('koa-helmet')

describe('helmet', () => {
it('should export helmet', async () => {
expect.assertions(2)

const { default: actual } = await import('./helmet.js')

expect(helmet.default).toHaveBeenCalledOnce()
expect(actual).toStrictEqual(helmet.default())
})
})
35 changes: 35 additions & 0 deletions src/middleware/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, expect, it, vi } from 'vitest'
import bodyParser from './bodyparser.js'
import compress from './compress.js'
import helmet from './helmet.js'
import postGraphile from './postgraphile.js'

vi.mock('./bodyparser', () => ({
default: vi.fn().mockName('bodyparser'),
}))
vi.mock('./compress', () => ({
default: vi.fn().mockName('compress'),
}))
vi.mock('./helmet', () => ({
default: vi.fn().mockName('helmet'),
}))
vi.mock('./postgraphile', () => ({
default: vi.fn().mockName('postgraphile'),
}))

describe('index', () => {
it('should export middlewares', async () => {
expect.assertions(2)

const actual = await import('./index.js')

const expected = {
bodyParser,
compress,
helmet,
postGraphile,
}
expect.soft(actual).toMatchObject(expected)
expect.soft(Object.keys(actual)).toEqual(Object.keys(expected))
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ describe('postgraphile', () => {
it('should be tested', async () => {
expect.assertions(3)

const { default: actual } = await import(
'../../src/middleware/postgraphile.js'
)
const { default: actual } = await import('./postgraphile.js')

expect.soft(mockPostgraphile).toHaveBeenCalledOnce()
expect
Expand Down
6 changes: 3 additions & 3 deletions tests/router/health.test.ts → src/router/health.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import koa from 'koa'
import supertest from 'supertest'
import { afterAll, describe, expect, it, vi } from 'vitest'
import { postgres } from '../../src/database/index.js'
import healthRouter from '../../src/router/health.js'
import { postgres } from '../database/index.js'
import healthRouter from './health.js'

vi.mock('../../src/database/index.js', () => ({
vi.mock('../database/index.js', () => ({
postgres: {
query: vi.fn(),
},
Expand Down
6 changes: 3 additions & 3 deletions tests/router/index.test.ts → src/router/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it, vi } from 'vitest'
import healthRouter from '../../src/router/health.js'
import healthRouter from './health.js'

vi.mock('../../src/router/health.js', () => ({
vi.mock('./health.js', () => ({
default: vi.fn(),
}))

Expand All @@ -11,7 +11,7 @@ describe('index', () => {
it('should export routers', async () => {
expect.assertions(2)

const actual = await import('../../src/router/index.js')
const actual = await import('./index.js')

const expected = { healthRouter: mockHealthRouter }
expect.soft(actual).toMatchObject(expected)
Expand Down
18 changes: 0 additions & 18 deletions tests/middleware/compress.test.ts

This file was deleted.

17 changes: 0 additions & 17 deletions tests/middleware/helmet.test.ts

This file was deleted.

40 changes: 0 additions & 40 deletions tests/middleware/index.test.ts

This file was deleted.

3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
"include": ["src"],
"exclude": ["src/**/*.test.ts"]
}
8 changes: 1 addition & 7 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,5 @@
"@tsconfig/strictest/tsconfig.json",
"@tsconfig/node22/tsconfig.json"
],
"include": [
"tests/**/*.ts",
"testsInt/**/*.ts",
"testsInt/global-setup.ts",
"vitest.config.ts",
"vitest.int.config.ts"
]
"include": ["testsInt/**/*.ts", "vitest.config.ts", "vitest.int.config.ts"]
}
14 changes: 12 additions & 2 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
include: ['tests/**/*.{test,spec}.?(c|m)[jt]s?(x)'],
include: ['src/**/*.{test,spec}.?(c|m)[jt]s?(x)'],
clearMocks: true,
coverage: {
include: ['src/**'],
enabled: true,
provider: 'v8',
reporter: ['text', 'lcov', 'json-summary'],
include: ['src/**/*.{js,ts}'],
thresholds: {
statements: 100,
},
},
unstubEnvs: true,
unstubGlobals: true,
restoreMocks: true,
},
})