背景
我最近了解到CLASP并对使用的可能性感到兴奋TDD编辑我的谷歌应用脚本(天然气)本地。
NOTE:可能有一种方法可以使用现有的 GAS 编辑器编写测试,但如果可能的话,我更愿意使用现代编辑器
clasp 效果很好,但我无法弄清楚如何模拟单元测试的依赖关系(主要通过jest,尽管我很乐意使用任何有效的工具)
- I got farthest by using the gas-local package, and was able to mock a single dependency within a test
- 但是,我找不到在单个测试/调用中模拟多个依赖项的方法,因此我创建了这个问题
挑战
尽管安装了@types/google-apps-script,我不清楚如何分别使用 ES5 或 ES2015 语法“要求”或“导入”Google Apps 脚本模块 - 请参阅下面的说明。
相关 StackOverflow 帖子
虽然单元测试也有类似的SO问题here,大多数内容/评论似乎来自前扣环时代,我在跟踪剩余线索时无法找到解决方案。 (当然,我未经训练的眼睛很可能错过了一些东西!)。
Attempts
使用本地天然气
正如我上面提到的,在使用gas-local时尝试模拟多个依赖项后,我创建了一个问题(请参阅上面的链接)。我的配置类似于jest.mock
我在下面描述了测试,但值得注意的是以下差异:
- 我使用 ES5 语法
gas-local
tests
- 我的包配置可能略有不同
使用 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
错误地?
其他文件
笑话配置.js
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"
}
}