使用卡扣测试GAS时如何模拟依赖项 LedgerScripts.test.js LedgerScripts.js 结果运行jest命令后 jest.config.js babel.config.js package.json 其他评论

如何解决使用卡扣测试GAS时如何模拟依赖项 LedgerScripts.test.js LedgerScripts.js 结果运行jest命令后 jest.config.js babel.config.js package.json 其他评论

背景

我最近了解了clasp,并对使用TDD在本地编辑我的Google Apps Scripts(GAS)的可能性感到兴奋。

注意:也许可以使用现有的GAS编辑器编写测试,但是我更愿意使用现代的编辑器:)

clasp可以很好地工作,但是我无法弄清楚如何模拟单元测试的依赖关系(尽管我很高兴使用任何有效的工具,但主要通过jest来>

  • 通过使用gas-local软件包,我走得最远,并且能够模拟测试中的单个依赖项
    • 但是我找不到在单个测试/调用中模拟多个依赖项的方法,因此我创建了this issue

挑战

  • 我是一位JavaScript新手,在研究此问题时可能错过了一些重要的事情:)
  • 尽管安装了@types/google-apps-script,但我不清楚如何分别使用ES5或ES2015语法来“要求”或“导入” Google Apps脚本模块。有关此示例,请参见下文。

相关的StackOverflow帖子

尽管在单元测试here中也有类似的SO问题,但大多数内容/评论似乎都来自拍前时代,在跟进其余线索时,我无法找到解决方案。 (当然,我未经训练的眼睛很可能错过了一些东西!)。

尝试

使用本地气体

如上所述,在尝试使用gas-local来模拟多个依赖项之后,我创建了一个问题(请参阅上面的链接)。我的配置与我在下面描述的jest.mock测试相似,但值得注意以下差异:

  • 我在gas-local测试中使用了ES5语法
  • 我的包裹配置可能略有不同

使用jest.mock

LedgerScripts.test.js

import { getSummaryHTML } from "./LedgerScripts.js";
import { SpreadsheetApp } from '../node_modules/@types/google-apps-script/google-apps-script.spreadsheet';

test('test a thing',() => {
    jest.mock('SpreadSheetApp',() => {
        return jest.fn().mockImplementation(() => { // Works and lets you check for constructor calls
          return { getActiveSpreadsheet: () => {} };
        });
      });
    SpreadsheetApp.mockResolvedValue('TestSpreadSheetName');

    const result = getSummaryHTML;
    expect(result).toBeInstanceOf(String);
});

LedgerScripts.js

//Generates the summary of transactions for embedding in email
function getSummaryHTML(){  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var dashboard = ss.getSheetByName("Dashboard");

  // Do other stuff  
  return "<p>some HTML would go here</p>"
}

export default getSummaryHTML;

结果(运行jest命令后)

Cannot find module '../node_modules/@types/google-apps-script/google-apps-script.spreadsheet' from 'src/LedgerScripts.test.js'

      1 | import { getSummaryHTML } from "./LedgerScripts.js";
    > 2 | import { SpreadsheetApp } from '../node_modules/@types/google-apps-script/google-apps-script.spreadsheet';
        | ^
      3 | 
      4 | test('test a thing',() => {
      5 |     jest.mock('SpreadSheetApp',() => {

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:307:11)
      at Object.<anonymous> (src/LedgerScripts.test.js:2:1)

作为参考,如果我转到具有所需类型的google-apps-script.spreadsheet.d.ts文件,则会在文件顶部看到以下声明...

declare namespace GoogleAppsScript {
  namespace Spreadsheet {

...而这个位于文件底部:

declare var SpreadsheetApp: GoogleAppsScript.Spreadsheet.SpreadsheetApp;

所以也许我只是错误地导入了SpreadsheetApp

其他文件

jest.config.js

module.exports = {
    
    clearMocks: true,moduleFileExtensions: [
      "js","json","jsx","ts","tsx","node"
    ],testEnvironment: "node",};

babel.config.js

module.exports = {
  presets: ["@babel/preset-env"],};

package.json

{
  "name": "ledger-scripts","version": "1.0.0","description": "","main": "index.js","scripts": {
    "test": "jest"
  },"author": "","license": "ISC","dependencies": {
    "@babel/core": "^7.11.1","@babel/preset-env": "^7.11.0","@types/google-apps-script": "^1.0.14","@types/node": "^14.0.27","babel-jest": "^26.3.0","commonjs": "0.0.1","eslint": "^7.6.0","eslint-plugin-jest": "^23.20.0","gas-local": "^1.3.1","requirejs": "^2.3.6"
  },"devDependencies": {
    "@types/jasmine": "^3.5.12","@types/jest": "^26.0.9","jest": "^26.3.0"
  }
}

解决方法

注意:您的问题范围很广,可能需要澄清。

clasp效果很好,但是我无法弄清楚如何模拟单元测试的依赖关系(主要是通过玩笑,尽管我很高兴使用任何有效的工具)

您不需要Jest或任何特定的测试框架即可模拟全局Apps脚本对象。

// LedgerScripts.test.js
import getSummaryHTML from "./LedgerScripts.js";

global.SpreadsheetApp = {
  getActiveSpreadsheet: () => ({
    getSheetByName: () => ({}),}),};

console.log(typeof getSummaryHTML() === "string");
$ node LedgerScripts.test.js
true

所以也许我只是错误地导入了SpreadsheetApp?

是的,将.d.ts导入Jest是不正确的。 Jest不需要SpreadsheetApp的TypeScript文件。您可以忽略它。 您只需为Jest稍微修改以上示例。

// LedgerScripts.test.js - Jest version
import getSummaryHTML from "./LedgerScripts";

global.SpreadsheetApp = {
  getActiveSpreadsheet: () => ({
    getSheetByName: () => ({}),};

test("summary returns a string",() => {
  expect(typeof getSummaryHTML()).toBe("string");
});

尽管安装了@ types / google-apps-script,但我不清楚如何使用ES5或ES2015语法“要求”或“导入” Google Apps脚本模块

@types/google-apps-script不包含模块,并且您不导入它们。这些是TypeScript declaration files。您的编辑器(如果支持TypeScript)将在后台读取这些文件,并且即使是普通的JavaScript文件,您也可以突然获得自动完成功能。

其他评论

  • 在这里检查函数是否返回字符串,也许只是为了使您的示例非常简单。但是,必须强调的是,此类测试最好留给TypeScript。
  • 自从您返回HTML字符串以来,我有义务指出Apps脚本出色的HTML Service和模板功能。
  • 单元测试还是集成测试?您提到了单元测试,但是依赖全局变量通常表示您可能不是单元测试。考虑重构您的函数,以便它们接收对象作为输入,而不是从全局范围调用它们。
  • 模块语法:如果使用export default foo,则在导入时不使用花括号:import foo from "foo.js",但是如果使用export function foo() {,则使用花括号:import { foo } from "foo.js"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-