Skip to content

Commit c524ab6

Browse files
author
v1rtl
committed
more tests and better types
1 parent 40d2beb commit c524ab6

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
# rpc
44

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

78
</div>
89

deno.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

request_test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { parseRequest } from './request.ts'
2+
import { assertEquals } from 'https://deno.land/std@0.190.0/testing/asserts.ts'
3+
4+
Deno.test('parseRequest', async (t) => {
5+
await t.step('Returns an error if not object', () => {
6+
assertEquals(parseRequest('i am text'), 'parse-error')
7+
})
8+
await t.step('Marks as invalid if empty array', () => {
9+
assertEquals(parseRequest('[]'), ['invalid'])
10+
})
11+
await t.step('Marks as invalid if not array of objects', () => {
12+
assertEquals(parseRequest('["i am text"]'), ['invalid'])
13+
})
14+
await t.step('Marks as invalid if version is not 2.0', () => {
15+
assertEquals(parseRequest(JSON.stringify({ method: 'hello' })), ['invalid'])
16+
assertEquals(
17+
parseRequest(JSON.stringify({ method: 'hello', jsonrpc: '1.0' })),
18+
['invalid'],
19+
)
20+
})
21+
await t.step('Marks as invalid if method is missing', () => {
22+
assertEquals(parseRequest(JSON.stringify({ jsonrpc: '2.0' })), ['invalid'])
23+
})
24+
await t.step('Properly parses valid request', () => {
25+
const response = { jsonrpc: '2.0', method: 'hello' }
26+
assertEquals(parseRequest(JSON.stringify(response)), [response])
27+
})
28+
})

types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export interface JsonRpcRequest<T extends unknown[] = unknown[]> {
22
method: string
33
id?: string
4-
params: T
4+
params?: T
55
}
66

77
export type JsonRpcError = {

utils_test.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1-
import { lazyJSONParse, pathsAreEqual } from './utils.ts'
2-
import { assertEquals } from 'https://deno.land/std@0.190.0/testing/asserts.ts'
1+
import { delay, lazyJSONParse, pathsAreEqual } from './utils.ts'
2+
import { assertEquals, assert } from 'https://deno.land/std@0.190.0/testing/asserts.ts'
33

4-
Deno.test('lazyJSONParse', async (it) => {
5-
await it.step('should parse JSON like JSON.parse', () => {
4+
Deno.test('lazyJSONParse', async (t) => {
5+
await t.step('should parse JSON like JSON.parse', () => {
66
assertEquals(lazyJSONParse('{ "a": "b" }'), JSON.parse('{ "a": "b" }'))
77
})
8-
await it.step('should return an empty object on failed parse', () => {
8+
await t.step('should return an empty object on failed parse', () => {
99
assertEquals(lazyJSONParse('{ "a": "b"'), {})
1010
})
1111
})
1212

13-
Deno.test('pathsAreEqual', async (it) => {
14-
await it.step('if expected path is asterisk, return true', () => {
13+
Deno.test('pathsAreEqual', async (t) => {
14+
await t.step('if expected path is asterisk, return true', () => {
1515
assertEquals(pathsAreEqual('/hello', '*'), true)
1616
})
17-
await it.step('should assert equal paths', () => {
17+
await t.step('should assert equal paths', () => {
1818
assertEquals(pathsAreEqual('/hello', '/hello'), true)
1919
})
20-
await it.step('if nothing is expected, default to "/"', () => {
20+
await t.step('if nothing is expected, default to "/"', () => {
2121
assertEquals(pathsAreEqual('/'), true)
2222
})
2323
})
24+
25+
Deno.test('delay', async (t) => {
26+
await t.step('it delays a function for given time', async () => {
27+
const then = performance.now()
28+
await delay(10)
29+
assert(performance.now() - then < 15) // there's extra run-time
30+
})
31+
})

0 commit comments

Comments
 (0)