-
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.
- Loading branch information
Amine Ben hammou
committed
Aug 13, 2023
1 parent
7111077
commit bc2bcb3
Showing
14 changed files
with
954 additions
and
26 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
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 '@japa/runner' | ||
|
||
declare module '@japa/runner' { | ||
interface TestContext { | ||
// notify TypeScript about custom context properties | ||
} | ||
|
||
interface Test<TestData> { | ||
// notify TypeScript about custom test properties | ||
} | ||
} |
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,17 @@ | ||
import { expect } from '@japa/expect' | ||
import { pathToFileURL } from 'node:url' | ||
import { specReporter } from '@japa/spec-reporter' | ||
import { expectTypeOf } from '@japa/expect-type' | ||
import { processCliArgs, configure, run } from '@japa/runner' | ||
|
||
configure({ | ||
...processCliArgs(process.argv.slice(2)), | ||
...{ | ||
files: ['tests/**/*.test.ts'], | ||
plugins: [expect(), expectTypeOf()], | ||
reporters: [specReporter()], | ||
importer: (filePath) => import(pathToFileURL(filePath).href), | ||
}, | ||
}) | ||
|
||
run() |
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 |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import type { WariError } from "./WariError.js" | ||
import type { WariError } from './WariError.js' | ||
|
||
export interface ErrorTypes {} | ||
export type ErrorKey = keyof ErrorTypes | ||
export type ErrorDetails<K extends ErrorKey = ErrorKey> = ErrorTypes[K] | ||
export type GetErrorKeys<E> = E extends WariError<infer K> ? K : never | ||
export type ErrorHandlers<E> = { | ||
[key in GetErrorKeys<E>]: (x: WariError<key>) => any | ||
} | ||
export type ErrorHandlers<E> = E extends WariError<infer K> | ||
? { | ||
[key in K]: (x: WariError<key>) => any | ||
} | ||
: never | ||
// @ts-expect-error | ||
export type MatchReturn<E, H extends ErrorHandlers<E>> = Exclude<E, WariError<keyof H & keyof ErrorTypes>> | VoidToUndefined<ReturnType<H[keyof H]>> | ||
export type Normalize<T> = {[key in keyof T]: T[key]} & {} | ||
export type Normalize<T> = { [key in keyof T]: T[key] } & {} | ||
|
||
type VoidToUndefined<T> = T extends void ? undefined : T |
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,18 @@ | ||
import { test } from '@japa/runner' | ||
import { is } from '../src/index.js' | ||
import { WariError } from '../src/WariError.js' | ||
|
||
test.group('is', () => { | ||
test('it checks the type of a WariError', ({ expect }) => { | ||
const error = new WariError('JsonError', {text: ''}) | ||
expect(is(error, 'JsonError')).toBe(true) | ||
// @ts-expect-error | ||
expect(is(error, 'HttpError')).toBe(false) | ||
}) | ||
|
||
test(`it's typed`, ({expectTypeOf}) => { | ||
expectTypeOf(is).toMatchTypeOf((_x: WariError<'FileError'>, _type: 'FileError'): boolean => true) | ||
expectTypeOf(is).toMatchTypeOf((_x: WariError<'FileError'|'JsonError'>, _type: 'FileError'|'JsonError'): boolean => true) | ||
expectTypeOf(is).not.toMatchTypeOf((_x: Error, _type: 'JsonError'): boolean => false) | ||
}) | ||
}) |
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,6 +1,24 @@ | ||
import test from 'node:test'; | ||
import assert from 'assert/strict'; | ||
import { test } from '@japa/runner' | ||
import {make} from '../src/index.js' | ||
import { WariError } from '../src/WariError.js' | ||
|
||
test('1 is equal to 1.', () => { | ||
assert.strictEqual(1, 1); | ||
}); | ||
test.group('make', () => { | ||
test('it creates a new WariError', ({ expect }) => { | ||
const error = make('JsonError', {text: 'foo'}) | ||
expect(error).toBeInstanceOf(WariError) | ||
expect(error).toMatchObject({ | ||
type: 'JsonError', | ||
details: {text: 'foo'} | ||
}) | ||
expect(String(error)).toBe(`Error: JsonError: {"text":"foo"}`) | ||
}) | ||
|
||
test(`it's typed`, ({expectTypeOf}) => { | ||
expectTypeOf(make).toMatchTypeOf((type: 'HttpError', details: {method: 'GET' | 'POST', url: string, status: number}) => new WariError(type, details)) | ||
expectTypeOf(make).toMatchTypeOf((type: 'JsonError', details: {text: string}) => new WariError(type, details)) | ||
expectTypeOf(make).toMatchTypeOf((type: 'FileError', details: {operation: 'read' | 'write', filePath: string, error: Error}) => new WariError(type, details)) | ||
expectTypeOf(make).not.toMatchTypeOf((type: 'HttpError', details: {url: string, status: number}) => new WariError(type, details as any)) | ||
expectTypeOf(make).not.toMatchTypeOf((_type: 'HttpError', _details: {url: string, status: number}) => new Error('Oups!')) | ||
expectTypeOf(make).not.toMatchTypeOf((type: 'OtherError', details: {method: 'GET' | 'POST', url: string, status: number}) => new WariError(type as any, details)) | ||
}) | ||
}) |
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,59 @@ | ||
import { test } from '@japa/runner' | ||
import { make, match } from '../src/index.js' | ||
import { WariError } from '../src/WariError.js' | ||
|
||
test.group('match', () => { | ||
test('it returns the non-error value', ({ expect }) => { | ||
const fn = (): number | WariError<'JsonError'> | WariError<'HttpError'> => 1 | ||
const res = match(fn(), { | ||
HttpError: console.error, | ||
JsonError: console.error, | ||
}) | ||
expect(res).toBe(1) | ||
}) | ||
|
||
test('it returns the error handler return if error is matched', ({ expect }) => { | ||
const fn = (): number | WariError<'JsonError'> | WariError<'HttpError'> => make('JsonError', { text: '' }) | ||
const res = match(fn(), { | ||
HttpError: () => 100, | ||
JsonError: () => 200, | ||
}) | ||
expect(res).toBe(200) | ||
}) | ||
|
||
test(`it's typed`, ({ expectTypeOf }) => { | ||
const foo = (): WariError<'JsonError'> | WariError<'HttpError'> => make('JsonError', { text: '' }) | ||
const bar = (): number | WariError<'JsonError'> | WariError<'HttpError'> => 1 | ||
const baz = (): number => 2 | ||
|
||
expectTypeOf( | ||
match(foo(), { | ||
HttpError: () => {}, | ||
JsonError: () => {}, | ||
}) | ||
).toBeUndefined() | ||
|
||
expectTypeOf( | ||
match(foo(), { | ||
HttpError: () => 1, | ||
JsonError: () => 'Oups!', | ||
}) | ||
).toEqualTypeOf<number | string>() | ||
|
||
expectTypeOf( | ||
match(bar(), { | ||
HttpError: () => {}, | ||
JsonError: () => {}, | ||
}) | ||
).toEqualTypeOf<number | undefined>() | ||
|
||
expectTypeOf( | ||
match(bar(), { | ||
HttpError: () => {}, | ||
JsonError: () => 'Oups', | ||
}) | ||
).toEqualTypeOf<string | number | undefined>() | ||
|
||
expectTypeOf(match(baz(), {} as never)).toBeNumber() | ||
}) | ||
}) |
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 @@ | ||
declare module '../src/index.js' { | ||
interface ErrorTypes { | ||
'HttpError': {method: 'GET' | 'POST', url: string, status: number} | ||
'JsonError': {text: string} | ||
'FileError': {operation: 'read' | 'write', filePath: string, error: Error} | ||
} | ||
} | ||
|
||
export {} |
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 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "dist", | ||
"noEmit": false | ||
} | ||
} |
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
Oops, something went wrong.