Skip to content

Commit

Permalink
test: update imports for clean up function
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgevrgs committed Sep 25, 2024
1 parent db55be3 commit dff24ee
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fastify-aws-powertools",
"version": "0.0.16",
"description": "",
"description": "Fastify plugin to use AWS Powertools",
"exports": {
"node": {
"require": "./dist/cjs/index.js",
Expand Down
10 changes: 4 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
export * from './commons/helpers';
export type * from './commons/types';
export * from './logger';
export * from './logger/classes';

export * from './metrics';
export {
fastifyAwsPowertools as default,
fastifyAwsPowertools,
fastifyAwsPowertoolsPlugin,
} from './plugin';

export * from './logger';
export * from './metrics';
export * from './tracer';

export type * from './commons/types';
6 changes: 6 additions & 0 deletions test/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { afterEach, beforeEach, vi } from 'vitest';

beforeEach(() => {
vi.spyOn(console, 'error').mockImplementation(vi.fn());
vi.spyOn(console, 'warn').mockImplementation(vi.fn());
vi.spyOn(console, 'info').mockImplementation(vi.fn());
vi.spyOn(console, 'debug').mockImplementation(vi.fn());
vi.spyOn(console, 'log').mockImplementation(vi.fn());

vi.mock('node:console', () => ({
...vi.importActual('node:console'),
Console: vi.fn().mockImplementation(() => ({
Expand Down
13 changes: 9 additions & 4 deletions test/integration/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('fastifyAwsPowertoolsLoggerPlugin', () => {

beforeEach(() => {
vi.stubEnv('POWERTOOLS_LOG_LEVEL', 'DEBUG');
vi.stubEnv('POWERTOOLS_DEV', 'true');

logSpy = vi.spyOn(console, 'info');
});
Expand Down Expand Up @@ -59,13 +60,17 @@ describe('fastifyAwsPowertoolsLoggerPlugin', () => {
// Prepare
logger = new Logger();

console.warn('calls --> ', logSpy.mock.calls);

// Act
logger.addContext(dummyContext);
logger.info('Hello, world!');

// Assess
expect(logSpy).toHaveBeenCalledTimes(1);
expect(JSON.parse(logSpy.mock.calls[0][0])).toStrictEqual(
expect(vi.mocked(console.info)).toHaveBeenCalledTimes(1);
expect(
JSON.parse(vi.mocked(console.info).mock.calls[0][0]),
).toStrictEqual(
expect.objectContaining({
message: 'Hello, world!',
...getContextLogEntries(),
Expand Down Expand Up @@ -101,7 +106,7 @@ describe('fastifyAwsPowertoolsLoggerPlugin', () => {

it('adds the context to log messages when the feature is enabled in the Middy.js middleware', async () => {
// Prepare
app = Fastify({ logger: false });
app = Fastify();
logger = new Logger();
app
.register(fastifyAwsPowertoolsLoggerPlugin, {
Expand All @@ -119,7 +124,7 @@ describe('fastifyAwsPowertoolsLoggerPlugin', () => {
await handler(event, dummyContext);

// Assess
expect(logSpy).toHaveBeenCalledTimes(1);
expect(logSpy).toHaveBeenCalledOnce();
expect(JSON.parse(logSpy.mock.calls[0][0])).toStrictEqual(
expect.objectContaining({
message: 'Hello, world!',
Expand Down
6 changes: 4 additions & 2 deletions test/integration/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import Fastify from 'fastify';
import { randomUUID } from 'node:crypto';
import type { MockInstance } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { fastifyAwsPowertoolsMetricsPlugin } from '../../src';
import { invokeCleanUpFunctions } from '../../src/commons/helpers';
import {
fastifyAwsPowertoolsMetricsPlugin,
invokeCleanUpFunctions,
} from '../../src';
import { dummyContext } from '../fixtures/context';
import { dummyEvent } from '../fixtures/event';

Expand Down
9 changes: 4 additions & 5 deletions test/integration/tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import type { FastifyInstance } from 'fastify';
import Fastify from 'fastify';
import { randomUUID } from 'node:crypto';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { fastifyAwsPowertoolsTracerPlugin } from '../../src';
import { invokeCleanUpFunctions } from '../../src/commons/helpers';
import {
fastifyAwsPowertoolsTracerPlugin,
invokeCleanUpFunctions,
} from '../../src';
import { dummyContext } from '../fixtures/context';
import { dummyEvent } from '../fixtures/event';

Expand Down Expand Up @@ -45,9 +47,6 @@ describe('fastifyAwsPowertoolsTracerPlugin', () => {
});

afterEach(async () => {
vi.unstubAllEnvs();
vi.clearAllMocks();

await app.close();
});

Expand Down

0 comments on commit dff24ee

Please sign in to comment.