Skip to content

Commit

Permalink
chore: jest types
Browse files Browse the repository at this point in the history
  • Loading branch information
denchiklut committed Oct 12, 2024
1 parent 097d366 commit 0330e69
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 41 deletions.
16 changes: 5 additions & 11 deletions @types/env/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import type { Env } from "src/common"

declare global {
declare const IS_DEV: boolean
declare const IS_PROD: boolean
declare const IS_SERVER: boolean
declare const IS_SPA: boolean
declare const clientPrefix: string
declare var IS_DEV: boolean
declare var IS_PROD: boolean
declare var IS_SERVER: boolean
declare var IS_SPA: boolean
declare var clientPrefix: string

namespace NodeJS {
interface ProcessEnv extends Partial<Collection<keyof Env, string>> {}
}

interface Window {
IS_SPA: boolean
IS_SERVER: boolean
IS_DEV: boolean
IS_PROD: boolean

env_vars: Partial<Collection<keyof Env, string>>
clientPrefix: string
}
}
1 change: 1 addition & 0 deletions @types/utils/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
type Collection<K extends string | number, V> = Record<K, V>
type Falsy = false | 0 | '' | null | undefined;

interface Array<T> {
filter<S extends T>(predicate: BooleanConstructor, thisArg?: unknown): Exclude<S, Falsy>[]
Expand Down
10 changes: 4 additions & 6 deletions config/spec/setup.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import '@testing-library/jest-dom'

declare const global: Window

global.IS_SERVER = false
global.IS_DEV = false
global.IS_SPA = true
global.clientPrefix = 'PUBLIC_'
globalThis.IS_SERVER = false
globalThis.IS_DEV = false
globalThis.IS_SPA = true
globalThis.clientPrefix = 'CLIENT_'

jest.mock('src/common/env/env.util', () => ({
...jest.requireActual('src/common/env/env.util'),
Expand Down
2 changes: 1 addition & 1 deletion src/common/env/env.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function createEnv<S extends ZodRawShape>({
const { success, data, error } = (IS_SERVER ? schema : client).safeParse(envs)

if (!success) {
logger.error('❌ Invalid environment variables:', error.flatten().fieldErrors)
logger.error('❌ Invalid environment variables: %o', error.flatten().fieldErrors)
throw new Error('Invalid environment variables')
}

Expand Down
1 change: 0 additions & 1 deletion src/server/middleware/error/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { ErrorRequestHandler } from 'express'

import { logger } from 'src/common'

export const error: ErrorRequestHandler = (error, _, res, __) => {
Expand Down
4 changes: 1 addition & 3 deletions webpack/configs/spa.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DIST_DIR, ROOT_DIR, devServerConfig } from '../utils'
import * as plugins from '../plugins'
import * as rules from '../rules'

const config = {
export default {
name: 'spa',
target: 'web',
devtool: 'eval-cheap-module-source-map',
Expand Down Expand Up @@ -43,5 +43,3 @@ const config = {
...plugins.htmlWebpackPlugin({ spa: true })
].filter(Boolean)
} as Configuration

export default config
36 changes: 17 additions & 19 deletions webpack/plugins/define.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@ interface Props {
server?: boolean
spa?: boolean
}
const config = (IS_SERVER: boolean, IS_SPA: boolean) => ({
IS_SERVER,
IS_DEV,
IS_PROD,
IS_SPA,
clientPrefix: JSON.stringify('CLIENT_'),
...(!IS_SERVER && {
'process.env': JSON.stringify(
Object.entries(process.env)
.filter(([k]) => k.startsWith('CLIENT_'))
.reduce<Collection<string, unknown>>((res, [k, v]) => {
res[k] = v
return res
}, {})
)
})
})

export const definePlugin = ({ server = false, spa = false }: Props = {}) =>
new DefinePlugin(config(server, spa))
new DefinePlugin({
IS_PROD,
IS_DEV,
IS_SPA: spa,
IS_SERVER: server,
clientPrefix: JSON.stringify('CLIENT_'),
...(spa && {
'process.env': JSON.stringify(
Object.entries(process.env)
.filter(([k]) => k.startsWith('CLIENT_'))
.reduce<Collection<string, unknown>>((res, [k, v]) => {
res[k] = v
return res
}, {})
)
})
})

0 comments on commit 0330e69

Please sign in to comment.