Spaces:
Sleeping
Sleeping
Commit ·
13def79
1
Parent(s): bd2e5f8
test: refactor test infra with createClearCjs helper
Browse files- Add a helper to clear the CJS require cache between module re-imports
- Simplify server tests that inject mock dependencies
- README.md +1 -1
- tests/README.md +2 -2
- tests/config/config.test.js +78 -83
- tests/db/migrate.test.js +2 -7
- tests/db/seed.test.js +2 -9
- tests/helpers/coverage.js +18 -1
- tests/server/coverage-printlog.test.js +2 -9
- vitest.config.js +0 -2
README.md
CHANGED
|
@@ -330,7 +330,7 @@ This runs comprehensive queries to inspect row counts, column metadata, relation
|
|
| 330 |
|
| 331 |
## Testing
|
| 332 |
|
| 333 |
-
Uses **vitest** with **V8 native coverage**. **
|
| 334 |
|
| 335 |
```bash
|
| 336 |
npm test # Run all tests once
|
|
|
|
| 330 |
|
| 331 |
## Testing
|
| 332 |
|
| 333 |
+
Uses **vitest** with **V8 native coverage**. **147 tests across 13 test files** cover the full stack — from integration tests (real HTTP server + SQLite) to unit tests for every module.
|
| 334 |
|
| 335 |
```bash
|
| 336 |
npm test # Run all tests once
|
tests/README.md
CHANGED
|
@@ -18,7 +18,7 @@ npm run test:coverage # Run with coverage report
|
|
| 18 |
```
|
| 19 |
tests/
|
| 20 |
config/
|
| 21 |
-
config.test.js # Config defaults and env var branches (
|
| 22 |
load-env.test.js # Load-env file loading chain and error paths (7 tests)
|
| 23 |
db/
|
| 24 |
database.test.js # Database CRUD, pagination, search, sort, SQL injection (10 tests)
|
|
@@ -41,7 +41,7 @@ tests/
|
|
| 41 |
seed-settings-coverage.test.js # Seed-settings.js V8 coverage (2 tests)
|
| 42 |
```
|
| 43 |
|
| 44 |
-
**Total:
|
| 45 |
|
| 46 |
## Test design
|
| 47 |
|
|
|
|
| 18 |
```
|
| 19 |
tests/
|
| 20 |
config/
|
| 21 |
+
config.test.js # Config defaults and env var branches (9 tests)
|
| 22 |
load-env.test.js # Load-env file loading chain and error paths (7 tests)
|
| 23 |
db/
|
| 24 |
database.test.js # Database CRUD, pagination, search, sort, SQL injection (10 tests)
|
|
|
|
| 41 |
seed-settings-coverage.test.js # Seed-settings.js V8 coverage (2 tests)
|
| 42 |
```
|
| 43 |
|
| 44 |
+
**Total: 147 tests across 13 test files.**
|
| 45 |
|
| 46 |
## Test design
|
| 47 |
|
tests/config/config.test.js
CHANGED
|
@@ -1,19 +1,9 @@
|
|
| 1 |
-
import { describe, it, expect
|
| 2 |
-
import fs from 'fs'
|
| 3 |
-
import path from 'path'
|
| 4 |
-
import { mkdtempSync } from 'fs'
|
| 5 |
-
import { tmpdir } from 'os'
|
| 6 |
import { createRequire } from 'module'
|
| 7 |
-
import { save, restore,
|
| 8 |
|
| 9 |
-
const tmpDir = mkdtempSync(path.join(tmpdir(), 'cov-config-'))
|
| 10 |
const _require = createRequire(import.meta.url)
|
| 11 |
-
|
| 12 |
-
for (const key of keys) {
|
| 13 |
-
const resolved = _require.resolve(key)
|
| 14 |
-
if (_require.cache[resolved]) delete _require.cache[resolved]
|
| 15 |
-
}
|
| 16 |
-
}
|
| 17 |
|
| 18 |
function mockLoadEnv() {
|
| 19 |
clearCjs('../../src/config/index.js', '../../src/config/load-env.js')
|
|
@@ -21,18 +11,48 @@ function mockLoadEnv() {
|
|
| 21 |
_require.cache[loadEnvResolved] = { exports: { loadEnv: () => {} } }
|
| 22 |
}
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
expect(mod.port).toBe(3000)
|
| 37 |
expect(mod.dbPath).toContain('storage/data.db')
|
| 38 |
expect(mod.redisOpts.host).toBe('127.0.0.1')
|
|
@@ -40,8 +60,8 @@ describe('config.js', () => {
|
|
| 40 |
restore(s)
|
| 41 |
})
|
| 42 |
|
| 43 |
-
it('covers mock fallback branches when env vars are deleted',
|
| 44 |
-
const s = save('PORT', 'DB_PATH', 'REDIS_URL', 'RATE_LIMIT_ENABLED', 'RATE_LIMIT_MAX', 'RATE_LIMIT_WINDOW_MS', 'REDIS_HOST', 'REDIS_PORT', 'REDIS_DB', 'REDIS_PASSWORD', 'DEBUG_SQL')
|
| 45 |
delete process.env.PORT
|
| 46 |
delete process.env.DB_PATH
|
| 47 |
delete process.env.REDIS_URL
|
|
@@ -53,8 +73,9 @@ describe('config.js', () => {
|
|
| 53 |
delete process.env.REDIS_DB
|
| 54 |
delete process.env.REDIS_PASSWORD
|
| 55 |
delete process.env.DEBUG_SQL
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
expect(mod.port).toBe(3000)
|
| 59 |
expect(mod.rateLimitEnabled).toBe(true)
|
| 60 |
expect(mod.rateLimitMax).toBe(100)
|
|
@@ -64,36 +85,35 @@ describe('config.js', () => {
|
|
| 64 |
expect(mod.redisOpts.host).toBe('127.0.0.1')
|
| 65 |
expect(mod.redisOpts.port).toBe(6379)
|
| 66 |
expect(mod.redisOpts.db).toBe(0)
|
|
|
|
| 67 |
restore(s)
|
| 68 |
})
|
| 69 |
|
| 70 |
-
it('uses REDIS_URL when set',
|
| 71 |
const s = save('REDIS_URL')
|
| 72 |
process.env.REDIS_URL = 'redis://u:p@h:6380/2'
|
| 73 |
-
|
| 74 |
-
const mod =
|
| 75 |
expect(mod.redisOpts.url).toBe('redis://u:p@h:6380/2')
|
| 76 |
restore(s)
|
| 77 |
})
|
| 78 |
|
| 79 |
-
it('uses explicit config values when set',
|
| 80 |
-
const s = save('PORT', 'DB_PATH', 'REDIS_URL', 'REDIS_HOST', 'REDIS_PORT', 'REDIS_DB', 'REDIS_PASSWORD', 'RATE_LIMIT_ENABLED', 'RATE_LIMIT_MAX', 'RATE_LIMIT_WINDOW_MS')
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
vi.resetModules()
|
| 94 |
-
const mod = await import('../../src/config/index.js')
|
| 95 |
expect(mod.port).toBe(4000)
|
| 96 |
-
expect(mod.dbPath).toBe(
|
| 97 |
expect(mod.redisOpts).toEqual({ host: 'redis.local', port: 6380, db: 2, password: 'secret' })
|
| 98 |
expect(mod.rateLimitEnabled).toBe(false)
|
| 99 |
expect(mod.rateLimitMax).toBe(7)
|
|
@@ -102,44 +122,10 @@ describe('config.js', () => {
|
|
| 102 |
restore(s)
|
| 103 |
})
|
| 104 |
|
| 105 |
-
it('covers branches in the real config module via CJS require', () => {
|
| 106 |
-
const s = save('PORT', 'DB_PATH', 'REDIS_URL', 'RATE_LIMIT_ENABLED', 'REDIS_HOST', 'REDIS_PORT', 'REDIS_DB', 'REDIS_PASSWORD', 'DEBUG_SQL', 'RATE_LIMIT_MAX', 'RATE_LIMIT_WINDOW_MS', 'MAX_BODY_SIZE', 'ADMIN_KEY')
|
| 107 |
-
process.env.PORT = '5000'
|
| 108 |
-
process.env.REDIS_URL = 'redis://custom:6379'
|
| 109 |
-
process.env.RATE_LIMIT_ENABLED = 'false'
|
| 110 |
-
process.env.DB_PATH = '/custom/data.db'
|
| 111 |
-
process.env.DEBUG_SQL = 'true'
|
| 112 |
-
process.env.MAX_BODY_SIZE = '2097152'
|
| 113 |
-
process.env.ADMIN_KEY = 'admin-secret-key'
|
| 114 |
-
clearCjs('../../src/config/index.js', '../../src/config/load-env.js')
|
| 115 |
-
const mod = _require('../../src/config/index.js')
|
| 116 |
-
expect(mod.port).toBe(5000)
|
| 117 |
-
expect(mod.redisOpts).toEqual({ url: 'redis://custom:6379' })
|
| 118 |
-
expect(mod.rateLimitEnabled).toBe(false)
|
| 119 |
-
expect(mod.dbDebugSql).toBe(true)
|
| 120 |
-
expect(mod.dbPath).toBe('/custom/data.db')
|
| 121 |
-
expect(mod.maxBodySize).toBe(2097152)
|
| 122 |
-
expect(mod.adminKey).toBe('admin-secret-key')
|
| 123 |
-
restore(s)
|
| 124 |
-
})
|
| 125 |
-
|
| 126 |
-
it('covers PORT default and rate limit enabled branches via CJS', () => {
|
| 127 |
-
const s = save('PORT', 'RATE_LIMIT_ENABLED', 'MAX_BODY_SIZE', 'REDIS_URL', 'DB_PATH', 'REDIS_HOST', 'REDIS_PORT', 'REDIS_DB', 'REDIS_PASSWORD', 'DEBUG_SQL', 'RATE_LIMIT_MAX', 'RATE_LIMIT_WINDOW_MS')
|
| 128 |
-
delete process.env.PORT
|
| 129 |
-
delete process.env.RATE_LIMIT_ENABLED
|
| 130 |
-
delete process.env.MAX_BODY_SIZE
|
| 131 |
-
mockLoadEnv()
|
| 132 |
-
const mod = _require('../../src/config/index.js')
|
| 133 |
-
expect(mod.port).toBe(3000)
|
| 134 |
-
expect(mod.rateLimitEnabled).toBe(true)
|
| 135 |
-
expect(mod.maxBodySize).toBe(1048576)
|
| 136 |
-
restore(s)
|
| 137 |
-
})
|
| 138 |
-
|
| 139 |
it('falls back to default maxBodySize when env var is not a valid integer', () => {
|
| 140 |
const s = save('MAX_BODY_SIZE')
|
| 141 |
process.env.MAX_BODY_SIZE = 'abc'
|
| 142 |
-
|
| 143 |
const mod = _require('../../src/config/index.js')
|
| 144 |
expect(mod.maxBodySize).toBe(1048576)
|
| 145 |
restore(s)
|
|
@@ -148,9 +134,18 @@ describe('config.js', () => {
|
|
| 148 |
it('falls back to default maxBodySize when env var is less than 1', () => {
|
| 149 |
const s = save('MAX_BODY_SIZE')
|
| 150 |
process.env.MAX_BODY_SIZE = '0'
|
| 151 |
-
|
| 152 |
const mod = _require('../../src/config/index.js')
|
| 153 |
expect(mod.maxBodySize).toBe(1048576)
|
| 154 |
restore(s)
|
| 155 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
})
|
|
|
|
| 1 |
+
import { describe, it, expect } from 'vitest'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import { createRequire } from 'module'
|
| 3 |
+
import { save, restore, createClearCjs } from '../helpers/coverage'
|
| 4 |
|
|
|
|
| 5 |
const _require = createRequire(import.meta.url)
|
| 6 |
+
const clearCjs = createClearCjs(import.meta.url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
function mockLoadEnv() {
|
| 9 |
clearCjs('../../src/config/index.js', '../../src/config/load-env.js')
|
|
|
|
| 11 |
_require.cache[loadEnvResolved] = { exports: { loadEnv: () => {} } }
|
| 12 |
}
|
| 13 |
|
| 14 |
+
describe('config.js', () => {
|
| 15 |
+
it('covers branches in the real config module via CJS require', () => {
|
| 16 |
+
const s = save('PORT', 'DB_PATH', 'REDIS_URL', 'RATE_LIMIT_ENABLED', 'REDIS_HOST', 'REDIS_PORT', 'REDIS_DB', 'REDIS_PASSWORD', 'DEBUG_SQL', 'RATE_LIMIT_MAX', 'RATE_LIMIT_WINDOW_MS', 'MAX_BODY_SIZE', 'ADMIN_KEY')
|
| 17 |
+
process.env.PORT = '5000'
|
| 18 |
+
process.env.REDIS_URL = 'redis://custom:6379'
|
| 19 |
+
process.env.RATE_LIMIT_ENABLED = 'false'
|
| 20 |
+
process.env.DB_PATH = '/custom/data.db'
|
| 21 |
+
process.env.DEBUG_SQL = 'true'
|
| 22 |
+
process.env.MAX_BODY_SIZE = '2097152'
|
| 23 |
+
process.env.ADMIN_KEY = 'your-test-secret-admin-key'
|
| 24 |
+
clearCjs('../../src/config/index.js', '../../src/config/load-env.js')
|
| 25 |
+
const mod = _require('../../src/config/index.js')
|
| 26 |
+
expect(mod.port).toBe(5000)
|
| 27 |
+
expect(mod.redisOpts).toEqual({ url: 'redis://custom:6379' })
|
| 28 |
+
expect(mod.rateLimitEnabled).toBe(false)
|
| 29 |
+
expect(mod.dbDebugSql).toBe(true)
|
| 30 |
+
expect(mod.dbPath).toBe('/custom/data.db')
|
| 31 |
+
expect(mod.maxBodySize).toBe(2097152)
|
| 32 |
+
expect(mod.adminKey).toBe('your-test-secret-admin-key')
|
| 33 |
+
expect(mod.defaultPageSize).toBe(10)
|
| 34 |
+
restore(s)
|
| 35 |
+
})
|
| 36 |
|
| 37 |
+
it('covers rate limit enabled default and maxBodySize fallback via CJS', () => {
|
| 38 |
+
const s = save('RATE_LIMIT_ENABLED', 'MAX_BODY_SIZE')
|
| 39 |
+
delete process.env.RATE_LIMIT_ENABLED
|
| 40 |
+
delete process.env.MAX_BODY_SIZE
|
| 41 |
+
mockLoadEnv()
|
| 42 |
+
const mod = _require('../../src/config/index.js')
|
| 43 |
+
expect(mod.rateLimitEnabled).toBe(true)
|
| 44 |
+
expect(mod.maxBodySize).toBe(1048576)
|
| 45 |
+
restore(s)
|
| 46 |
+
})
|
| 47 |
|
| 48 |
+
it('uses defaults when PORT is empty string', () => {
|
| 49 |
+
const s = save('PORT', 'DB_PATH', 'REDIS_URL', 'RATE_LIMIT_ENABLED', 'MAX_BODY_SIZE')
|
| 50 |
+
process.env.PORT = ''
|
| 51 |
+
process.env.DB_PATH = ''
|
| 52 |
+
process.env.REDIS_URL = ''
|
| 53 |
+
process.env.RATE_LIMIT_ENABLED = ''
|
| 54 |
+
mockLoadEnv()
|
| 55 |
+
const mod = _require('../../src/config/index.js')
|
| 56 |
expect(mod.port).toBe(3000)
|
| 57 |
expect(mod.dbPath).toContain('storage/data.db')
|
| 58 |
expect(mod.redisOpts.host).toBe('127.0.0.1')
|
|
|
|
| 60 |
restore(s)
|
| 61 |
})
|
| 62 |
|
| 63 |
+
it('covers mock fallback branches when env vars are deleted', () => {
|
| 64 |
+
const s = save('PORT', 'DB_PATH', 'REDIS_URL', 'RATE_LIMIT_ENABLED', 'RATE_LIMIT_MAX', 'RATE_LIMIT_WINDOW_MS', 'REDIS_HOST', 'REDIS_PORT', 'REDIS_DB', 'REDIS_PASSWORD', 'DEBUG_SQL', 'MAX_BODY_SIZE')
|
| 65 |
delete process.env.PORT
|
| 66 |
delete process.env.DB_PATH
|
| 67 |
delete process.env.REDIS_URL
|
|
|
|
| 73 |
delete process.env.REDIS_DB
|
| 74 |
delete process.env.REDIS_PASSWORD
|
| 75 |
delete process.env.DEBUG_SQL
|
| 76 |
+
delete process.env.MAX_BODY_SIZE
|
| 77 |
+
mockLoadEnv()
|
| 78 |
+
const mod = _require('../../src/config/index.js')
|
| 79 |
expect(mod.port).toBe(3000)
|
| 80 |
expect(mod.rateLimitEnabled).toBe(true)
|
| 81 |
expect(mod.rateLimitMax).toBe(100)
|
|
|
|
| 85 |
expect(mod.redisOpts.host).toBe('127.0.0.1')
|
| 86 |
expect(mod.redisOpts.port).toBe(6379)
|
| 87 |
expect(mod.redisOpts.db).toBe(0)
|
| 88 |
+
expect(mod.maxBodySize).toBe(1048576)
|
| 89 |
restore(s)
|
| 90 |
})
|
| 91 |
|
| 92 |
+
it('uses REDIS_URL when set', () => {
|
| 93 |
const s = save('REDIS_URL')
|
| 94 |
process.env.REDIS_URL = 'redis://u:p@h:6380/2'
|
| 95 |
+
mockLoadEnv()
|
| 96 |
+
const mod = _require('../../src/config/index.js')
|
| 97 |
expect(mod.redisOpts.url).toBe('redis://u:p@h:6380/2')
|
| 98 |
restore(s)
|
| 99 |
})
|
| 100 |
|
| 101 |
+
it('uses explicit config values when set', () => {
|
| 102 |
+
const s = save('PORT', 'DB_PATH', 'REDIS_URL', 'REDIS_HOST', 'REDIS_PORT', 'REDIS_DB', 'REDIS_PASSWORD', 'RATE_LIMIT_ENABLED', 'RATE_LIMIT_MAX', 'RATE_LIMIT_WINDOW_MS', 'MAX_BODY_SIZE')
|
| 103 |
+
process.env.PORT = '4000'
|
| 104 |
+
process.env.DB_PATH = '/tmp/config.db'
|
| 105 |
+
process.env.REDIS_URL = ''
|
| 106 |
+
process.env.REDIS_HOST = 'redis.local'
|
| 107 |
+
process.env.REDIS_PORT = '6380'
|
| 108 |
+
process.env.REDIS_DB = '2'
|
| 109 |
+
process.env.REDIS_PASSWORD = 'secret'
|
| 110 |
+
process.env.RATE_LIMIT_ENABLED = 'false'
|
| 111 |
+
process.env.RATE_LIMIT_MAX = '7'
|
| 112 |
+
process.env.RATE_LIMIT_WINDOW_MS = '15000'
|
| 113 |
+
mockLoadEnv()
|
| 114 |
+
const mod = _require('../../src/config/index.js')
|
|
|
|
|
|
|
| 115 |
expect(mod.port).toBe(4000)
|
| 116 |
+
expect(mod.dbPath).toBe('/tmp/config.db')
|
| 117 |
expect(mod.redisOpts).toEqual({ host: 'redis.local', port: 6380, db: 2, password: 'secret' })
|
| 118 |
expect(mod.rateLimitEnabled).toBe(false)
|
| 119 |
expect(mod.rateLimitMax).toBe(7)
|
|
|
|
| 122 |
restore(s)
|
| 123 |
})
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
it('falls back to default maxBodySize when env var is not a valid integer', () => {
|
| 126 |
const s = save('MAX_BODY_SIZE')
|
| 127 |
process.env.MAX_BODY_SIZE = 'abc'
|
| 128 |
+
mockLoadEnv()
|
| 129 |
const mod = _require('../../src/config/index.js')
|
| 130 |
expect(mod.maxBodySize).toBe(1048576)
|
| 131 |
restore(s)
|
|
|
|
| 134 |
it('falls back to default maxBodySize when env var is less than 1', () => {
|
| 135 |
const s = save('MAX_BODY_SIZE')
|
| 136 |
process.env.MAX_BODY_SIZE = '0'
|
| 137 |
+
mockLoadEnv()
|
| 138 |
const mod = _require('../../src/config/index.js')
|
| 139 |
expect(mod.maxBodySize).toBe(1048576)
|
| 140 |
restore(s)
|
| 141 |
})
|
| 142 |
+
|
| 143 |
+
it('defaults adminKey to empty string when ADMIN_KEY is not set', () => {
|
| 144 |
+
const s = save('ADMIN_KEY')
|
| 145 |
+
process.env.ADMIN_KEY = ''
|
| 146 |
+
mockLoadEnv()
|
| 147 |
+
const mod = _require('../../src/config/index.js')
|
| 148 |
+
expect(mod.adminKey).toBe('')
|
| 149 |
+
restore(s)
|
| 150 |
+
})
|
| 151 |
})
|
tests/db/migrate.test.js
CHANGED
|
@@ -4,16 +4,11 @@ import path from 'path'
|
|
| 4 |
import { mkdtempSync } from 'fs'
|
| 5 |
import { tmpdir } from 'os'
|
| 6 |
import { createRequire } from 'module'
|
| 7 |
-
import { save, restore, configMockFactory } from '../helpers/coverage'
|
| 8 |
|
| 9 |
const tmpDir = mkdtempSync(path.join(tmpdir(), 'cov-migrate-'))
|
| 10 |
const _require = createRequire(import.meta.url)
|
| 11 |
-
|
| 12 |
-
for (const key of keys) {
|
| 13 |
-
const resolved = _require.resolve(key)
|
| 14 |
-
if (_require.cache[resolved]) delete _require.cache[resolved]
|
| 15 |
-
}
|
| 16 |
-
}
|
| 17 |
|
| 18 |
afterAll(() => {
|
| 19 |
try { fs.rmSync(tmpDir, { recursive: true, force: true }) } catch {}
|
|
|
|
| 4 |
import { mkdtempSync } from 'fs'
|
| 5 |
import { tmpdir } from 'os'
|
| 6 |
import { createRequire } from 'module'
|
| 7 |
+
import { save, restore, configMockFactory, createClearCjs } from '../helpers/coverage'
|
| 8 |
|
| 9 |
const tmpDir = mkdtempSync(path.join(tmpdir(), 'cov-migrate-'))
|
| 10 |
const _require = createRequire(import.meta.url)
|
| 11 |
+
const clearCjs = createClearCjs(import.meta.url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
afterAll(() => {
|
| 14 |
try { fs.rmSync(tmpDir, { recursive: true, force: true }) } catch {}
|
tests/db/seed.test.js
CHANGED
|
@@ -5,17 +5,10 @@ import { mkdtempSync } from 'fs'
|
|
| 5 |
import { tmpdir } from 'os'
|
| 6 |
import { EventEmitter } from 'events'
|
| 7 |
import { Buffer } from 'buffer'
|
| 8 |
-
import {
|
| 9 |
-
import { save, restore, configMockFactory } from '../helpers/coverage'
|
| 10 |
|
| 11 |
const tmpDir = mkdtempSync(path.join(tmpdir(), 'cov-seed-'))
|
| 12 |
-
const
|
| 13 |
-
function clearCjs(...keys) {
|
| 14 |
-
for (const key of keys) {
|
| 15 |
-
const resolved = _require.resolve(key)
|
| 16 |
-
if (_require.cache[resolved]) delete _require.cache[resolved]
|
| 17 |
-
}
|
| 18 |
-
}
|
| 19 |
|
| 20 |
afterAll(() => {
|
| 21 |
try { fs.rmSync(tmpDir, { recursive: true, force: true }) } catch {}
|
|
|
|
| 5 |
import { tmpdir } from 'os'
|
| 6 |
import { EventEmitter } from 'events'
|
| 7 |
import { Buffer } from 'buffer'
|
| 8 |
+
import { save, restore, configMockFactory, createClearCjs } from '../helpers/coverage'
|
|
|
|
| 9 |
|
| 10 |
const tmpDir = mkdtempSync(path.join(tmpdir(), 'cov-seed-'))
|
| 11 |
+
const clearCjs = createClearCjs(import.meta.url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
afterAll(() => {
|
| 14 |
try { fs.rmSync(tmpDir, { recursive: true, force: true }) } catch {}
|
tests/helpers/coverage.js
CHANGED
|
@@ -1,4 +1,15 @@
|
|
| 1 |
import { vi } from 'vitest'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
export function save(...keys) {
|
| 4 |
const saved = {}
|
|
@@ -18,7 +29,7 @@ export function setEnv(overrides) {
|
|
| 18 |
}
|
| 19 |
|
| 20 |
export function configMockFactory() {
|
| 21 |
-
const METHODS = ['port', 'dbPath', 'dbDebugSql', 'redisOpts', 'rateLimitEnabled', 'rateLimitMax', 'rateLimitWindowMs', 'rateLimitWindowSec']
|
| 22 |
const cfg = {
|
| 23 |
getPort: () => parseInt(process.env.PORT || '3000', 10),
|
| 24 |
getDbPath: () => process.env.DB_PATH || process.cwd() + '/storage/data.db',
|
|
@@ -36,6 +47,12 @@ export function configMockFactory() {
|
|
| 36 |
getRateLimitMax: () => parseInt(process.env.RATE_LIMIT_MAX || '100', 10),
|
| 37 |
getRateLimitWindowMs: () => parseInt(process.env.RATE_LIMIT_WINDOW_MS || '60000', 10),
|
| 38 |
getRateLimitWindowSec: () => Math.ceil(parseInt(process.env.RATE_LIMIT_WINDOW_MS || '60000', 10) / 1000),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
const getMethod = (prop) => cfg['get' + prop[0].toUpperCase() + prop.slice(1)]
|
| 41 |
return new Proxy(cfg, {
|
|
|
|
| 1 |
import { vi } from 'vitest'
|
| 2 |
+
import { createRequire } from 'module'
|
| 3 |
+
|
| 4 |
+
export function createClearCjs(importMetaUrl) {
|
| 5 |
+
const _require = createRequire(importMetaUrl)
|
| 6 |
+
return (...keys) => {
|
| 7 |
+
for (const key of keys) {
|
| 8 |
+
const resolved = _require.resolve(key)
|
| 9 |
+
if (_require.cache[resolved]) delete _require.cache[resolved]
|
| 10 |
+
}
|
| 11 |
+
}
|
| 12 |
+
}
|
| 13 |
|
| 14 |
export function save(...keys) {
|
| 15 |
const saved = {}
|
|
|
|
| 29 |
}
|
| 30 |
|
| 31 |
export function configMockFactory() {
|
| 32 |
+
const METHODS = ['port', 'dbPath', 'dbDebugSql', 'redisOpts', 'rateLimitEnabled', 'rateLimitMax', 'rateLimitWindowMs', 'rateLimitWindowSec', 'adminKey', 'maxBodySize', 'defaultPageSize']
|
| 33 |
const cfg = {
|
| 34 |
getPort: () => parseInt(process.env.PORT || '3000', 10),
|
| 35 |
getDbPath: () => process.env.DB_PATH || process.cwd() + '/storage/data.db',
|
|
|
|
| 47 |
getRateLimitMax: () => parseInt(process.env.RATE_LIMIT_MAX || '100', 10),
|
| 48 |
getRateLimitWindowMs: () => parseInt(process.env.RATE_LIMIT_WINDOW_MS || '60000', 10),
|
| 49 |
getRateLimitWindowSec: () => Math.ceil(parseInt(process.env.RATE_LIMIT_WINDOW_MS || '60000', 10) / 1000),
|
| 50 |
+
getAdminKey: () => process.env.ADMIN_KEY || '',
|
| 51 |
+
getMaxBodySize: () => {
|
| 52 |
+
const raw = parseInt(process.env.MAX_BODY_SIZE || '1048576', 10)
|
| 53 |
+
return isNaN(raw) || raw < 1 ? 1048576 : raw
|
| 54 |
+
},
|
| 55 |
+
getDefaultPageSize: () => parseInt(process.env.DEFAULT_PAGE_SIZE || '10', 10),
|
| 56 |
}
|
| 57 |
const getMethod = (prop) => cfg['get' + prop[0].toUpperCase() + prop.slice(1)]
|
| 58 |
return new Proxy(cfg, {
|
tests/server/coverage-printlog.test.js
CHANGED
|
@@ -1,14 +1,7 @@
|
|
| 1 |
-
import { createRequire } from 'module'
|
| 2 |
import { describe, it, expect, vi } from 'vitest'
|
| 3 |
-
import { save, restore } from '../helpers/coverage'
|
| 4 |
|
| 5 |
-
const
|
| 6 |
-
function clearCjs(...keys) {
|
| 7 |
-
for (const key of keys) {
|
| 8 |
-
const resolved = _require.resolve(key)
|
| 9 |
-
if (_require.cache[resolved]) delete _require.cache[resolved]
|
| 10 |
-
}
|
| 11 |
-
}
|
| 12 |
|
| 13 |
function mkReq(url, method, rawBody) {
|
| 14 |
const req = {
|
|
|
|
|
|
|
| 1 |
import { describe, it, expect, vi } from 'vitest'
|
| 2 |
+
import { save, restore, createClearCjs } from '../helpers/coverage'
|
| 3 |
|
| 4 |
+
const clearCjs = createClearCjs(import.meta.url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
function mkReq(url, method, rawBody) {
|
| 7 |
const req = {
|
vitest.config.js
CHANGED
|
@@ -5,8 +5,6 @@ process.env.PORT = '3199';
|
|
| 5 |
|
| 6 |
export default defineConfig({
|
| 7 |
test: {
|
| 8 |
-
include: ['tests/**/*.test.js', 'tests/**/*.spec.js'],
|
| 9 |
-
exclude: ['tests/helpers/**'],
|
| 10 |
globals: true,
|
| 11 |
testTimeout: 15000,
|
| 12 |
hookTimeout: 15000,
|
|
|
|
| 5 |
|
| 6 |
export default defineConfig({
|
| 7 |
test: {
|
|
|
|
|
|
|
| 8 |
globals: true,
|
| 9 |
testTimeout: 15000,
|
| 10 |
hookTimeout: 15000,
|