Angular Unit Tests ReferenceError: type is not defined

Angular Unit Tests ReferenceError: type is not defined

我的 Angular 应用程序的单元测试存在一些问题,该应用程序旨在成为 Outlook 的加载项。

代码本身运行良好,没有错误,但测试经常失败。

我遇到的错误如下:

ReferenceError: Office is not defined

ReferenceError: fabric is not defined

任何使用 Office.js 或 Fabric 的方法的测试都会失败。因此,例如,如果方法如下:

  public getName() {
    this.item = Office.context.mailbox.item;
    return this.item.from.displayName;
  }

它会失败并出现未定义办公室的错误。

Office 和 Fabric 都通过 index.html 文件添加到应用程序:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Firefish Outlook Addin</title>

  <base href="/AddinClient/">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <link rel="stylesheet" href="https://static2.sharepointonline.com/files/fabric/office-ui-fabric-js/1.4.0/css/fabric.min.css" />
  <link rel="stylesheet" href="https://static2.sharepointonline.com/files/fabric/office-ui-fabric-js/1.4.0/css/fabric.components.min.css" />
  <script src="assets/js/fabric.js"></script>
  <script src="https://appsforoffice.microsoft.com/lib/1/hosted/Office.js" type="text/javascript"></script>
</head>
<body>
  <app-root></app-root>
</body>
</html>

特别是 office 在 main.ts 文件中初始化:

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './modules/app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

Office.initialize = function () {
  platformBrowserDynamic()
    .bootstrapModule(AppModule)
    .catch(err => console.error(err));
};

我一直在网上寻找可能的解决方案,很遗憾找不到任何解决方案。我的 Karma.conf.js 文件如下:

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, '../coverage'),
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true
    },
    Files: ["https://appsforoffice.microsoft.com/lib/1/hosted/Office.js"],
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

我的 tsconfig.spec.json 文件如下:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "types": [
      "jasmine",
      "node",
      "@types/office-js"
    ]
  },

  "files": [
    "test.ts",
    "polyfills.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts",
    "../node_modules/office-ui-fabric-js/src/components/Dropdown/Dropdown.ts",
    "../node_modules/office-ui-fabric-js/src/components/Button/Button.ts",
    "../node_modules/office-ui-fabric-js/src/components/TextField/TextField.ts",
    "../node_modules/office-ui-fabric-js/src/components/RadioButton/RadioButton.ts",
    "../node_modules/office-ui-fabric-js/src/components/Spinner/Spinner.ts",
    "../node_modules/office-ui-fabric-js/src/components/ProgressIndicator/ProgressIndicator.ts"
  ]
}

所以我添加了对 Office 和 fabric 文件的必要引用。

在这种情况下,我需要创建 Office 和 fabric 的模拟吗?

所以我找到了解决这个问题的方法。

对于 fabric,因为我们正在使用它的本地副本,所以我可以在测试部分下的 angular.json 文件中添加对它的引用:

"test": {
  "builder": "@angular-devkit/build-angular:karma",
  "options": {
    "main": "src/test.ts",
    "polyfills": "src/polyfills.ts",
    "tsConfig": "src/tsconfig.spec.json",
    "karmaConfig": "src/karma.conf.js",
    "styles": [
      "src/styles.scss"
    ],
    "scripts": ["src/assets/js/fabric.js"],
    "assets": [
      "src/assets"
    ]
  }
}

对于 Office 问题,我在我的规范 class 中添加了以下内容以解决该问题:

const officeScript = 'https://appsforoffice.microsoft.com/lib/1/hosted/Office.js';
const node = document.createElement('script');
node.src = officeScript;
node.type = 'text/javascript';
node.async = false;
node.charset = 'utf-8';
document.getElementsByTagName('head')[0].appendChild(node);