Download wp-calypso/client/lib/explat/internals/test/log-error.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.88 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/lib/explat/internals/test/log-error.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/wp-calypso/client/lib/explat/internals/test/log-error.ts
-
curl -L -o log-error.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/lib/explat/internals/test/log-error.ts
1.88 kB
| // @ts-nocheck - TODO: Fix TypeScript issues | |
| import wpcom from 'calypso/lib/wp'; | |
| import * as Logger from 'calypso/server/lib/logger'; | |
| import { logError } from '../log-error'; | |
| jest.mock( 'calypso/lib/wp' ); | |
| jest.mock( 'calypso/server/lib/logger', () => { | |
| const logger = jest.fn(); | |
| return { | |
| getLogger: () => ( { error: logger } ), | |
| }; | |
| } ); | |
| function setSsrContext() { | |
| // eslint-disable-next-line @typescript-eslint/ban-ts-comment | |
| // @ts-ignore | |
| globalThis.window = undefined; | |
| } | |
| const mockWindow = { | |
| FormData: class { | |
| append( key: string, value: unknown ) { | |
| this[ key ] = value; | |
| } | |
| }, | |
| fetch: jest.fn(), | |
| }; | |
| function setBrowserContext() { | |
| // eslint-disable-next-line @typescript-eslint/ban-ts-comment | |
| // @ts-ignore | |
| globalThis.window = mockWindow; | |
| } | |
| beforeEach( () => { | |
| jest.resetAllMocks(); | |
| setBrowserContext(); | |
| } ); | |
| describe( 'logError', () => { | |
| it( 'should log to the server in SSR', () => { | |
| setSsrContext(); | |
| logError( { message: 'asdf', foo: 'bar' } ); | |
| expect( jest.mocked( Logger.getLogger().error ).mock.calls ).toMatchInlineSnapshot( ` | |
| Array [ | |
| Array [ | |
| Object { | |
| "message": "asdf", | |
| "properties": Object { | |
| "context": "explat", | |
| "explat_client": "calypso", | |
| "foo": "bar", | |
| "message": "asdf", | |
| }, | |
| }, | |
| ], | |
| ] | |
| ` ); | |
| } ); | |
| it( 'should log to the server in the browser', () => { | |
| logError( { message: 'asdf', foo: 'bar' } ); | |
| expect( jest.mocked( wpcom.req.post ).mock.calls ).toMatchInlineSnapshot( ` | |
| Array [ | |
| Array [ | |
| Object { | |
| "apiNamespace": "rest/v1.1", | |
| "body": Object { | |
| "error": "{\\"message\\":\\"asdf\\",\\"properties\\":{\\"foo\\":\\"bar\\",\\"context\\":\\"explat\\",\\"explat_client\\":\\"calypso\\",\\"message\\":\\"asdf\\"}}", | |
| }, | |
| "path": "/js-error", | |
| }, | |
| [Function], | |
| ], | |
| ] | |
| ` ); | |
| } ); | |
| } ); | |