Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop 6.2.x #17648

Merged
merged 13 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions core-libs/setup/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spartacus/setup",
"version": "6.2.0-1",
"version": "6.2.1",
"description": "Includes features that makes Spartacus and it's setup easier and streamlined.",
"keywords": [
"spartacus",
Expand All @@ -20,10 +20,10 @@
},
"peerDependencies": {
"@angular/core": "^15.2.4",
"@spartacus/cart": "6.2.0-1",
"@spartacus/core": "6.2.0-1",
"@spartacus/order": "6.2.0-1",
"@spartacus/user": "6.2.0-1"
"@spartacus/cart": "6.2.1",
"@spartacus/core": "6.2.1",
"@spartacus/order": "6.2.1",
"@spartacus/user": "6.2.1"
},
"optionalDependencies": {
"@angular/platform-server": "^15.2.4",
Expand Down
4 changes: 2 additions & 2 deletions core-libs/setup/ssr/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
* SPDX-License-Identifier: Apache-2.0
*/

export * from './loggers';
export * from './services';
export * from './loggers/index';
export * from './services/index';
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import * as angularCore from '@angular/core';
import { Request } from 'express';
import { DefaultExpressServerLogger } from './default-express-server-logger';
import { ExpressServerLoggerContext } from './express-server-logger';

const request = {
originalUrl: 'test',
res: {
locals: {
cx: {
request: { uuid: 'test', timeReceived: new Date('2023-05-26') },
},
},
},
} as unknown as Request;

describe('DefaultExpressServerLogger', () => {
let logger: DefaultExpressServerLogger;
Expand All @@ -21,7 +32,7 @@ describe('DefaultExpressServerLogger', () => {
logger.log('test', { request: {} as Request });

expect(logSpy).toHaveBeenCalledWith(
logger['createLogMessage']('test', { request: {} as Request })
logger['stringifyWithContext']('test', { request: {} as Request })
);
});

Expand All @@ -31,7 +42,7 @@ describe('DefaultExpressServerLogger', () => {
logger.warn('test', { request: {} as Request });

expect(warnSpy).toHaveBeenCalledWith(
logger['createLogMessage']('test', { request: {} as Request })
logger['stringifyWithContext']('test', { request: {} as Request })
);
});

Expand All @@ -43,7 +54,7 @@ describe('DefaultExpressServerLogger', () => {
logger.error('test', { request: {} as Request });

expect(errorSpy).toHaveBeenCalledWith(
logger['createLogMessage']('test', { request: {} as Request })
logger['stringifyWithContext']('test', { request: {} as Request })
);
});

Expand All @@ -53,7 +64,7 @@ describe('DefaultExpressServerLogger', () => {
logger.info('test', { request: {} as Request });

expect(infoSpy).toHaveBeenCalledWith(
logger['createLogMessage']('test', { request: {} as Request })
logger['stringifyWithContext']('test', { request: {} as Request })
);
});

Expand All @@ -65,30 +76,270 @@ describe('DefaultExpressServerLogger', () => {
logger.debug('test', { request: {} as Request });

expect(debugSpy).toHaveBeenCalledWith(
logger['createLogMessage']('test', { request: {} as Request })
logger['stringifyWithContext']('test', { request: {} as Request })
);
});

describe('is not dev mode', () => {
beforeEach(() => {
jest.spyOn(angularCore, 'isDevMode').mockReturnValue(false);
});

it('should log proper shape of the JSON', () => {
const debugSpy = jest
.spyOn(console, 'log')
.mockImplementation(() => {});

logger.log('test', { request });

expect(debugSpy.mock.lastCall).toMatchInlineSnapshot(`
[
"{"message":"test","context":{"timestamp":"2023-05-26T00:00:00.000Z","request":{"url":"test","uuid":"test","timeReceived":"2023-05-26T00:00:00.000Z"}}}",
]
`);
});

it('should warn proper shape of the JSON', () => {
const debugSpy = jest
.spyOn(console, 'warn')
.mockImplementation(() => {});

logger.warn('test', { request });

expect(debugSpy.mock.lastCall).toMatchInlineSnapshot(`
[
"{"message":"test","context":{"timestamp":"2023-05-26T00:00:00.000Z","request":{"url":"test","uuid":"test","timeReceived":"2023-05-26T00:00:00.000Z"}}}",
]
`);
});

it('should error proper shape of the JSON', () => {
const debugSpy = jest
.spyOn(console, 'error')
.mockImplementation(() => {});

logger.error('test', { request });

expect(debugSpy.mock.lastCall).toMatchInlineSnapshot(`
[
"{"message":"test","context":{"timestamp":"2023-05-26T00:00:00.000Z","request":{"url":"test","uuid":"test","timeReceived":"2023-05-26T00:00:00.000Z"}}}",
]
`);
});

it('should info proper shape of the JSON', () => {
const request = {
originalUrl: 'test',
res: {
locals: {
cx: {
request: { uuid: 'test', timeReceived: new Date() },
},
},
},
} as unknown as Request;

const debugSpy = jest
.spyOn(console, 'info')
.mockImplementation(() => {});

logger.info('test', { request });

expect(debugSpy.mock.lastCall).toMatchInlineSnapshot(`
[
"{"message":"test","context":{"timestamp":"2023-05-26T00:00:00.000Z","request":{"url":"test","uuid":"test","timeReceived":"2023-05-26T00:00:00.000Z"}}}",
]
`);
});

it('should debug proper shape of the JSON', () => {
const debugSpy = jest
.spyOn(console, 'debug')
.mockImplementation(() => {});

logger.debug('test', { request });

expect(debugSpy.mock.lastCall).toMatchInlineSnapshot(`
[
"{"message":"test","context":{"timestamp":"2023-05-26T00:00:00.000Z","request":{"url":"test","uuid":"test","timeReceived":"2023-05-26T00:00:00.000Z"}}}",
]
`);
});
});

describe('is dev mode', () => {
beforeEach(() => {
jest.spyOn(angularCore, 'isDevMode').mockReturnValue(true);
});

it('should log proper shape of the JSON', () => {
const debugSpy = jest
.spyOn(console, 'log')
.mockImplementation(() => {});

logger.log('test', { request });

expect(debugSpy.mock.lastCall).toMatchInlineSnapshot(`
[
"{
"message": "test",
"context": {
"timestamp": "2023-05-26T00:00:00.000Z",
"request": {
"url": "test",
"uuid": "test",
"timeReceived": "2023-05-26T00:00:00.000Z"
}
}
}",
]
`);
});

it('should warn proper shape of the JSON', () => {
const debugSpy = jest
.spyOn(console, 'warn')
.mockImplementation(() => {});

logger.warn('test', { request });

expect(debugSpy.mock.lastCall).toMatchInlineSnapshot(`
[
"{
"message": "test",
"context": {
"timestamp": "2023-05-26T00:00:00.000Z",
"request": {
"url": "test",
"uuid": "test",
"timeReceived": "2023-05-26T00:00:00.000Z"
}
}
}",
]
`);
});

it('should error proper shape of the JSON', () => {
const debugSpy = jest
.spyOn(console, 'error')
.mockImplementation(() => {});

logger.error('test', { request });

expect(debugSpy.mock.lastCall).toMatchInlineSnapshot(`
[
"{
"message": "test",
"context": {
"timestamp": "2023-05-26T00:00:00.000Z",
"request": {
"url": "test",
"uuid": "test",
"timeReceived": "2023-05-26T00:00:00.000Z"
}
}
}",
]
`);
});

it('should info proper shape of the JSON', () => {
const debugSpy = jest
.spyOn(console, 'info')
.mockImplementation(() => {});

logger.info('test', { request });

expect(debugSpy.mock.lastCall).toMatchInlineSnapshot(`
[
"{
"message": "test",
"context": {
"timestamp": "2023-05-26T00:00:00.000Z",
"request": {
"url": "test",
"uuid": "test",
"timeReceived": "2023-05-26T00:00:00.000Z"
}
}
}",
]
`);
});

it('should debug proper shape of the JSON', () => {
const debugSpy = jest
.spyOn(console, 'debug')
.mockImplementation(() => {});

logger.debug('test', { request });

expect(debugSpy.mock.lastCall).toMatchInlineSnapshot(`
[
"{
"message": "test",
"context": {
"timestamp": "2023-05-26T00:00:00.000Z",
"request": {
"url": "test",
"uuid": "test",
"timeReceived": "2023-05-26T00:00:00.000Z"
}
}
}",
]
`);
});
});
});

describe('create log message', () => {
it('should return message without request', () => {
const logMessage = logger['createLogMessage'](
'test',
{} as ExpressServerLoggerContext
);
const logMessage = logger['stringifyWithContext']('test', {});

expect(logMessage).not.toContain('request');
});

it('should return message with request', () => {
const logMessage = logger['createLogMessage']('test', {
const logMessage = logger['stringifyWithContext']('test', {
request: {} as Request,
});

expect(logMessage).toContain('request');
});
});

describe('map context', () => {
it('should return context without request', () => {
const context = logger['mapContext']({
options: {},
});

expect(context).toMatchInlineSnapshot(`
{
"options": {},
"timestamp": "2023-05-26T00:00:00.000Z",
}
`);
});

it('should return context with request', () => {
const context = logger['mapContext']({ request });

expect(context).toMatchInlineSnapshot(`
{
"request": {
"timeReceived": 2023-05-26T00:00:00.000Z,
"url": "test",
"uuid": "test",
},
"timestamp": "2023-05-26T00:00:00.000Z",
}
`);
});
});

describe('map request', () => {
it('should return mapped request', () => {
const request = {
Expand All @@ -106,7 +357,7 @@ describe('DefaultExpressServerLogger', () => {

expect(mappedRequest).toEqual({
url: 'test',
...request.res.locals.cx.request,
...request.res?.locals.cx.request,
});
});
});
Expand Down
Loading
Loading