开玩笑的测试用例失败-ReferenceError:未定义Office

时间:2020-04-30 13:27:02

标签: javascript reactjs jestjs office-js office-fabric

我正在将React-fabric UI与Office.js一起使用来创建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)"]

}

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

参考错误:未定义办公室

请尝试通过多种方法来解决此问题,但不能走运。

1 个答案:

答案 0 :(得分:1)

通过添加酶设置文件并向全局对象添加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 = () => ({});