2022-07-15 11:53:06 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { bind } from 'jest-each';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-03-08 19:07:29 +00:00
|
|
|
import {
|
|
|
|
sendFailedTestScreenshotToSlack,
|
2021-03-08 23:22:55 +00:00
|
|
|
sendFailedTestMessageToSlack,
|
|
|
|
} from '../slack';
|
2021-04-14 13:05:03 +00:00
|
|
|
const { takeScreenshotFor } = require( '../../utils' );
|
2021-03-08 19:07:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the test case method so we can take screenshots of assertion failures.
|
|
|
|
*
|
|
|
|
* See: https://github.com/smooth-code/jest-puppeteer/issues/131#issuecomment-469439666
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We need to reference the original version of Jest.
|
|
|
|
*/
|
|
|
|
const originalDescribe = global.describe;
|
|
|
|
const originalIt = global.it;
|
|
|
|
|
2021-03-08 23:22:55 +00:00
|
|
|
/**
|
|
|
|
* A custom describe function that stores the name of the describe block.
|
2022-03-25 08:24:13 +00:00
|
|
|
*
|
2021-03-08 23:22:55 +00:00
|
|
|
* @type {describe}
|
|
|
|
*/
|
2022-03-25 08:24:13 +00:00
|
|
|
global.describe = ( () => {
|
2021-03-08 19:07:29 +00:00
|
|
|
const describe = ( blockName, callback ) => {
|
|
|
|
try {
|
|
|
|
originalDescribe( blockName, callback );
|
|
|
|
} catch ( e ) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const only = ( blockName, callback ) => {
|
|
|
|
originalDescribe.only( blockName, callback );
|
|
|
|
};
|
|
|
|
const skip = ( blockName, callback ) => {
|
|
|
|
originalDescribe.skip( blockName, callback );
|
|
|
|
};
|
|
|
|
|
|
|
|
describe.each = bind( describe, false );
|
|
|
|
only.each = bind( only, false );
|
|
|
|
skip.each = bind( skip, false );
|
|
|
|
describe.only = only;
|
|
|
|
describe.skip = skip;
|
|
|
|
|
|
|
|
return describe;
|
2022-03-25 08:24:13 +00:00
|
|
|
} )();
|
2021-03-08 19:07:29 +00:00
|
|
|
|
2021-03-08 23:22:55 +00:00
|
|
|
/**
|
|
|
|
* A custom it function that wraps the test function in a callback
|
|
|
|
* which takes a screenshot on test failure.
|
|
|
|
*
|
|
|
|
* @type {function(*=, *=): *}
|
|
|
|
*/
|
2022-03-25 08:24:13 +00:00
|
|
|
global.it = ( () => {
|
2021-03-08 19:07:29 +00:00
|
|
|
const test = async ( testName, callback ) => {
|
2021-03-09 20:49:47 +00:00
|
|
|
const testCallback = async () => screenshotTest( testName, callback );
|
2021-03-08 19:07:29 +00:00
|
|
|
return originalIt( testName, testCallback );
|
|
|
|
};
|
2021-03-09 14:19:43 +00:00
|
|
|
const only = ( testName, callback ) => {
|
|
|
|
return originalIt.only( testName, callback );
|
2021-03-08 19:07:29 +00:00
|
|
|
};
|
2021-03-09 14:19:43 +00:00
|
|
|
const skip = ( testName, callback ) => {
|
|
|
|
return originalIt.skip( testName, callback );
|
2021-03-08 19:07:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
test.each = bind( test, false );
|
|
|
|
only.each = bind( only, false );
|
|
|
|
skip.each = bind( skip, false );
|
|
|
|
test.only = only;
|
|
|
|
test.skip = skip;
|
|
|
|
|
|
|
|
return test;
|
2022-03-25 08:24:13 +00:00
|
|
|
} )();
|
2021-03-08 19:07:29 +00:00
|
|
|
|
2021-03-08 23:22:55 +00:00
|
|
|
/**
|
|
|
|
* Save a screenshot during a test if the test fails.
|
2022-03-25 08:24:13 +00:00
|
|
|
*
|
2022-07-15 11:53:06 +00:00
|
|
|
* @param {string} testName
|
|
|
|
* @param {Function} callback
|
2022-03-25 08:24:13 +00:00
|
|
|
* @return {Promise<void>}
|
2021-03-08 23:22:55 +00:00
|
|
|
*/
|
2021-03-09 20:49:47 +00:00
|
|
|
const screenshotTest = async ( testName, callback ) => {
|
2021-03-08 19:07:29 +00:00
|
|
|
try {
|
|
|
|
await callback();
|
|
|
|
} catch ( e ) {
|
2021-04-14 13:05:03 +00:00
|
|
|
const { title, filePath } = await takeScreenshotFor( testName );
|
|
|
|
await sendFailedTestMessageToSlack( title );
|
|
|
|
if ( filePath ) {
|
|
|
|
await sendFailedTestScreenshotToSlack( filePath );
|
|
|
|
}
|
2021-03-08 19:07:29 +00:00
|
|
|
|
2022-03-25 08:24:13 +00:00
|
|
|
throw e;
|
2021-03-08 19:07:29 +00:00
|
|
|
}
|
|
|
|
};
|