Skip to content

Commit

Permalink
Rip out tsdx, configure jest etc manually
Browse files Browse the repository at this point in the history
  • Loading branch information
af committed Jul 22, 2021
1 parent 81e5c80 commit 0f32147
Show file tree
Hide file tree
Showing 8 changed files with 1,264 additions and 4,606 deletions.
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
"src"
],
"scripts": {
"build": "tsdx build",
"coverage": "tsdx test --coverage",
"lint": "tsdx lint src tests",
"prepare": "tsdx build",
"start": "tsdx watch",
"test": "tsdx test"
"build": "tsc",
"coverage": "jest --coverage",
"lint": "tsc && prettier -list-different --write src tests",
"test": "jest"
},
"repository": {
"type": "git",
Expand All @@ -35,14 +33,19 @@
"environment variable",
"validation"
],
"dependencies": {},
"devDependencies": {
"@types/jest": "26.0.24",
"@types/node": "16.4.0",
"husky": "4.3.6",
"tsdx": "^0.14.1",
"typescript": "^4.1.3"
"jest": "27.0.6",
"prettier": "2.3.2",
"ts-jest": "27.0.4",
"tslib": "2.3.0",
"typescript": "4.3.5"
},
"author": "Aaron Franks",
"license": "MIT",
"dependencies": {},
"prettier": {
"printWidth": 100,
"semi": false,
Expand All @@ -51,7 +54,7 @@
},
"husky": {
"hooks": {
"pre-commit": "yarn lint --fix",
"pre-commit": "yarn lint",
"pre-push": "yarn test"
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/envalid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ export function customCleanEnv<T, MW>(
* For more context, see https://github.com/af/envalid/issues/32
*/
export const testOnly = <T>(defaultValueForTests: T) => {
return process.env.NODE_ENV === 'test' ? defaultValueForTests : ((testOnlySymbol as unknown) as T) // T is not strictly correct, but prevents type errors during usage
return process.env.NODE_ENV === 'test' ? defaultValueForTests : (testOnlySymbol as unknown as T) // T is not strictly correct, but prevents type errors during usage
}
2 changes: 1 addition & 1 deletion src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const defaultReporter = <T = any>(
colors.yellow('\n Exiting with error code 1'),
RULE,
]
.filter(x => !!x)
.filter((x) => !!x)
.join('\n')

logger(output)
Expand Down
6 changes: 4 additions & 2 deletions src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const isFQDN = (input: string) => {
// "best effort" regex-based IP address check
// If you want a more exhaustive check, create your own custom validator, perhaps wrapping this
// implementation (the source of the ipv4 regex below): https://github.com/validatorjs/validator.js/blob/master/src/lib/isIP.js
const ipv4Regex = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/
const ipv4Regex =
/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/
const ipv6Regex = /([a-f0-9]+:+)+[a-f0-9]+/
const isIP = (input: string) => {
if (!input.length) return false
Expand All @@ -27,7 +28,7 @@ const isIP = (input: string) => {
const EMAIL_REGEX = /^[^@\s]+@[^@\s]+\.[^@\s]+$/ // intentionally non-exhaustive

export const makeValidator = <T>(parseFn: (input: string) => T) => {
return function(spec?: Spec<T>): ValidatorSpec<T> {
return function (spec?: Spec<T>): ValidatorSpec<T> {
return { ...spec, _parse: parseFn }
}
}
Expand Down Expand Up @@ -104,6 +105,7 @@ export function port<T extends number = number>(spec?: Spec<T>) {
export function url<T extends string = string>(spec?: Spec<T>) {
return makeValidator((x: string) => {
try {
// @ts-expect-error TS doesn't acknowledge this API by default yet
new URL(x)
return x
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions tests/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ test('str()', () => {
})

test('custom types', () => {
const alwaysFoo = makeValidator(_x => 'foo')
const alwaysFoo = makeValidator((_x) => 'foo')

const fooEnv = cleanEnv({ FOO: 'asdf' }, { FOO: alwaysFoo() })
expect(fooEnv).toEqual({ FOO: 'foo' })

const hex10 = makeValidator(x => {
const hex10 = makeValidator((x) => {
if (/^[a-f0-9]{10}$/.test(x)) return x
throw new Error('need 10 hex chars')
})
Expand Down
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"module": "esnext",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true
"noImplicitReturns": true,
"lib": ["es2019"]
},
"include": ["src"],
"exclude": [
Expand Down
Loading

0 comments on commit 0f32147

Please sign in to comment.