Har*_*516 5 javascript unit-testing reactjs jestjs azure-application-insights
我正在尝试在我的 React 应用程序中使用 Microsoft 的 Application Insights JavaScript SDK React 插件,尽管它运行成功,但我无法让我的 Jest 和 Enzyme 单元测试通过它。
我已经设置了我的单元测试,如下所示:
import React from 'react';
import {act} from 'react-dom/test-utils';
import ReactDOM from 'react-dom';
import App from '../App.view';
jest.mock('@microsoft/applicationinsights-react-js', () => ({
ReactPlugin: Object,
}));
jest.mock('@microsoft/applicationinsights-web', () => ({
ApplicationInsights: Object,
loadAppInsights: () => ({}),
}));
describe('App View', () => {
it('renders without crashing', () => {
const div = document.createElement('div');
act(() => {
ReactDOM.render(<App />, div);
});
ReactDOM.unmountComponentAtNode(div);
});
});
Run Code Online (Sandbox Code Playgroud)
使用我的应用程序洞察服务:
import {ApplicationInsights} from '@microsoft/applicationinsights-web';
import {ReactPlugin, withAITracking} from '@microsoft/applicationinsights-react-js';
import {createBrowserHistory} from 'history';
// Set up AppInsights connection
const browserHistory = createBrowserHistory({basename: ''});
const reactPlugin = new ReactPlugin();
const ai = new ApplicationInsights({
config: {
instrumentationKey:
process.env.REACT_APP_APPINSIGHTS_KEY || 'xxxxxxxxxxx-xxxxxxxxxx-xxxxxxx-xxxxx',
extensions: [reactPlugin],
extensionConfig: {
[reactPlugin.identifier]: {history: browserHistory},
},
},
});
ai.loadAppInsights();
export default Component => withAITracking(reactPlugin, Component);
export const appInsights = ai.appInsights;
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,我不断收到错误消息, TypeError: ai.loadAppInsights is not a function
由于我对应用程序洞察模块使用了 jest 模拟,因此我也尝试以多种方式模拟此方法,但没有成功。关于我在这里缺少什么的任何想法?我也找不到关于如何将应用程序洞察正确集成到 React 应用程序的任何好的文档,其中包括正确的单元测试。
提前致谢!
小智 0
我尝试了这个模拟并且它有效,
jest.mock('@microsoft/applicationinsights-web', () => ({
ApplicationInsights: jest.fn().mockImplementation(() => ({
loadAppInsights: jest.fn(),
addTelemetryInitializer: jest.fn(),
trackPageView: jest.fn(),
trackException: jest.fn(),
trackTrace: jest.fn(),
trackEvent: jest.fn(),
flush: jest.fn(),
startTrackPage: jest.fn(),
stopTrackPage: jest.fn(),
})),
}));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
756 次 |
| 最近记录: |