diff --git a/tests/config.defaults.test.ts b/src/config.defaults.test.ts similarity index 95% rename from tests/config.defaults.test.ts rename to src/config.defaults.test.ts index d93f1e9..2816478 100644 --- a/tests/config.defaults.test.ts +++ b/src/config.defaults.test.ts @@ -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: { diff --git a/tests/config.error.test.ts b/src/config.error.test.ts similarity index 100% rename from tests/config.error.test.ts rename to src/config.error.test.ts diff --git a/tests/config.overwrite.test.ts b/src/config.overwrite.test.ts similarity index 96% rename from tests/config.overwrite.test.ts rename to src/config.overwrite.test.ts index 1ca6f70..7e51697 100644 --- a/tests/config.overwrite.test.ts +++ b/src/config.overwrite.test.ts @@ -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: { diff --git a/tests/database/index.test.ts b/src/database/index.test.ts similarity index 69% rename from tests/database/index.test.ts rename to src/database/index.test.ts index ad92b73..695b342 100644 --- a/tests/database/index.test.ts +++ b/src/database/index.test.ts @@ -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(), })) @@ -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) diff --git a/tests/database/postgres.test.ts b/src/database/postgres.test.ts similarity index 77% rename from tests/database/postgres.test.ts rename to src/database/postgres.test.ts index 409b4c1..c30f7fb 100644 --- a/tests/database/postgres.test.ts +++ b/src/database/postgres.test.ts @@ -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 { @@ -9,7 +9,7 @@ vi.mock('pg', () => { }, } }) -vi.mock('../../src/config', () => ({ +vi.mock('../config', () => ({ default: { database: { connectionString: 'connectionString' }, }, @@ -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) diff --git a/tests/index.test.ts b/src/index.test.ts similarity index 81% rename from tests/index.test.ts rename to src/index.test.ts index d6d4530..2bec590 100644 --- a/tests/index.test.ts +++ b/src/index.test.ts @@ -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( @@ -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 diff --git a/tests/middleware/bodyparser.test.ts b/src/middleware/bodyparser.test.ts similarity index 76% rename from tests/middleware/bodyparser.test.ts rename to src/middleware/bodyparser.test.ts index 75672e6..5f14915 100644 --- a/tests/middleware/bodyparser.test.ts +++ b/src/middleware/bodyparser.test.ts @@ -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()) }) diff --git a/src/middleware/compress.test.ts b/src/middleware/compress.test.ts new file mode 100644 index 0000000..d82707f --- /dev/null +++ b/src/middleware/compress.test.ts @@ -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 })) + }) +}) diff --git a/src/middleware/helmet.test.ts b/src/middleware/helmet.test.ts new file mode 100644 index 0000000..76f514e --- /dev/null +++ b/src/middleware/helmet.test.ts @@ -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()) + }) +}) diff --git a/src/middleware/index.test.ts b/src/middleware/index.test.ts new file mode 100644 index 0000000..c4ec9c2 --- /dev/null +++ b/src/middleware/index.test.ts @@ -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)) + }) +}) diff --git a/tests/middleware/postgraphile.test.ts b/src/middleware/postgraphile.test.ts similarity index 93% rename from tests/middleware/postgraphile.test.ts rename to src/middleware/postgraphile.test.ts index 2322357..fa3c0a8 100644 --- a/tests/middleware/postgraphile.test.ts +++ b/src/middleware/postgraphile.test.ts @@ -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 diff --git a/tests/router/health.test.ts b/src/router/health.test.ts similarity index 92% rename from tests/router/health.test.ts rename to src/router/health.test.ts index 09a4c52..f27d33b 100644 --- a/tests/router/health.test.ts +++ b/src/router/health.test.ts @@ -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(), }, diff --git a/tests/router/index.test.ts b/src/router/index.test.ts similarity index 71% rename from tests/router/index.test.ts rename to src/router/index.test.ts index c07f979..a644e94 100644 --- a/tests/router/index.test.ts +++ b/src/router/index.test.ts @@ -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(), })) @@ -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) diff --git a/tests/middleware/compress.test.ts b/tests/middleware/compress.test.ts deleted file mode 100644 index 6e75f35..0000000 --- a/tests/middleware/compress.test.ts +++ /dev/null @@ -1,18 +0,0 @@ -import compress from 'koa-compress' -import { describe, expect, it, vi } from 'vitest' - -vi.mock('koa-compress') - -const mockCompress = vi.mocked(compress) - -describe('compress', () => { - it('should export compress', async () => { - expect.assertions(3) - - const { default: actual } = await import('../../src/middleware/compress.js') - - expect.soft(mockCompress).toHaveBeenCalledOnce() - expect.soft(mockCompress).toHaveBeenCalledWith({ threshold: 0 }) - expect.soft(actual).toBe(mockCompress({ threshold: 0 })) - }) -}) diff --git a/tests/middleware/helmet.test.ts b/tests/middleware/helmet.test.ts deleted file mode 100644 index 468f29c..0000000 --- a/tests/middleware/helmet.test.ts +++ /dev/null @@ -1,17 +0,0 @@ -import helmet from 'koa-helmet' -import { describe, expect, it, vi } from 'vitest' - -vi.mock('koa-helmet') - -const mockHelmet = vi.mocked(helmet.default) - -describe('helmet', () => { - it('should export helmet', async () => { - expect.assertions(2) - - const { default: actual } = await import('../../src/middleware/helmet.js') - - expect(mockHelmet).toHaveBeenCalledOnce() - expect(actual).toStrictEqual(mockHelmet()) - }) -}) diff --git a/tests/middleware/index.test.ts b/tests/middleware/index.test.ts deleted file mode 100644 index 56a2661..0000000 --- a/tests/middleware/index.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { describe, expect, it, vi } from 'vitest' -import bodyParser from '../../src/middleware/bodyparser.js' -import compress from '../../src/middleware/compress.js' -import helmet from '../../src/middleware/helmet.js' -import postGraphile from '../../src/middleware/postgraphile.js' - -vi.mock('../../src/middleware/bodyparser', () => ({ - default: vi.fn().mockName('bodyparser'), -})) -vi.mock('../../src/middleware/compress', () => ({ - default: vi.fn().mockName('compress'), -})) -vi.mock('../../src/middleware/helmet', () => ({ - default: vi.fn().mockName('helmet'), -})) -vi.mock('../../src/middleware/postgraphile', () => ({ - default: vi.fn().mockName('postgraphile'), -})) - -const mockBodyParser = vi.mocked(bodyParser) -const mockCompress = vi.mocked(compress) -const mockHelmet = vi.mocked(helmet) -const mockPostGraphile = vi.mocked(postGraphile) - -describe('index', () => { - it('should export middlewares', async () => { - expect.assertions(2) - - const actual = await import('../../src/middleware/index.js') - - const expected = { - bodyParser: mockBodyParser, - compress: mockCompress, - helmet: mockHelmet, - postGraphile: mockPostGraphile, - } - expect.soft(actual).toMatchObject(expected) - expect.soft(Object.keys(actual)).toEqual(Object.keys(expected)) - }) -}) diff --git a/tsconfig.json b/tsconfig.json index c954e52..037604b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,5 +7,6 @@ "rootDir": "src", "outDir": "dist" }, - "include": ["src"] + "include": ["src"], + "exclude": ["src/**/*.test.ts"] } diff --git a/tsconfig.test.json b/tsconfig.test.json index 6e68b75..1eed8ce 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -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"] } diff --git a/vitest.config.ts b/vitest.config.ts index 97ec416..5986f1d 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -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, }, })