gpt4 book ai didi

unit-testing - 使用 CLASP 测试 GAS 时如何模拟依赖项

转载 作者:行者123 更新时间:2023-12-04 11:48:42 24 4
gpt4 key购买 nike

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

NOTE: there might be a way to write tests using the existing GAS editor, but I'd prefer to use a modern editor if at all possible


clasp 效果很好,但我不知道如何模拟单元测试的依赖项(主要通过 jest ,尽管我很高兴使用任何有效的工具)
  • 我使用 gas-local 走得最远包,并且能够在测试中模拟单个依赖项
  • 但是,我找不到在单个测试/调用中模拟多个依赖项的方法,因此我创建了 this issue


  • 挑战
    尽管安装 @types/google-apps-script ,我不清楚如何分别“要求”或“导入”Google Apps 脚本模块是使用 ES5 还是 ES2015 语法——请参见下面的说明。
    相关的 StackOverflow 帖子
    尽管在单元测试 here 上有一个类似的 SO 问题,大部分内容/评论似乎来自前扣时代,我无法在跟进其余线索时得出解决方案。 (当然,我未经训练的眼睛很可能漏掉了一些东西!)。
    尝试
    使用gas-local
    正如我上面提到的,在使用 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 配置文件
    module.exports = {

    clearMocks: true,
    moduleFileExtensions: [
    "js",
    "json",
    "jsx",
    "ts",
    "tsx",
    "node"
    ],
    testEnvironment: "node",
    };
    babel.config.js
    module.exports = {
    presets: ["@babel/preset-env"],
    };
    包.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 works great, but I cannot figure out how to mock dependencies for unit tests (primarily via jest, though I'm happy to use any tool that works)


    您不需要 Jest 或任何特定的测试框架来模拟全局 Apps 脚本对象。
    // LedgerScripts.test.js
    import getSummaryHTML from "./LedgerScripts.js";

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

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

    So maybe I am just importing SpreadsheetApp incorrectly?


    是的,导入 .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");
    });

    Despite installing @types/google-apps-script, I am unclear on how to "require" or "import" Google Apps Script modules whether using ES5 or ES2015 syntax

    @types/google-apps-script不包含模块,您不导入它们。这些是 TypeScript declaration files .您的编辑器(如果它支持 TypeScript)将在后台读取这些文件,并且突然之间您将能够获得自动完成功能,即使在纯 JavaScript 文件中也是如此。
    补充评论
  • 在这里您检查一个函数是否返回一个字符串,也许只是为了使您的示例非常简单。但是,必须强调的是,此类测试最好留给 TypeScript。
  • 既然你返回了一个 HTML 字符串,我觉得有必要指出优秀的 HTML Service和 Apps 脚本的模板能力。
  • 单元测试还是集成测试?您提到了单元测试,但依赖全局变量通常表明您可能不是在进行单元测试。考虑重构您的函数,以便它们接收对象作为输入,而不是从全局范围内调用它们。
  • 模块语法:如果你使用 export default foo , 然后不带花括号导入: import foo from "foo.js"但如果你使用 export function foo() {然后你使用花括号:import { foo } from "foo.js"
  • 关于unit-testing - 使用 CLASP 测试 GAS 时如何模拟依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63428746/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com