Skip to content

Commit

Permalink
fix: add assert error content
Browse files Browse the repository at this point in the history
  • Loading branch information
zhoushaw committed Aug 5, 2021
1 parent f4bb6b3 commit db648aa
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
parser: '@typescript-eslint/parser',
ignorePatterns: [
'**/dist',
'dev',
'dev/',
'api-extractor.json',
'packages/runtime/*/dist',
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <reference types="cypress" />
import { matchAndMatch } from '../common';
import { findMultiAndMatch } from '../common';

const basename = '/garfish-app';

Expand All @@ -23,18 +23,43 @@ describe('monitoring the isolation', () => {
win.Garfish.router.push({ path: '/react/monitoring' });
cy.contains('[data-test=title]', MonitoringTitle);
cy.get('[data-test=click-error]').click();
matchAndMatch(
1,
{
ev_type: 'js_error',
},
{
// cy.get('[data-test=main-click-error]').click();
cy.get('[data-test=click-unhandledrejection-error]').click();
// cy.get('[data-test=main-click-unhandledrejection-error]').click();

const errorPatch = {
ev_type: 'js_error',
};

const errorMessageMap = {
mainAppError: 'mainApp: normal error',
mainUnhandledrejectionError: 'mainApp: unhandledrejection error',
subAppError: 'subApp: normal error',
subUnhandledrejectionError: 'subApp: unhandledrejection error',
};

const errMsg = (msg) => {
return {
payload: {
error: {
message: 'mainApp: normal error1',
message: msg,
},
},
},
};
};

// matchAndMatch(0, errorPatch, errMsg(errorMessageMap.mainAppError));
// matchAndMatch(0, errorPatch, errMsg(errorMessageMap.mainUnhandledrejectionError));
// // react-error-overlay Make the same error trigger twice
// matchAndMatch(2, errorPatch, errMsg(errorMessageMap.subAppError));
findMultiAndMatch(2, errorPatch, errMsg(errorMessageMap.subAppError), {
'payload.error.message': errorMessageMap.subAppError,
});
findMultiAndMatch(
1,
errorPatch,
errMsg(errorMessageMap.subUnhandledrejectionError),
{ 'payload.error.message': errorMessageMap.subUnhandledrejectionError },
);
});
});
Expand Down
15 changes: 9 additions & 6 deletions cypress/integration/common.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import matches from 'lodash/matches';

