1

我正在使用带有 Office.js 的 React-fabric UI 来创建 Excel 插件。在 CDN 下用于在 index.html 文件中导入 office.js。

<script
            src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"
            type="text/javascript"
        ></script>

下面是我的 jest.config.json

{
"testEnvironment": "node",
"verbose": true,
"files": ["https://appsforoffice.microsoft.com/lib/1/hosted/office.js"],
"setupFilesAfterEnv": ["<rootDir>/setupTests.js"],
"moduleFileExtensions": ["js", "jsx", "json"],
"transform": {
    "^.+\\.(t|j)s$": "babel-jest"
},
"moduleDirectories": ["node_modules", "<rootDir>/app/src"],
"testPathIgnorePatterns": [
    "Office",
    "node_modules",
    "<rootDir>/app/src/styles",
    "<rootDir>/app/images"
],
"moduleNameMapper": {
    "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/app/__mocks__/fileMock.js",
    "\\.(css|sass|scss)$": "<rootDir>/app/__mocks__/styleMock.js",
    "office-ui-fabric-react/lib/(.*)$": "office-ui-fabric-react/lib-commonjs/$1",
    "alias": {
        "^components(.*)$": "<rootDir>/app/src/components/$1",
        "^utils/(.*)$": "<rootDir>/app/src/utils/$1",
        "^styles/(.*)$": "<rootDir>/app/src/styles/$1",
        "^@[/](.+)": "<rootDir>/app/src"
    }
},
"testMatch": ["**/__tests__/**/*.js?(x)"]

}

我的测试用例失败并出现以下错误:

ReferenceError: Office 未定义

请帮助解决此问题,因为尝试了多种方法来解决此问题,但没有运气。

4

1 回答 1

3

它通过添加酶设置文件并将Office键添加到全局对象和箭头函数作为值来工作。如下所示:setupTest.js

import React from 'react';
import Adapter from 'enzyme-adapter-react-16';
import { configure, shallow, render, mount } from 'enzyme';

configure({ adapter: new Adapter() });

global.React = React;
global.shallow = shallow;
global.render = render;
global.mount = mount;
global.Office = () => ({});
global.Office.onReady = () => ({});
于 2020-04-30T15:03:49.940 回答