Skip to content

Commit

Permalink
more tests and better types
Browse files Browse the repository at this point in the history
  • Loading branch information
v1rtl committed Jun 4, 2023
1 parent 40d2beb commit c524ab6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

# rpc

[![nest badge][nest-badge]](https://nest.land/package/rpc/mod.ts) [![][docs-badge]][docs] [![][code-quality-img]][code-quality]
[![nest badge][nest-badge]](https://nest.land/package/rpc/mod.ts)
[![][docs-badge]][docs] [![][code-quality-img]][code-quality]

</div>

Expand Down
9 changes: 9 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions request_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { parseRequest } from './request.ts'
import { assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts'

Deno.test('parseRequest', async (t) => {
await t.step('Returns an error if not object', () => {
assertEquals(parseRequest('i am text'), 'parse-error')
})
await t.step('Marks as invalid if empty array', () => {
assertEquals(parseRequest('[]'), ['invalid'])
})
await t.step('Marks as invalid if not array of objects', () => {
assertEquals(parseRequest('["i am text"]'), ['invalid'])
})
await t.step('Marks as invalid if version is not 2.0', () => {
assertEquals(parseRequest(JSON.stringify({ method: 'hello' })), ['invalid'])
assertEquals(
parseRequest(JSON.stringify({ method: 'hello', jsonrpc: '1.0' })),
['invalid'],
)
})
await t.step('Marks as invalid if method is missing', () => {
assertEquals(parseRequest(JSON.stringify({ jsonrpc: '2.0' })), ['invalid'])
})
await t.step('Properly parses valid request', () => {
const response = { jsonrpc: '2.0', method: 'hello' }
assertEquals(parseRequest(JSON.stringify(response)), [response])
})
})
2 changes: 1 addition & 1 deletion types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface JsonRpcRequest<T extends unknown[] = unknown[]> {
method: string
id?: string
params: T
params?: T
}

export type JsonRpcError = {
Expand Down
26 changes: 17 additions & 9 deletions utils_test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import { lazyJSONParse, pathsAreEqual } from './utils.ts'
import { assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts'
import { delay, lazyJSONParse, pathsAreEqual } from './utils.ts'
import { assertEquals, assert } from 'https://deno.land/[email protected]/testing/asserts.ts'

Deno.test('lazyJSONParse', async (it) => {
await it.step('should parse JSON like JSON.parse', () => {
Deno.test('lazyJSONParse', async (t) => {
await t.step('should parse JSON like JSON.parse', () => {
assertEquals(lazyJSONParse('{ "a": "b" }'), JSON.parse('{ "a": "b" }'))
})
await it.step('should return an empty object on failed parse', () => {
await t.step('should return an empty object on failed parse', () => {
assertEquals(lazyJSONParse('{ "a": "b"'), {})
})
})

Deno.test('pathsAreEqual', async (it) => {
await it.step('if expected path is asterisk, return true', () => {
Deno.test('pathsAreEqual', async (t) => {
await t.step('if expected path is asterisk, return true', () => {
assertEquals(pathsAreEqual('/hello', '*'), true)
})
await it.step('should assert equal paths', () => {
await t.step('should assert equal paths', () => {
assertEquals(pathsAreEqual('/hello', '/hello'), true)
})
await it.step('if nothing is expected, default to "/"', () => {
await t.step('if nothing is expected, default to "/"', () => {
assertEquals(pathsAreEqual('/'), true)
})
})

Deno.test('delay', async (t) => {
await t.step('it delays a function for given time', async () => {
const then = performance.now()
await delay(10)
assert(performance.now() - then < 15) // there's extra run-time
})
})

0 comments on commit c524ab6

Please sign in to comment.