mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
jest & lint (not clean yet)
This commit is contained in:
parent
07bc54f4f4
commit
85bc0c4a49
27
dht-node/jest.config.js
Normal file
27
dht-node/jest.config.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
|
||||||
|
module.exports = {
|
||||||
|
verbose: true,
|
||||||
|
preset: 'ts-jest',
|
||||||
|
collectCoverage: true,
|
||||||
|
collectCoverageFrom: ['src/**/*.ts', '!**/node_modules/**', '!src/seeds/**', '!build/**'],
|
||||||
|
setupFiles: ['<rootDir>/test/testSetup.ts'],
|
||||||
|
setupFilesAfterEnv: ['<rootDir>/test/extensions.ts'],
|
||||||
|
modulePathIgnorePatterns: ['<rootDir>/build/'],
|
||||||
|
moduleNameMapper: {
|
||||||
|
'@/(.*)': '<rootDir>/src/$1',
|
||||||
|
'@arg/(.*)': '<rootDir>/src/graphql/arg/$1',
|
||||||
|
'@enum/(.*)': '<rootDir>/src/graphql/enum/$1',
|
||||||
|
'@model/(.*)': '<rootDir>/src/graphql/model/$1',
|
||||||
|
'@union/(.*)': '<rootDir>/src/graphql/union/$1',
|
||||||
|
'@repository/(.*)': '<rootDir>/src/typeorm/repository/$1',
|
||||||
|
'@test/(.*)': '<rootDir>/test/$1',
|
||||||
|
'@entity/(.*)':
|
||||||
|
process.env.NODE_ENV === 'development'
|
||||||
|
? '<rootDir>/../database/entity/$1'
|
||||||
|
: '<rootDir>/../database/build/entity/$1',
|
||||||
|
'@dbTools/(.*)':
|
||||||
|
process.env.NODE_ENV === 'development'
|
||||||
|
? '<rootDir>/../database/src/$1'
|
||||||
|
: '<rootDir>/../database/build/src/$1',
|
||||||
|
},
|
||||||
|
}
|
||||||
@ -12,12 +12,14 @@
|
|||||||
"clean": "tsc --build --clean",
|
"clean": "tsc --build --clean",
|
||||||
"start": "cross-env TZ=UTC TS_NODE_BASEURL=./build node -r tsconfig-paths/register build/src/index.js",
|
"start": "cross-env TZ=UTC TS_NODE_BASEURL=./build node -r tsconfig-paths/register build/src/index.js",
|
||||||
"dev": "cross-env TZ=UTC nodemon -w src --ext ts --exec ts-node -r dotenv/config -r tsconfig-paths/register src/index.ts",
|
"dev": "cross-env TZ=UTC nodemon -w src --ext ts --exec ts-node -r dotenv/config -r tsconfig-paths/register src/index.ts",
|
||||||
"lint": "eslint --max-warnings=0 --ext .js,.ts ."
|
"lint": "eslint --max-warnings=0 --ext .js,.ts .",
|
||||||
|
"test": "cross-env TZ=UTC NODE_ENV=development jest --runInBand --coverage --forceExit --detectOpenHandles"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@hyperswarm/dht": "^6.3.3",
|
"@hyperswarm/dht": "^6.3.3",
|
||||||
"@types/dotenv": "^8.2.0",
|
"@types/dotenv": "^8.2.0",
|
||||||
"@types/i18n": "^0.13.6",
|
"@types/i18n": "^0.13.6",
|
||||||
|
"@types/jest": "^27.0.2",
|
||||||
"@types/jsonwebtoken": "^8.5.9",
|
"@types/jsonwebtoken": "^8.5.9",
|
||||||
"@types/lodash.clonedeep": "^4.5.7",
|
"@types/lodash.clonedeep": "^4.5.7",
|
||||||
"@types/node": "^18.11.18",
|
"@types/node": "^18.11.18",
|
||||||
@ -49,6 +51,8 @@
|
|||||||
"eslint-plugin-n": "^15.6.0",
|
"eslint-plugin-n": "^15.6.0",
|
||||||
"eslint-plugin-prettier": "^4.2.1",
|
"eslint-plugin-prettier": "^4.2.1",
|
||||||
"eslint-plugin-promise": "^6.1.1",
|
"eslint-plugin-promise": "^6.1.1",
|
||||||
"prettier": "^2.3.1"
|
"jest": "^27.2.4",
|
||||||
|
"prettier": "^2.3.1",
|
||||||
|
"ts-jest": "^27.0.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
33
dht-node/test/extensions.ts
Normal file
33
dht-node/test/extensions.ts
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-empty-interface */
|
||||||
|
|
||||||
|
import Decimal from 'decimal.js-light'
|
||||||
|
|
||||||
|
expect.extend({
|
||||||
|
decimalEqual(received, value) {
|
||||||
|
const pass = new Decimal(value).equals(received.toString())
|
||||||
|
if (pass) {
|
||||||
|
return {
|
||||||
|
message: () => `expected ${received} to not equal ${value}`,
|
||||||
|
pass: true,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
message: () => `expected ${received} to equal ${value}`,
|
||||||
|
pass: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
interface CustomMatchers<R = unknown> {
|
||||||
|
decimalEqual(value: number): R
|
||||||
|
}
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
||||||
|
namespace jest {
|
||||||
|
interface Expect extends CustomMatchers {}
|
||||||
|
interface Matchers<R> extends CustomMatchers<R> {}
|
||||||
|
interface InverseAsymmetricMatchers extends CustomMatchers {}
|
||||||
|
}
|
||||||
|
}
|
||||||
7
dht-node/test/helpers.test.ts
Normal file
7
dht-node/test/helpers.test.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { contributionDateFormatter } from '@test/helpers'
|
||||||
|
|
||||||
|
describe('contributionDateFormatter', () => {
|
||||||
|
it('formats the date correctly', () => {
|
||||||
|
expect(contributionDateFormatter(new Date('Thu Feb 29 2024 13:12:11'))).toEqual('2/29/2024')
|
||||||
|
})
|
||||||
|
})
|
||||||
59
dht-node/test/helpers.ts
Normal file
59
dht-node/test/helpers.ts
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||||
|
|
||||||
|
import { createTestClient } from 'apollo-server-testing'
|
||||||
|
import createServer from '../src/server/createServer'
|
||||||
|
import { initialize } from '@dbTools/helpers'
|
||||||
|
import { entities } from '@entity/index'
|
||||||
|
import { i18n, logger } from './testSetup'
|
||||||
|
|
||||||
|
export const headerPushMock = jest.fn((t) => {
|
||||||
|
context.token = t.value
|
||||||
|
})
|
||||||
|
|
||||||
|
const context = {
|
||||||
|
token: '',
|
||||||
|
setHeaders: {
|
||||||
|
push: headerPushMock,
|
||||||
|
forEach: jest.fn(),
|
||||||
|
},
|
||||||
|
clientTimezoneOffset: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
export const cleanDB = async () => {
|
||||||
|
// this only works as lond we do not have foreign key constraints
|
||||||
|
for (let i = 0; i < entities.length; i++) {
|
||||||
|
await resetEntity(entities[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const testEnvironment = async (testLogger: any = logger, testI18n: any = i18n) => {
|
||||||
|
const server = await createServer(context, testLogger, testI18n)
|
||||||
|
const con = server.con
|
||||||
|
const testClient = createTestClient(server.apollo)
|
||||||
|
const mutate = testClient.mutate
|
||||||
|
const query = testClient.query
|
||||||
|
await initialize()
|
||||||
|
return { mutate, query, con }
|
||||||
|
}
|
||||||
|
|
||||||
|
export const resetEntity = async (entity: any) => {
|
||||||
|
const items = await entity.find({ withDeleted: true })
|
||||||
|
if (items.length > 0) {
|
||||||
|
const ids = items.map((i: any) => i.id)
|
||||||
|
await entity.delete(ids)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const resetToken = () => {
|
||||||
|
context.token = ''
|
||||||
|
}
|
||||||
|
|
||||||
|
// format date string as it comes from the frontend for the contribution date
|
||||||
|
export const contributionDateFormatter = (date: Date): string => {
|
||||||
|
return `${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}`
|
||||||
|
}
|
||||||
|
|
||||||
|
export const setClientTimezoneOffset = (offset: number): void => {
|
||||||
|
context.clientTimezoneOffset = offset
|
||||||
|
}
|
||||||
41
dht-node/test/testSetup.ts
Normal file
41
dht-node/test/testSetup.ts
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
import CONFIG from '@/config'
|
||||||
|
import { backendLogger as logger } from '@/server/logger'
|
||||||
|
import { i18n } from '@/server/localization'
|
||||||
|
|
||||||
|
CONFIG.EMAIL = true
|
||||||
|
CONFIG.EMAIL_TEST_MODUS = false
|
||||||
|
|
||||||
|
jest.setTimeout(1000000)
|
||||||
|
|
||||||
|
jest.mock('@/server/logger', () => {
|
||||||
|
const originalModule = jest.requireActual('@/server/logger')
|
||||||
|
return {
|
||||||
|
__esModule: true,
|
||||||
|
...originalModule,
|
||||||
|
backendLogger: {
|
||||||
|
addContext: jest.fn(),
|
||||||
|
trace: jest.fn(),
|
||||||
|
debug: jest.fn(),
|
||||||
|
warn: jest.fn(),
|
||||||
|
info: jest.fn(),
|
||||||
|
error: jest.fn(),
|
||||||
|
fatal: jest.fn(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
jest.mock('@/server/localization', () => {
|
||||||
|
const originalModule = jest.requireActual('@/server/localization')
|
||||||
|
return {
|
||||||
|
__esModule: true,
|
||||||
|
...originalModule,
|
||||||
|
i18n: {
|
||||||
|
init: jest.fn(),
|
||||||
|
// configure: jest.fn(),
|
||||||
|
// __: jest.fn(),
|
||||||
|
// setLocale: jest.fn(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
export { logger, i18n }
|
||||||
2255
dht-node/yarn.lock
2255
dht-node/yarn.lock
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user