forked from powerhouse-inc/switchboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Improve test coverage (powerhouse-inc#25)
- Loading branch information
1 parent
125d8b1
commit 7cedbca
Showing
16 changed files
with
189 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
### Testing structure | ||
|
||
The test suite is designed to ensure that some of the modules are fully covered by tests. | ||
Therefore, there're 2 runs of tests: one validates that overall code coverage is on appropriate level, another validates that the code coverage for | ||
a subset of modules is maximal. | ||
|
||
The coverage report will therefore contain two tables. | ||
|
||
See corresponding configs in `./vitest.config.ts` and `./vitest.full-coverage.config.ts` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const getJwtSecret = (): string => { | ||
if (!process.env.JWT_SECRET) { | ||
if (process.env.NODE_ENV === 'production') { | ||
throw new Error('JWT_SECRET is not defined'); | ||
} | ||
} | ||
return process.env.JWT_SECRET || 'dev'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import dotenv from 'dotenv'; | ||
import { getJwtSecret } from './getters'; | ||
|
||
dotenv.config(); | ||
|
||
export const JWT_SECRET = getJwtSecret(); | ||
export const PORT = Number(process.env.PORT ?? '3000'); | ||
export const isDevelopment = process.env.NODE_ENV === 'development'; | ||
export const AUTH_SIGNUP_ENABLED = Boolean(process.env.AUTH_SIGNUP_ENABLED); | ||
// https://www.npmjs.com/package/jsonwebtoken for `expiresIn` format | ||
export const JWT_EXPIRATION_PERIOD: number | string = process.env.JWT_EXPIRATION_PERIOD_SECONDS ? Number(process.env.JWT_EXPIRATION_PERIOD_SECONDS) : '7d'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { test, expect } from 'vitest'; | ||
import { getJwtSecret } from '../src/env/getters'; | ||
import { restoreEnvAfterEach } from './helpers/env'; | ||
|
||
restoreEnvAfterEach(); | ||
|
||
test('Env: production has jwt secret defined', async () => { | ||
process.env.JWT_SECRET = ''; | ||
process.env.NODE_ENV = 'production'; | ||
expect(getJwtSecret).toThrowError('JWT_SECRET is not defined'); | ||
}); | ||
|
||
test('Env: dev environment has jwt secret automatically set', async () => { | ||
process.env.JWT_SECRET = ''; | ||
expect(getJwtSecret()).toBe('dev'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { test, expect } from 'vitest'; | ||
import builder from 'gql-query-builder'; | ||
import { CoreUnit } from '@prisma/client'; | ||
import { cleanDatabase as cleanDatabaseBeforeAfterEachTest } from './helpers/database'; | ||
import { executeGraphQlQuery } from './helpers/server'; | ||
import { getPrisma } from '../src/database'; | ||
|
||
cleanDatabaseBeforeAfterEachTest(); | ||
|
||
test('Core Unit: get all', async () => { | ||
const prisma = getPrisma(); | ||
await prisma.coreUnit.create({ | ||
data: { | ||
code: 'asdf', | ||
shortCode: 'a', | ||
name: 'name', | ||
imageSource: '', | ||
descriptionSentence: '', | ||
descriptionParagraph: '', | ||
descriptionParagraphImageSource: '', | ||
}, | ||
}); | ||
const query = builder.query({ | ||
operation: 'coreUnits', | ||
fields: ['code', 'shortCode', 'name'], | ||
}); | ||
const response = await executeGraphQlQuery(query) as { coreUnits: CoreUnit[] }; | ||
expect(response.coreUnits).toHaveLength(1); | ||
expect(response.coreUnits[0].code).toBe('asdf'); | ||
expect(response.coreUnits[0].shortCode).toBe('a'); | ||
expect(response.coreUnits[0].name).toBe('name'); | ||
}); | ||
|
||
test('Core Unit: get by id', async () => { | ||
const prisma = getPrisma(); | ||
const created = await prisma.coreUnit.create({ | ||
data: { | ||
code: 'asdf', | ||
shortCode: 'a', | ||
name: 'name', | ||
imageSource: '', | ||
descriptionSentence: '', | ||
descriptionParagraph: '', | ||
descriptionParagraphImageSource: '', | ||
}, | ||
}); | ||
const query = builder.query({ | ||
operation: 'coreUnit', | ||
variables: { | ||
id: created.id, | ||
}, | ||
fields: ['id'], | ||
}); | ||
const response = await executeGraphQlQuery(query) as { coreUnit: CoreUnit }; | ||
expect(response.coreUnit.id).toBe(created.id); | ||
}); | ||
|
||
test('Core Unit: get by id without id field throws', async () => { | ||
const query = builder.query({ | ||
operation: 'coreUnit', | ||
fields: ['id'], | ||
}); | ||
const response = await executeGraphQlQuery(query) as { errors: Record<string, unknown>[] }; | ||
expect(response.errors[0].message).toBe('please provide id'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { afterEach } from 'vitest'; | ||
|
||
const originalEnv = { ...process.env }; | ||
export function restoreEnvAfterEach() { | ||
afterEach(() => { | ||
process.env = { ...originalEnv }; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
import { defineConfig } from 'vitest/config'; | ||
import { defineConfig, UserConfigExport } from 'vitest/config'; | ||
|
||
export default defineConfig({ | ||
test: { | ||
coverage: { | ||
provider: 'istanbul', | ||
lines: 0, | ||
functions: 0, | ||
statements: 0, | ||
all: true, | ||
export const getVitestConfig = (fullyCoveredModulePaths?: string[]): UserConfigExport => { | ||
const enableFullCoverage = !!fullyCoveredModulePaths && fullyCoveredModulePaths.length !== 0; | ||
const coverage = enableFullCoverage ? 100 : 90; | ||
return { | ||
test: { | ||
coverage: { | ||
enabled: true, | ||
provider: 'istanbul', | ||
lines: coverage, | ||
functions: coverage, | ||
statements: coverage, | ||
include: fullyCoveredModulePaths || undefined, | ||
}, | ||
singleThread: true, | ||
}, | ||
singleThread: true, | ||
}, | ||
}); | ||
}; | ||
}; | ||
|
||
export default defineConfig(getVitestConfig()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { defineConfig } from 'vitest/config'; | ||
import { getVitestConfig } from './vitest.config'; | ||
|
||
const fullyCoveredModulePaths = [ | ||
'src/modules/**', | ||
]; | ||
|
||
export default defineConfig(getVitestConfig(fullyCoveredModulePaths)); |