@nymphjs/server 1.0.0-beta.2 → 1.0.0-beta.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.test.ts CHANGED
@@ -5,14 +5,17 @@ import { Nymph } from '@nymphjs/client-node';
5
5
  import { Entity } from '@nymphjs/client';
6
6
 
7
7
  import createServer from './index';
8
- import { EmployeeModel, Employee } from './testArtifacts';
8
+ import {
9
+ EmployeeModel as EmployeeModelClass,
10
+ Employee as EmployeeClass,
11
+ } from './testArtifacts';
9
12
 
10
13
  const sqliteConfig = {
11
14
  filename: ':memory:',
12
15
  };
13
16
 
14
17
  const nymphServer = new NymphServer({}, new SQLite3Driver(sqliteConfig));
15
- nymphServer.addEntityClass(EmployeeModel);
18
+ const EmployeeModel = nymphServer.addEntityClass(EmployeeModelClass);
16
19
 
17
20
  const app = express();
18
21
  app.use('/test', createServer(nymphServer));
@@ -21,7 +24,7 @@ const server = app.listen(5080);
21
24
  const nymph = new Nymph({
22
25
  restUrl: 'http://localhost:5080/test/',
23
26
  });
24
- nymph.addEntityClass(Employee);
27
+ const Employee = nymph.addEntityClass(EmployeeClass);
25
28
 
26
29
  describe('Nymph REST Server and Client', () => {
27
30
  async function createJane() {
@@ -490,27 +493,57 @@ describe('Nymph REST Server and Client', () => {
490
493
  });
491
494
 
492
495
  it('handle server side static error', async () => {
493
- let error = { error: { name: '' } };
496
+ let error = { status: 0, error: { name: '' } };
494
497
  try {
495
498
  await Employee.throwErrorStatic();
496
499
  } catch (e: any) {
497
500
  error = e;
498
501
  }
502
+ expect(error.status).toEqual(500);
499
503
  expect(error.error.name).toEqual('BadFunctionCallError');
500
504
  });
501
505
 
502
506
  it('handle server side error', async () => {
503
507
  const jane = await createJane();
504
508
 
505
- let error = { error: { name: '' } };
509
+ let error = { status: 0, error: { name: '' } };
506
510
  try {
507
511
  await jane.$throwError();
508
512
  } catch (e: any) {
509
513
  error = e;
510
514
  }
515
+ expect(error.status).toEqual(500);
511
516
  expect(error.error.name).toEqual('BadFunctionCallError');
512
517
  });
513
518
 
519
+ it('handle server side HTTP error', async () => {
520
+ const jane = await createJane();
521
+
522
+ let error = { status: 0, statusText: '', message: '' };
523
+ try {
524
+ await jane.$throwHttpError();
525
+ } catch (e: any) {
526
+ error = e;
527
+ }
528
+ expect(error.status).toEqual(501);
529
+ expect(error.statusText).toEqual('Not Implemented');
530
+ expect(error.message).toEqual('A 501 HTTP error.');
531
+ });
532
+
533
+ it('handle server side custom HTTP error', async () => {
534
+ const jane = await createJane();
535
+
536
+ let error = { status: 0, statusText: '', message: '' };
537
+ try {
538
+ await jane.$throwHttpErrorWithDescription();
539
+ } catch (e: any) {
540
+ error = e;
541
+ }
542
+ expect(error.status).toEqual(512);
543
+ expect(error.statusText).toEqual('Some Error');
544
+ expect(error.message).toEqual('A 512 HTTP error.');
545
+ });
546
+
514
547
  it('call a server side static method', async () => {
515
548
  const data = await Employee.testStatic(5);
516
549
  expect(data).toEqual(10);