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

fix(deps): update jest monorepo to v27 (major) #53620

Merged
merged 7 commits into from
Jul 6, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ describe( 'useWithServerCredentials HOC', () => {
// Verify the form was pre-filled with the current store
expect( formDataContainer.innerHTML ).toContain( 'jetpackUser' );
expect( formDataContainer.innerHTML ).toContain( 'jetpackHost' );
expect( formDataContainer.innerHTML ).toContain( 33 );
expect( formDataContainer.innerHTML ).toContain( '33' );
expect( formDataContainer.innerHTML ).toContain( '/jetpack/path' );

fireEvent.click( submitButton );
Expand Down
97 changes: 50 additions & 47 deletions client/server/lib/logger/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,61 +23,64 @@ afterEach( () => {
mockFs.restore();
} );

it( "Returns a logger with the name 'calypso'", () => {
const logger = getLogger();
describe( 'Logger', () => {
it( "Returns a logger with the name 'calypso'", () => {
const logger = getLogger();

logger.info( 'message' );
logger.info( 'message' );

expect( mockStdout ).toHaveBeenCalledTimes( 1 );
const loggedMessage = mockStdout.mock.calls[ 0 ][ 0 ];
expect( JSON.parse( loggedMessage ) ).toEqual( expect.objectContaining( { name: 'calypso' } ) );
} );
expect( mockStdout ).toHaveBeenCalledTimes( 1 );
const loggedMessage = mockStdout.mock.calls[ 0 ][ 0 ];
expect( JSON.parse( loggedMessage ) ).toEqual( expect.objectContaining( { name: 'calypso' } ) );
} );

it( 'Logs info and above levels to stdout', () => {
const logger = getLogger();
it( 'Logs info and above levels to stdout', () => {
const logger = getLogger();

logger.trace( 'trace' ); // not logged
logger.debug( 'debug' ); // not logged
logger.info( 'info' );
logger.warn( 'warn' );
logger.error( 'error' );
logger.fatal( 'fatal' );
logger.trace( 'trace' ); // not logged
logger.debug( 'debug' ); // not logged
logger.info( 'info' );
logger.warn( 'warn' );
logger.error( 'error' );
logger.fatal( 'fatal' );

const loggedMessages = mockStdout.mock.calls.map( ( args ) => JSON.parse( args[ 0 ] ) );
expect( loggedMessages ).toEqual( [
expect.objectContaining( { msg: 'info', level: 30 } ),
expect.objectContaining( { msg: 'warn', level: 40 } ),
expect.objectContaining( { msg: 'error', level: 50 } ),
expect.objectContaining( { msg: 'fatal', level: 60 } ),
] );
} );
const loggedMessages = mockStdout.mock.calls.map( ( args ) => JSON.parse( args[ 0 ] ) );
expect( loggedMessages ).toEqual( [
expect.objectContaining( { msg: 'info', level: 30 } ),
expect.objectContaining( { msg: 'warn', level: 40 } ),
expect.objectContaining( { msg: 'error', level: 50 } ),
expect.objectContaining( { msg: 'fatal', level: 60 } ),
] );
} );

it( 'Logs info and above levels to the filesystem when the env variable is present', async () => {
process.env.CALYPSO_LOGFILE = '/tmp/calypso.log';
const logger = getLogger();
it( 'Logs info and above levels to the filesystem when the env variable is present', async () => {
process.env.CALYPSO_LOGFILE = '/tmp/calypso.log';
const logger = getLogger();

logger.trace( 'trace' ); // not logged
logger.debug( 'debug' ); // not logged
logger.info( 'info' );
logger.warn( 'warn' );
logger.error( 'error' );
logger.fatal( 'fatal' );
logger.trace( 'trace' ); // not logged
logger.debug( 'debug' ); // not logged
logger.info( 'info' );
logger.warn( 'warn' );
logger.error( 'error' );
logger.fatal( 'fatal' );

const logLines = ( await fs.promises.readFile( '/tmp/calypso.log', 'utf8' ) )
.split( '\n' )
.filter( ( line ) => line.length > 0 )
.map( JSON.parse );
expect( logLines ).toEqual( [
expect.objectContaining( { msg: 'info', level: 30 } ),
expect.objectContaining( { msg: 'warn', level: 40 } ),
expect.objectContaining( { msg: 'error', level: 50 } ),
expect.objectContaining( { msg: 'fatal', level: 60 } ),
] );
} );
const logLines = ( await fs.promises.readFile( '/tmp/calypso.log', 'utf8' ) )
.split( '\n' )
.filter( ( line ) => line.length > 0 )
.map( JSON.parse );

expect( logLines ).toEqual( [
expect.objectContaining( { msg: 'info', level: 30 } ),
expect.objectContaining( { msg: 'warn', level: 40 } ),
expect.objectContaining( { msg: 'error', level: 50 } ),
expect.objectContaining( { msg: 'fatal', level: 60 } ),
] );
} );

it( 'Reuses the same logger', () => {
const logger1 = getLogger();
const logger2 = getLogger();
it( 'Reuses the same logger', () => {
const logger1 = getLogger();
const logger2 = getLogger();

expect( logger1 ).toEqual( logger2 );
expect( logger1 ).toEqual( logger2 );
} );
} );
189 changes: 95 additions & 94 deletions client/server/middleware/test/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const withEnv = ( env ) => {
} );

afterAll( () => {
config.reset();
config.mockReset();
} );
};

Expand All @@ -67,112 +67,113 @@ const simulateRequest = ( { req, res, delay, finished = true } ) => {
res.emit( 'close' );
};

beforeEach( () => {
jest.useFakeTimers( 'modern' );
} );

afterEach( () => {
jest.useRealTimers();
jest.clearAllMocks();
} );

it( 'Adds a `logger` property to the request with the request id', () => {
const req = fakeRequest();
describe( 'Logger middleware', () => {
withEnv( 'production' );
withCommitSha( 'abcd1234' );

simulateRequest( {
req,
res: fakeResponse(),
beforeEach( () => {
jest.useFakeTimers();
} );

expect( req.logger ).toBe( requestLogger );
expect( mockRootLogger.child ).toHaveBeenCalledWith( {
reqId: '00000000-0000-0000-0000-000000000000',
afterEach( () => {
jest.useRealTimers();
jest.clearAllMocks();
} );
} );

it( 'Logs info about the request', () => {
withEnv( 'production' );
it( 'Adds a `logger` property to the request with the request id', () => {
const req = fakeRequest();

simulateRequest( {
req: fakeRequest( {
method: 'GET',
httpVersion: '2.0',
headers: {
'user-agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
referer: 'https://wordpress.com',
},
url: '/example.html',
ip: '127.0.0.1',
} ),
res: fakeResponse( {
statusCode: '200',
headers: {
'content-length': 123,
},
} ),
delay: 100,
} );
simulateRequest( {
req,
res: fakeResponse(),
} );

expect( requestLogger.info ).toHaveBeenCalledWith(
{
length: 123,
duration: 100,
status: '200',
method: 'GET',
env: 'production',
url: '/example.html',
httpVersion: '2.0',
userAgent: 'Chrome 85',
rawUserAgent:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
remoteAddr: '127.0.0.1',
referrer: 'https://wordpress.com',
},
'request finished'
);
} );

it( 'Logs closed requests', () => {
simulateRequest( {
req: fakeRequest(),
res: fakeResponse(),
finished: false,
expect( req.logger ).toBe( requestLogger );
expect( mockRootLogger.child ).toHaveBeenCalledWith( {
reqId: '00000000-0000-0000-0000-000000000000',
} );
} );

expect( requestLogger.info ).toHaveBeenCalledWith( expect.anything(), 'request closed' );
} );

it( "Logs raw UserAgent if it can't be parsed", () => {
simulateRequest( {
req: fakeRequest( {
headers: {
'user-agent': 'A random browser',
},
} ),
res: fakeResponse(),
it( 'Logs info about the request', () => {
simulateRequest( {
req: fakeRequest( {
method: 'GET',
httpVersion: '2.0',
headers: {
'user-agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
referer: 'https://wordpress.com',
},
url: '/example.html',
ip: '127.0.0.1',
} ),
res: fakeResponse( {
statusCode: '200',
headers: {
'content-length': 123,
},
} ),
delay: 100,
} );

expect( requestLogger.info ).toHaveBeenCalledWith(
expect.objectContaining( {
length: 123,
duration: 100,
status: '200',
method: 'GET',
env: 'production',
url: '/example.html',
httpVersion: '2.0',
userAgent: 'Chrome 85',
rawUserAgent:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
remoteAddr: '127.0.0.1',
referrer: 'https://wordpress.com',
} ),
'request finished'
);
} );

expect( requestLogger.info ).toHaveBeenCalledWith(
expect.objectContaining( {
userAgent: 'A random browser',
} ),
expect.anything()
);
} );
it( 'Logs closed requests', () => {
simulateRequest( {
req: fakeRequest(),
res: fakeResponse(),
finished: false,
} );

it( 'Adds the COMMIT_SHA as version', () => {
withCommitSha( 'abcd1234' );
expect( requestLogger.info ).toHaveBeenCalledWith( expect.anything(), 'request closed' );
} );

simulateRequest( {
req: fakeRequest(),
res: fakeResponse(),
it( "Logs raw UserAgent if it can't be parsed", () => {
simulateRequest( {
req: fakeRequest( {
headers: {
'user-agent': 'A random browser',
},
} ),
res: fakeResponse(),
} );

expect( requestLogger.info ).toHaveBeenCalledWith(
expect.objectContaining( {
userAgent: 'A random browser',
} ),
expect.anything()
);
} );

expect( requestLogger.info ).toHaveBeenCalledWith(
expect.objectContaining( {
appVersion: 'abcd1234',
} ),
expect.anything()
);
it( 'Adds the COMMIT_SHA as version', () => {
simulateRequest( {
req: fakeRequest(),
res: fakeResponse(),
} );

expect( requestLogger.info ).toHaveBeenCalledWith(
expect.objectContaining( {
appVersion: 'abcd1234',
} ),
expect.anything()
);
} );
} );
3 changes: 0 additions & 3 deletions client/server/pages/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,6 @@ const assertSection = ( { url, entry, sectionName, sectionGroup } ) => {
// you are not supossed to log anything after the test is done.
//
// This timer should give time to that promise to fail within the test.
jest.useRealTimers();
setTimeout( done, 5 );
} ) );
} );
Expand Down Expand Up @@ -1300,8 +1299,6 @@ describe( 'main app', () => {
// you are not supossed to log anything after the test is done.
//
// This timer should give time to that promise to fail within the test.
// done();
jest.useRealTimers();
setTimeout( done, 5 );
} )
);
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
"@wordpress/npm-package-json-lint-config": "^4.0.3",
"@wordpress/url": "^2.22.2",
"autoprefixer": "^10.2.5",
"babel-jest": "^26.3.0",
"babel-jest": "^27.0.6",
"babel-loader": "^8.2.2",
"babel-plugin-dynamic-import-node": "^2.3.0",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
Expand Down Expand Up @@ -269,8 +269,8 @@
"i18n-calypso-cli": "^1.0.0",
"ignore-loader": "^0.1.2",
"imports-loader": "^0.8.0",
"jest": "^26.4.0",
"jest-environment-jsdom": "^26.6.2",
"jest": "^27.0.6",
"jest-environment-jsdom": "^27.0.6",
"jest-enzyme": "^7.1.2",
"jest-junit": "^9.0.0",
"lerna": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/calypso-build/jest/transform/babel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
const babelJest = require( 'babel-jest' );
const babelJest = require( 'babel-jest' ).default;
const path = require( 'path' );

module.exports = babelJest.createTransformer( {
Expand Down
Loading