Skip to content

Commit

Permalink
Merge pull request #209 from H4ad/test/add-more-tests
Browse files Browse the repository at this point in the history
test: add more tests & clean code
  • Loading branch information
H4ad authored Feb 29, 2024
2 parents 115628a + c3dcfff commit 26fb92d
Show file tree
Hide file tree
Showing 44 changed files with 269 additions and 154 deletions.
14 changes: 7 additions & 7 deletions src/adapters/apollo-server/apollo-server-mutation.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ export type ApolloServerMutationAdapterOptions = {
* @breadcrumb Adapters / Apollo Server / ApolloServerMutationAdapter
* @public
*/
export class ApolloServerMutationAdapter<
TEvent,
TContext,
TResponse,
TBaseAdapter extends AdapterContract<TEvent, TContext, TResponse>,
> implements AdapterContract<TEvent, TContext, TResponse>
export class ApolloServerMutationAdapter<TEvent, TContext, TResponse>
implements AdapterContract<TEvent, TContext, TResponse>
{
//#region Constructor

/**
* The default constructor
*/
constructor(
protected readonly baseAdapter: TBaseAdapter,
protected readonly baseAdapter: AdapterContract<
TEvent,
TContext,
TResponse
>,
protected readonly options: ApolloServerMutationAdapterOptions,
) {}

Expand Down
9 changes: 6 additions & 3 deletions src/network/response-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,12 @@ export class ServerlessStreamResponse extends ServerResponse {
endHeaderIndex,
);
const headers = parseHeaders(headerData);
log.debug('SERVERLESS_ADAPTER:RESPONSE_STREAM:FRAMEWORK_HEADERS', {
headers,
});
log.debug(
'SERVERLESS_ADAPTER:RESPONSE_STREAM:FRAMEWORK_HEADERS',
() => ({
headers,
}),
);

writesToIgnore = 1;
internalWritable = onReceiveHeaders(status, headers);
Expand Down
9 changes: 5 additions & 4 deletions test/adapters/apollo-server/apollo-mutation.adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { ApolloServer } from '@apollo/server';
import { describe, expect, it, vitest } from 'vitest';
import type { SQSEvent } from 'aws-lambda';
import {
AdapterContract,
type AdapterContract,
EmptyResponse,
GetResponseAdapterProps,
type GetResponseAdapterProps,
ServerlessRequest,
ServerlessResponse,
createDefaultLogger,
waitForStreamComplete,
} from '../../../src';
import {
ApolloServerMutationAdapter,
ApolloServerMutationAdapterOptions,
type ApolloServerMutationAdapterOptions,
} from '../../../src/adapters/apollo-server';
import {
DynamoDBAdapter,
Expand Down Expand Up @@ -279,7 +280,7 @@ it('onErrorWhileForwarding: should forward error dealing to base adapter', () =>
});

const props = {
event: {},
event: {} as SQSEvent,
log: createDefaultLogger(),
error: new Error(),
delegatedResolver: { fail: vitest.fn(), succeed: vitest.fn() },
Expand Down
6 changes: 3 additions & 3 deletions test/adapters/aws/alb.adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { ALBEvent, ALBResult } from 'aws-lambda';
import { beforeEach, describe, expect, it, vitest } from 'vitest';
import {
DelegatedResolver,
GetResponseAdapterProps,
ILogger,
type DelegatedResolver,
type GetResponseAdapterProps,
type ILogger,
getEventBodyAsBuffer,
getFlattenedHeadersMap,
getMultiValueHeadersMap,
Expand Down
6 changes: 3 additions & 3 deletions test/adapters/aws/api-gateway-v1.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { APIGatewayProxyResult } from 'aws-lambda';
import type { APIGatewayProxyEvent } from 'aws-lambda/trigger/api-gateway-proxy';
import { beforeEach, describe, expect, it, vitest } from 'vitest';
import {
DelegatedResolver,
GetResponseAdapterProps,
ILogger,
type DelegatedResolver,
type GetResponseAdapterProps,
type ILogger,
ServerlessResponse,
getEventBodyAsBuffer,
getFlattenedHeadersMap,
Expand Down
6 changes: 3 additions & 3 deletions test/adapters/aws/api-gateway-v2.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { APIGatewayProxyEventV2 } from 'aws-lambda';
import type { APIGatewayProxyStructuredResultV2 } from 'aws-lambda/trigger/api-gateway-proxy';
import { beforeEach, describe, expect, it, vitest } from 'vitest';
import {
DelegatedResolver,
GetResponseAdapterProps,
ILogger,
type DelegatedResolver,
type GetResponseAdapterProps,
type ILogger,
ServerlessResponse,
getEventBodyAsBuffer,
getFlattenedHeadersMap,
Expand Down
4 changes: 2 additions & 2 deletions test/adapters/aws/aws-simple-adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, it, vitest } from 'vitest';
import {
DelegatedResolver,
type DelegatedResolver,
EmptyResponse,
ILogger,
type ILogger,
createDefaultLogger,
getEventBodyAsBuffer,
} from '../../../src';
Expand Down
10 changes: 5 additions & 5 deletions test/adapters/aws/lambda-edge.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import type {
} from 'aws-lambda/trigger/cloudfront-request';
import { beforeEach, describe, expect, it, vitest } from 'vitest';
import {
BothValueHeaders,
DelegatedResolver,
ILogger,
MultiValueHeaders,
SingleValueHeaders,
type BothValueHeaders,
type DelegatedResolver,
type ILogger,
type MultiValueHeaders,
type SingleValueHeaders,
} from '../../../src';
import {
DEFAULT_LAMBDA_EDGE_DISALLOWED_HEADERS,
Expand Down
10 changes: 5 additions & 5 deletions test/adapters/aws/request-lambda-edge.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import type {
} from 'aws-lambda/trigger/cloudfront-request';
import { beforeEach, describe, expect, it, vitest } from 'vitest';
import {
BothValueHeaders,
DelegatedResolver,
ILogger,
MultiValueHeaders,
SingleValueHeaders,
type BothValueHeaders,
type DelegatedResolver,
type ILogger,
type MultiValueHeaders,
type SingleValueHeaders,
} from '../../../src';
import {
DEFAULT_LAMBDA_EDGE_DISALLOWED_HEADERS,
Expand Down
8 changes: 4 additions & 4 deletions test/adapters/azure/http-trigger.adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Cookie, HttpRequest, HttpResponseSimple } from '@azure/functions';
import { beforeEach, describe, expect, it, vitest } from 'vitest';
import {
DelegatedResolver,
GetResponseAdapterProps,
ILogger,
type DelegatedResolver,
type GetResponseAdapterProps,
type ILogger,
getEventBodyAsBuffer,
getFlattenedHeadersMap,
getPathWithQueryStringParams,
Expand Down Expand Up @@ -303,7 +303,7 @@ describe(HttpTriggerV4Adapter.name, () => {

const log = {} as ILogger;

const resolver: DelegatedResolver<HttpRequest> = {
const resolver: DelegatedResolver<HttpResponseSimple> = {
fail: vitest.fn(),
succeed: vitest.fn(),
};
Expand Down
2 changes: 1 addition & 1 deletion test/adapters/azure/utils/http-trigger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { URL } from 'url';
import type { Context, Form, HttpRequest } from '@azure/functions';
import { vitest } from 'vitest';
import { BothValueHeaders } from '../../../../src';
import { type BothValueHeaders } from '../../../../src';

export function createHttpTriggerEvent(
method: HttpRequest['method'],
Expand Down
12 changes: 7 additions & 5 deletions test/adapters/digital-ocean/http-function.adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { beforeEach, describe, expect, it, vitest } from 'vitest';
import {
DelegatedResolver,
GetResponseAdapterProps,
ILogger,
type DelegatedResolver,
type GetResponseAdapterProps,
type ILogger,
getEventBodyAsBuffer,
getPathWithQueryStringParams,
} from '../../../src';
import { DigitalOceanHttpEvent } from '../../../src/@types/digital-ocean';
import { DigitalOceanHttpResponse } from '../../../src/@types/digital-ocean/digital-ocean-http-response';
import {
type DigitalOceanHttpEvent,
type DigitalOceanHttpResponse,
} from '../../../src/@types/digital-ocean';
import { HttpFunctionAdapter } from '../../../src/adapters/digital-ocean';
import { createCanHandleTestsForAdapter } from '../utils/can-handle';
import { createHttpFunctionEvent } from './utils/http-function';
Expand Down
2 changes: 1 addition & 1 deletion test/adapters/digital-ocean/utils/http-function.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DigitalOceanHttpEvent } from '../../../../src/@types/digital-ocean';
import { type DigitalOceanHttpEvent } from '../../../../src/@types/digital-ocean';

export function createHttpFunctionEvent(
method: string,
Expand Down
2 changes: 1 addition & 1 deletion test/adapters/dummy/dummy.adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vitest } from 'vitest';
import {
DelegatedResolver,
type DelegatedResolver,
EmptyResponse,
createDefaultLogger,
} from '../../../src';
Expand Down
8 changes: 4 additions & 4 deletions test/adapters/huawei/huawei-api-gateway.adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { beforeEach, describe, expect, it, vitest } from 'vitest';
import {
DelegatedResolver,
GetResponseAdapterProps,
ILogger,
type DelegatedResolver,
type GetResponseAdapterProps,
type ILogger,
getEventBodyAsBuffer,
getFlattenedHeadersMap,
getPathWithQueryStringParams,
} from '../../../src';
import {
import type {
HuaweiApiGatewayEvent,
HuaweiApiGatewayResponse,
} from '../../../src/@types/huawei';
Expand Down
4 changes: 2 additions & 2 deletions test/adapters/huawei/utils/huawei-api-gateway.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BothValueHeaders } from '../../../../src';
import {
import type { BothValueHeaders } from '../../../../src';
import type {
HuaweiApiGatewayEvent,
HuaweiRequestQueryStringParameters,
} from '../../../../src/@types/huawei';
Expand Down
2 changes: 1 addition & 1 deletion test/adapters/utils/can-handle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { AdapterContract, ILogger } from '../../../src';
import type { AdapterContract, ILogger } from '../../../src';
import { allEvents } from './events';

export function createCanHandleTestsForAdapter<
Expand Down
36 changes: 22 additions & 14 deletions test/core/base-handler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import { describe, expect, it, vitest } from 'vitest';
import {
AdapterContract,
AdapterRequest,
type AdapterContract,
type AdapterRequest,
BaseHandler,
ILogger,
type ILogger,
ServerlessRequest,
ServerlessResponse,
createDefaultLogger,
} from '../../src';
import { AlbAdapter, SQSAdapter } from '../../src/adapters/aws';
import { createSQSEvent } from '../adapters/aws/utils/sqs';

class TestHandler<
class TestHandler<TApp, TContext, TCallback, TReturn> extends BaseHandler<
TApp,
TEvent,
unknown,
TContext,
TCallback,
TResponse,
TReturn,
> extends BaseHandler<TApp, TEvent, TContext, TCallback, TResponse, TReturn> {
unknown,
TReturn
> {
getHandler = vitest.fn();

/**
* {@inheritDoc}
*/
public getAdapterByEventAndContext(
public override getAdapterByEventAndContext(
event: any,
context: any,
adapters: AdapterContract<TEvent, TContext, TResponse>[],
adapters: AdapterContract<unknown, TContext, unknown>[],
log: ILogger,
): AdapterContract<TEvent, TContext, TResponse> {
): AdapterContract<unknown, TContext, unknown> {
return super.getAdapterByEventAndContext(event, context, adapters, log);
}

/**
* {@inheritDoc}
*/
public getServerlessRequestResponseFromAdapterRequest(
public override getServerlessRequestResponseFromAdapterRequest(
requestValues: AdapterRequest,
): [request: ServerlessRequest, response: ServerlessResponse] {
return super.getServerlessRequestResponseFromAdapterRequest(requestValues);
Expand All @@ -48,7 +48,11 @@ describe(BaseHandler.name, () => {
const handler = new TestHandler();
const testEvent = createSQSEvent();
const eventAdapter = new SQSAdapter();
const adapters = [eventAdapter, new AlbAdapter()];
const adapters = [eventAdapter, new AlbAdapter()] as AdapterContract<
any,
any,
any
>[];
const context = {};
const logger = createDefaultLogger();

Expand Down Expand Up @@ -89,7 +93,11 @@ describe(BaseHandler.name, () => {
it('should throw error when resolve more than one adapter', () => {
const handler = new TestHandler();
const testEvent = createSQSEvent();
const adapters = [new SQSAdapter(), new SQSAdapter()];
const adapters = [new SQSAdapter(), new SQSAdapter()] as AdapterContract<
any,
any,
any
>[];
const adapterNames = adapters
.map(adapter => adapter.getAdapterName())
.join(', ');
Expand Down
2 changes: 1 addition & 1 deletion test/core/headers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import {
BothValueHeaders,
type BothValueHeaders,
getFlattenedHeadersMap,
getFlattenedHeadersMapAndCookies,
getMultiValueHeadersMap,
Expand Down
2 changes: 1 addition & 1 deletion test/core/is-binary.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import {
BothValueHeaders,
type BothValueHeaders,
DEFAULT_BINARY_CONTENT_TYPES,
DEFAULT_BINARY_ENCODINGS,
getContentType,
Expand Down
6 changes: 3 additions & 3 deletions test/core/logger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/unbound-method */

import {
SpyInstance,
type MockInstance,
afterEach,
beforeEach,
describe,
Expand All @@ -10,14 +10,14 @@ import {
vitest,
} from 'vitest';
import {
LogLevels,
type LogLevels,
NO_OP,
createDefaultLogger,
isInternalLogger,
} from '../../src';

describe('createDefaultLogger', () => {
const mocks: SpyInstance[] = [];
const mocks: MockInstance[] = [];

beforeEach(() => {
const mockMethods: (keyof Console)[] = [
Expand Down
6 changes: 3 additions & 3 deletions test/core/utils/stream.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// credits to: https://github.com/b4nst/stream-mock/pull/64/files#diff-52aee274967f2fcfa3ffa78ebba2f510dd23d176aa92ccf8c0ad4843373f5ce7

import { Readable, ReadableOptions } from 'node:stream';
import { IReadableMock } from 'stream-mock';
import { Readable, type ReadableOptions } from 'node:stream';
import type { IReadableMock } from 'stream-mock';

/**
* ErrorReadableMock is a readable stream that mocks error.
Expand Down Expand Up @@ -34,7 +34,7 @@ export default class ErrorReadableMock
private expectedError: Error;

// tslint:disable-next-line:function-name Not responsible of this function name
public _read() {
public override _read() {
this.destroy(this.expectedError);
}
}
Loading

0 comments on commit 26fb92d

Please sign in to comment.