From 2eef3ce957f347728f6a890f8af0ce23b9e7484b Mon Sep 17 00:00:00 2001 From: Rhuan Barreto Date: Fri, 10 May 2024 01:52:20 +0200 Subject: [PATCH 1/5] test: add test cases for parse date and random object --- test/treaty2.test.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/treaty2.test.ts b/test/treaty2.test.ts index 412e7df..e97659f 100644 --- a/test/treaty2.test.ts +++ b/test/treaty2.test.ts @@ -29,6 +29,7 @@ const app = new Elysia() .get('/number', () => 1) .get('/true', () => true) .get('/false', () => false) + .get('/date', () => new Date("2022-01-01")) .post('/array', ({ body }) => body, { body: t.Array(t.String()) }) @@ -43,6 +44,7 @@ const app = new Elysia() password: t.String() }) }) + .get('/randomObject', () => randomObject) .get('/query', ({ query }) => query, { query: t.Object({ username: t.String() @@ -171,6 +173,17 @@ describe('Treaty2', () => { expect(data).toEqual(false) }) + it('parse date', async () => { + const { data } = await client.date.get() + console.log(data instanceof Date) + expect(data).toEqual(new Date("2022-01-01")) + }) + + it('get random object', async () => { + const { data } = await client.randomObject.get() + expect(data).toEqual(randomObject) + }) + it('post array', async () => { const { data } = await client.array.post(['a', 'b']) From 2b877d5b20c2bf4bdc8bf35e87cc0b5e04eda44b Mon Sep 17 00:00:00 2001 From: Rhuan Barreto Date: Fri, 10 May 2024 01:55:21 +0200 Subject: [PATCH 2/5] fix expectation --- test/treaty2.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/treaty2.test.ts b/test/treaty2.test.ts index e97659f..b1e8bcf 100644 --- a/test/treaty2.test.ts +++ b/test/treaty2.test.ts @@ -175,7 +175,7 @@ describe('Treaty2', () => { it('parse date', async () => { const { data } = await client.date.get() - console.log(data instanceof Date) + expect(data instanceof Date).toBeTrue() expect(data).toEqual(new Date("2022-01-01")) }) From 80ef6d814ed2c514bba94c66227752f54ad0ca95 Mon Sep 17 00:00:00 2001 From: Rhuan Barreto Date: Fri, 10 May 2024 02:03:03 +0200 Subject: [PATCH 3/5] add one more failing test --- test/treaty2.test.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/treaty2.test.ts b/test/treaty2.test.ts index b1e8bcf..f1fae3d 100644 --- a/test/treaty2.test.ts +++ b/test/treaty2.test.ts @@ -30,6 +30,7 @@ const app = new Elysia() .get('/true', () => true) .get('/false', () => false) .get('/date', () => new Date("2022-01-01")) + .get('/dateString', () => "1970-01-01T00:00:00.000Z") .post('/array', ({ body }) => body, { body: t.Array(t.String()) }) @@ -179,6 +180,12 @@ describe('Treaty2', () => { expect(data).toEqual(new Date("2022-01-01")) }) + it('parse date string', async () => { + const { data } = await client.dateString.get() + expect(typeof data).toBe("string") + expect(data).toBe("1970-01-01T00:00:00.000Z") + }) + it('get random object', async () => { const { data } = await client.randomObject.get() expect(data).toEqual(randomObject) From aacd04445aed3bc2306199980ac15891f803e0a1 Mon Sep 17 00:00:00 2001 From: Rhuan Barreto Date: Fri, 10 May 2024 02:14:15 +0200 Subject: [PATCH 4/5] add one more case --- test/treaty2.test.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/treaty2.test.ts b/test/treaty2.test.ts index f1fae3d..d208084 100644 --- a/test/treaty2.test.ts +++ b/test/treaty2.test.ts @@ -31,6 +31,8 @@ const app = new Elysia() .get('/false', () => false) .get('/date', () => new Date("2022-01-01")) .get('/dateString', () => "1970-01-01T00:00:00.000Z") + .get('/objectWithdateString', () => ({ d: "1970-01-01T00:00:00.000Z"})) + .get('/randomObject', () => randomObject) .post('/array', ({ body }) => body, { body: t.Array(t.String()) }) @@ -45,7 +47,6 @@ const app = new Elysia() password: t.String() }) }) - .get('/randomObject', () => randomObject) .get('/query', ({ query }) => query, { query: t.Object({ username: t.String() @@ -186,6 +187,12 @@ describe('Treaty2', () => { expect(data).toBe("1970-01-01T00:00:00.000Z") }) + it('parse object with date string', async () => { + const { data } = await client.objectWithdateString.get() + expect(typeof data?.d).toBe("string") + expect(data?.d).toBe("1970-01-01T00:00:00.000Z") + }) + it('get random object', async () => { const { data } = await client.randomObject.get() expect(data).toEqual(randomObject) From 8962cf1d003f7ea2ac693842fd80e385af7e316d Mon Sep 17 00:00:00 2001 From: Rhuan Barreto Date: Fri, 10 May 2024 02:29:53 +0200 Subject: [PATCH 5/5] more failing cases --- test/treaty2.test.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/treaty2.test.ts b/test/treaty2.test.ts index d208084..607822d 100644 --- a/test/treaty2.test.ts +++ b/test/treaty2.test.ts @@ -4,7 +4,7 @@ import { treaty } from '../src' import { describe, expect, it, beforeAll, afterAll, mock } from 'bun:test' -const randomObject = { a: 'a', b: 2, c: true, d: false, e: null, f: new Date(0) } +const randomObject = { a: 'a', b: 2, c: true, d: false, e: null, f: new Date(0), g: "1970-01-01T00:00:00.000Z"} const randomArray = ['a', 2, true, false, null, new Date(0), { a: 'a', b: 2, c: true, d: false, e: null, f: new Date(0)}] const websocketPayloads = [ // strings @@ -17,6 +17,8 @@ const websocketPayloads = [ null, // A date new Date(0), + // A date as a string + "1970-01-01T00:00:00.000Z", // A random object randomObject, // A random array @@ -31,7 +33,7 @@ const app = new Elysia() .get('/false', () => false) .get('/date', () => new Date("2022-01-01")) .get('/dateString', () => "1970-01-01T00:00:00.000Z") - .get('/objectWithdateString', () => ({ d: "1970-01-01T00:00:00.000Z"})) + .get('/objectWithDateString', () => ({ d: "1970-01-01T00:00:00.000Z"})) .get('/randomObject', () => randomObject) .post('/array', ({ body }) => body, { body: t.Array(t.String()) @@ -188,7 +190,7 @@ describe('Treaty2', () => { }) it('parse object with date string', async () => { - const { data } = await client.objectWithdateString.get() + const { data } = await client.objectWithDateString.get() expect(typeof data?.d).toBe("string") expect(data?.d).toBe("1970-01-01T00:00:00.000Z") })