Skip to content

Commit

Permalink
Develop 6.2.x (#17648)
Browse files Browse the repository at this point in the history
Co-authored-by: i333055 <[email protected]>
Co-authored-by: luoyifeng11 <[email protected]>
Co-authored-by: Paweł Fraś <[email protected]>
Co-authored-by: Scar Ai <[email protected]>
Co-authored-by: Grace Dong <[email protected]>
Co-authored-by: niehuayang <[email protected]>
Co-authored-by: Christoph Hinssen <[email protected]>
Co-authored-by: Krzysztof Platis <[email protected]>
Co-authored-by: anjana-bl <[email protected]>
Co-authored-by: Radhep Sabapathipillai <[email protected]>
Co-authored-by: Hak Woo Kim <[email protected]>
Co-authored-by: Hakwoo Kim <[email protected]>
  • Loading branch information
13 people authored Jul 17, 2023
1 parent c29cd18 commit 0f3f22c
Show file tree
Hide file tree
Showing 81 changed files with 1,289 additions and 687 deletions.
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';
275 changes: 263 additions & 12 deletions core-libs/setup/ssr/logger/loggers/default-express-server-logger.spec.ts
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

0 comments on commit 0f3f22c

Please sign in to comment.