如何使用DefinitelyTyped UrlFetchApp进行测试

时间:2017-04-02 10:02:02

标签: typescript google-apps-script typescript-typings definitelytyped

我想在DefinitelyTyped中使用@types/google-apps-script测试UrlFetchApp。

Underscore.js从编译传递到测试,但UrlFetchApp不起作用。

来源

Github:https://github.com/ina6ra/tsdt-sample

的package.json

{
  "dependencies": {
    "mocha": "^3.2.0",
    "power-assert": "^1.4.2",
    "ts-node": "^3.0.2",
    "underscore": "^1.8.3"
  },
  "scripts": {
    "init": "yarn install;typings install",
    "tsc": "tsc --noEmit true",
    "test": "mocha --compilers ts:ts-node/register test/**/*.ts"
  }
}

typings.json

{
  "dependencies": {},
  "globalDependencies": {
    "google-apps-script/google-apps-script.base": "registry:dt/google-apps-script/google-apps-script.base#0.0.0+20160510192225",
    "google-apps-script/google-apps-script.types": "registry:dt/google-apps-script/google-apps-script.types#0.0.0+20160316171810",
    "google-apps-script/google-apps-script.url-fetch": "registry:dt/google-apps-script/google-apps-script.url-fetch#0.0.0+20160510192225",
    "mocha": "registry:dt/mocha#2.2.5+20170311011848",
    "underscore": "registry:dt/underscore#1.8.3+20161123125004"
  }
}

的src / urlfetchapp.ts

var UrlFetchApp: GoogleAppsScript.URL_Fetch.UrlFetchApp;

export function gasFetch(url:string) {
  var res:GoogleAppsScript.URL_Fetch.HTTPResponse = UrlFetchApp.fetch(url);
  return true;
}

测试/ UFA-test.ts

import * as assert from 'power-assert';
import * as gas from '../src/urlfetchapp';

describe('UrlFetchApp', () => {
    it('test for fetch', () => {
        assert.equal(gas.gasFetch('http://www.google.com'), true);
    });
});

配置

tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "sourceMap": false
    },
    "files": [
        "typings/index.d.ts",
        "src/underscore.ts",
        "src/urlfetchapp.ts"
    ],
    "exclude": [
        "node_modules/"
    ]
}

编译

$ yarn run tsc
yarn run v0.21.3
$ tsc --noEmit true 
✨  Done in 2.94s.

编译过程没有错误。

测试

$ yarn run test
yarn run v0.21.3
$ mocha --compilers ts:ts-node/register test/**/*.ts 

  UrlFetchApp
    1) test for fetch

  Underscore.js
    ✓ test for maxList

  1 passing (29ms)
  1 failing

  1) UrlFetchApp test for fetch:
     TypeError: Cannot read property 'fetch' of undefined
      at Object.gasFetch (src/urlfetchapp.ts:4:64)
      at Context.<anonymous> (test/ufa-test.ts:6:26)

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

测试失败。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

您的测试正在本地计算机上运行,​​并且您的本地环境中没有全局UrlFetchApp对象。

var UrlFetchApp: GoogleAppsScript.URL_Fetch.UrlFetchApp; // <-- this object has not been initialized

export function gasFetch(url:string) {
  var res:GoogleAppsScript.URL_Fetch.HTTPResponse = UrlFetchApp.fetch(url); // <-- `UrlFetchApp` is undefined for your local environment
  return true;
}
  

有关详细信息,请参阅此处how to unit test google apps scripts?