export function findMultiAndMatch(count, findFn, matchFn) {
export function findMultiAndMatch(count, findObj, matchObj, equalObj) {
let data = [];
function assert(body) {
data = data.concat(body?.list?.filter(findFn).filter(matchFn) || []);
data = data.concat(
body?.list?.filter(matches(findObj)).filter(matches(matchObj)) || [],
);
if (count > 0 && data.length > 0)
expect(data[0]).to.nested.include(equalObj);
expect(data.length).to.be.equal(count);
}
cy.wait('@post').its('request.body').then(assert);
}

export const matchAndMatch = matchMultiAndMatch.bind(null, 1);

export function matchMultiAndMatch(count, findObj, matchObj) {
findMultiAndMatch(count, matches(findObj), matches(matchObj));
export function matchMultiAndMatch(count, findObj, matchObj, equalObj) {
// findMultiAndMatch(count, matches(findObj), matches(matchObj));
findMultiAndMatch(count, findObj, matchObj, equalObj);
}
7 changes: 7 additions & 0 deletions dev/main/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@
<div id="VueApp">
<div>
<h1 align="center">Main project</h1>
<div style="text-align: center;">
<h3> main App content</h3>
<a data-test="main-click-fetch" onclick="window.fetchM()">fetch</a>
<a data-test="main-click-error" onclick="window.normalError()">error</a>
<a data-test="main-click-unhandledrejection-error" onclick="window.unhandledrejectionError()">unhandledrejectionError</a>
<a data-test="main-click-dynamic-resource" onclick="window.DynamicResource()">DynamicResource</a>
</div>
<h2 align="center">
<b id="vueBtn">vue</b>
<b id="reactBtn">react</b>
Expand Down
13 changes: 1 addition & 12 deletions dev/main/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import GarfishInstance, { interfaces } from 'garfish';
import {
fetchM,
unhandledrejectionError,
normalError,
DynamicResource,
} from './monitoring';
import './monitoring';

(window as any).__GARFISH_PARENT__ = true;

Expand Down Expand Up @@ -44,12 +39,6 @@ let defaultConfig: interfaces.Options = {
autoRefreshApp: true,
disablePreloadApp: true,
protectVariable: ['MonitoringInstance', 'Garfish'],
props: {
fetchM,
unhandledrejectionError,
normalError,
DynamicResource,
},
// sandbox: {
// snapshot: false
// },
Expand Down
26 changes: 18 additions & 8 deletions dev/main/src/monitoring.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { interfaces } from 'mocha';
import ConfigCommon from '../../config.json';

export function fetchM() {
declare global {
interface Window {
fetchM: Function;
unhandledrejectionError: Function;
normalError: Function;
DynamicResource: Function;
}
}

window.fetchM = function fetchM() {
const xhr = new XMLHttpRequest();
xhr.open('get', `http://localhost:${ConfigCommon.mainPort}/mainApp`, true);
xhr.onreadystatechange = function () {
Expand All @@ -16,24 +26,24 @@ export function fetchM() {
fetch(
`http://localhost:${ConfigCommon.mainPort}/fetch/mainApp`,
).then((res) => {});
}
};

export function unhandledrejectionError() {
window.unhandledrejectionError = function unhandledrejectionError() {
setTimeout(() => {
throw Error('mainApp: unhandledrejection error');
}, 2000);
}
};

export function normalError() {
window.normalError = function normalError() {
throw Error('mainApp: normal error');
}
};

export function DynamicResource() {
window.DynamicResource = function DynamicResource() {
const sc = document.createElement('script');
sc.src = `http://localhost:${ConfigCommon.mainPort}/monitoring/dynamicScript.js`;
document.body.appendChild(sc);

const link = document.createElement('link');
link.href = `http://localhost:${ConfigCommon.mainPort}/monitoring/dynamicLink.css`;
document.body.appendChild(link);
}
};
18 changes: 2 additions & 16 deletions dev/react/src/components/monitoring.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import { Button } from 'antd';
const reactPort = 2444;

export default function () {
setTimeout(() => {
window.Garfish.props.normalError();
});
function fetchM() {
// At the same time master application http request
if (window.__GARFISH__) window.Garfish.props.fetchM();
const xhr = new XMLHttpRequest();
xhr.open('get', `http://localhost:${reactPort}/subApp`, true);
xhr.onreadystatechange = function () {
Expand All @@ -25,25 +20,16 @@ export default function () {
}

function unhandledrejectionError() {
if (window.__GARFISH__) window.Garfish.props.unhandledrejectionError();
// At the same time master application error
setTimeout(() => {
throw Error('subApp: unhandledrejection error');
}, 2000);
}

function normalError() {
// setTimeout(()=>{
// throw Error('subApp: normal error');
// })
// At the same time master application error
if (window.__GARFISH__) window.Garfish.props.normalError();
throw Error('subApp: normal error');
}

function DynamicResource() {
if (window.__GARFISH__) window.Garfish.props.DynamicResource();

// At the same time master application inject dynamicResource
const sc = document.createElement('script');
sc.src = `http://localhost:${reactPort}/monitoring/dynamicScript.js`;
document.body.appendChild(sc);
Expand All @@ -66,7 +52,7 @@ export default function () {
</Button>
<br />
<Button
data-test="click-fetch"
data-test="click-dynamic-resource"
type="primary"
onClick={() => DynamicResource()}
style={{ marginBottom: '30px' }}
Expand Down

0 comments on commit db648aa

Please sign in to comment.