@ciderjs/vitest-plugin-gas-mock 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +96 -0
- package/dist/index.cjs +26 -0
- package/dist/index.d.cts +13 -0
- package/dist/index.d.mts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.mjs +22 -0
- package/dist/setup.cjs +92859 -0
- package/dist/setup.d.cts +2 -0
- package/dist/setup.d.mts +2 -0
- package/dist/setup.d.ts +2 -0
- package/dist/setup.mjs +92857 -0
- package/dist/shared/vitest-plugin-gas-mock.DDSZt7OP.mjs +12 -0
- package/dist/shared/vitest-plugin-gas-mock.DyNb5Y_N.cjs +15 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 luth
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# @ciderjs/vitest-plugin-gas-mock
|
|
2
|
+
|
|
3
|
+
[](LICENSE)
|
|
4
|
+
[](https://www.npmjs.com/package/@ciderjs/vitest-plugin-gas-mock)
|
|
5
|
+
[](https://www.npmjs.com/package/@ciderjs/vitest-plugin-gas-mock)
|
|
6
|
+
[](https://github.com/luthpg/vitest-plugin-gas-mock/issues)
|
|
7
|
+
|
|
8
|
+
Vitest plugin to mock Google Apps Script (GAS) environment.
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- **Automatic Mocking**: Dynamically generates mocks based on official `@types/google-apps-script`.
|
|
13
|
+
- **Method Chaining**: Supports GAS fluent APIs (e.g., `SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1')`).
|
|
14
|
+
- **Global Injection**: Automatically injects mocks into the global scope in tests.
|
|
15
|
+
- **`mockChain` Helper**: Easily override return values for complex method paths.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pnpm add -D @ciderjs/vitest-plugin-gas-mock
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Setup
|
|
24
|
+
|
|
25
|
+
Add the plugin to your `vitest.config.ts`:
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { defineConfig } from 'vitest/config';
|
|
29
|
+
import { mockGas } from '@ciderjs/vitest-plugin-gas-mock';
|
|
30
|
+
|
|
31
|
+
export default defineConfig({
|
|
32
|
+
plugins: [mockGas()],
|
|
33
|
+
test: {
|
|
34
|
+
globals: true, // Required for global injection
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
### Basic Usage
|
|
42
|
+
|
|
43
|
+
Once configured, GAS services like `SpreadsheetApp`, `DriveApp`, etc., are available globally in your tests.
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { it, expect } from 'vitest';
|
|
47
|
+
|
|
48
|
+
it('should mock SpreadsheetApp', () => {
|
|
49
|
+
const ss = SpreadsheetApp.getActiveSpreadsheet();
|
|
50
|
+
expect(ss).toBeDefined();
|
|
51
|
+
|
|
52
|
+
const sheet = ss.getSheetByName('MySheet');
|
|
53
|
+
expect(sheet).toBeDefined();
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Overriding Return Values with `mockChain`
|
|
58
|
+
|
|
59
|
+
Use `mockChain` to set return values for specific method call paths.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { it, expect } from 'vitest';
|
|
63
|
+
import { mockChain } from '@ciderjs/vitest-plugin-gas-mock';
|
|
64
|
+
|
|
65
|
+
it('should return specific value for a chain', () => {
|
|
66
|
+
const mockValue = "Hello from mock";
|
|
67
|
+
|
|
68
|
+
// Set the override
|
|
69
|
+
mockChain(
|
|
70
|
+
SpreadsheetApp,
|
|
71
|
+
'SpreadsheetApp.getActiveSpreadsheet.getName',
|
|
72
|
+
mockValue
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const name = SpreadsheetApp.getActiveSpreadsheet().getName();
|
|
76
|
+
expect(name).toBe(mockValue);
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## How it works
|
|
81
|
+
|
|
82
|
+
The plugin performs the following steps:
|
|
83
|
+
|
|
84
|
+
1. **Analyze**: Parses GAS type definitions to build a map of classes, methods, and return types.
|
|
85
|
+
2. **Generate**: Creates a runtime map that identifies which methods are "chainable" (i.e., return another GAS class).
|
|
86
|
+
3. **Inject**: Uses a Proxy-based factory to create spies (`vi.fn()`) that automatically return new proxies for chainable methods.
|
|
87
|
+
|
|
88
|
+
## Scripts
|
|
89
|
+
|
|
90
|
+
- `pnpm generate`: Refreshes the internal GAS API map (runs automatically before build/test).
|
|
91
|
+
- `pnpm build`: Builds the plugin.
|
|
92
|
+
- `pnpm test`: Runs integration and unit tests.
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
|
|
96
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const node_path = require('node:path');
|
|
4
|
+
const node_url = require('node:url');
|
|
5
|
+
const helpers = require('./shared/vitest-plugin-gas-mock.DyNb5Y_N.cjs');
|
|
6
|
+
|
|
7
|
+
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
8
|
+
const __dirname$1 = node_path.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))));
|
|
9
|
+
function mockGas() {
|
|
10
|
+
const setupFilePath = node_path.resolve(__dirname$1, "setup");
|
|
11
|
+
return {
|
|
12
|
+
name: "vite-plugin-gasmock",
|
|
13
|
+
config: (config) => {
|
|
14
|
+
const setupFiles = config.test?.setupFiles || [];
|
|
15
|
+
const newSetupFiles = Array.isArray(setupFiles) ? [...setupFiles, setupFilePath] : [setupFiles, setupFilePath];
|
|
16
|
+
return {
|
|
17
|
+
test: {
|
|
18
|
+
setupFiles: newSetupFiles
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
exports.mockChain = helpers.mockChain;
|
|
26
|
+
exports.mockGas = mockGas;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Plugin } from 'vitest/config';
|
|
2
|
+
|
|
3
|
+
declare function mockGas(): Plugin;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 特定のメソッドチェーンの戻り値を指定した値に設定する
|
|
7
|
+
* @param root モックのルートオブジェクト(現在は使用していませんが、API互換性のために残しています)
|
|
8
|
+
* @param path 文字列によるパス記法 (例: "SpreadsheetApp.getActiveSpreadsheet.getSheetByName")
|
|
9
|
+
* @param value 返すべき値
|
|
10
|
+
*/
|
|
11
|
+
declare function mockChain(_root: any, path: string, value: any): void;
|
|
12
|
+
|
|
13
|
+
export { mockChain, mockGas };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Plugin } from 'vitest/config';
|
|
2
|
+
|
|
3
|
+
declare function mockGas(): Plugin;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 特定のメソッドチェーンの戻り値を指定した値に設定する
|
|
7
|
+
* @param root モックのルートオブジェクト(現在は使用していませんが、API互換性のために残しています)
|
|
8
|
+
* @param path 文字列によるパス記法 (例: "SpreadsheetApp.getActiveSpreadsheet.getSheetByName")
|
|
9
|
+
* @param value 返すべき値
|
|
10
|
+
*/
|
|
11
|
+
declare function mockChain(_root: any, path: string, value: any): void;
|
|
12
|
+
|
|
13
|
+
export { mockChain, mockGas };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Plugin } from 'vitest/config';
|
|
2
|
+
|
|
3
|
+
declare function mockGas(): Plugin;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 特定のメソッドチェーンの戻り値を指定した値に設定する
|
|
7
|
+
* @param root モックのルートオブジェクト(現在は使用していませんが、API互換性のために残しています)
|
|
8
|
+
* @param path 文字列によるパス記法 (例: "SpreadsheetApp.getActiveSpreadsheet.getSheetByName")
|
|
9
|
+
* @param value 返すべき値
|
|
10
|
+
*/
|
|
11
|
+
declare function mockChain(_root: any, path: string, value: any): void;
|
|
12
|
+
|
|
13
|
+
export { mockChain, mockGas };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { dirname, resolve } from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
export { m as mockChain } from './shared/vitest-plugin-gas-mock.DDSZt7OP.mjs';
|
|
4
|
+
|
|
5
|
+
const __dirname$1 = dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
function mockGas() {
|
|
7
|
+
const setupFilePath = resolve(__dirname$1, "setup");
|
|
8
|
+
return {
|
|
9
|
+
name: "vite-plugin-gasmock",
|
|
10
|
+
config: (config) => {
|
|
11
|
+
const setupFiles = config.test?.setupFiles || [];
|
|
12
|
+
const newSetupFiles = Array.isArray(setupFiles) ? [...setupFiles, setupFilePath] : [setupFiles, setupFilePath];
|
|
13
|
+
return {
|
|
14
|
+
test: {
|
|
15
|
+
setupFiles: newSetupFiles
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { mockGas };
|