【问题标题】:Angular Unit Tests ReferenceError: type is not definedAngular 单元测试参考错误:类型未定义
【发布时间】:2018-11-21 11:16:57
【问题描述】:

我的 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的错误而失败。

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 模拟的案例吗?

【问题讨论】:

  • 你必须在测试文件中导入这两种类型
  • tests.ts 文件中,您应该添加您的依赖项。另外,在索引的头部使用脚本是一种不好的做法,您应该使用angular.json文件来导入JS脚本。

标签: angular typescript office-js office-ui-fabric


【解决方案1】:

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

对于织物,因为我们正在使用它的本地副本,所以我可以在测试部分下的 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 问题,我在我的规范类中添加了以下内容以解决该问题:

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);

【讨论】:

    猜你喜欢
    • 2018-06-05
    • 1970-01-01
    • 2012-11-25
    • 2019-04-18
    • 2014-11-16
    • 2022-01-11
    • 1970-01-01
    • 2021-09-22
    • 2023-03-14
    相关资源
    最近更新 更多