-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-setup.ts
33 lines (26 loc) · 1 KB
/
test-setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { Mock, vi } from 'vitest';
// In order to test our server code we need the Google Apps Script globals to be available
// These are not available by default when we run vitest in node environment
// That is why we setup "polyfill" mocks to do the work for us
// We utilise the mocking functions from vitest to mock all the functionality
class Range {
static getLastRow = vi.fn(() => this);
static offset = vi.fn(() => this);
static getValues: Mock = vi.fn(() => []);
}
class Sheet {
static getSheetId = vi.fn();
}
class Spreadsheet {
static getSheets = vi.fn(() => [Sheet]);
static getRangeByName = vi.fn(() => Range);
}
class SpreadSheetApp {
static getActiveSpreadsheet = vi.fn(() => Spreadsheet);
}
vi.stubGlobal('SpreadsheetApp', SpreadSheetApp);
vi.stubGlobal('Spreadsheet', Spreadsheet);
export const RangeMock = vi.mocked(Range);
export const SheetMock = vi.mocked(Sheet);
export const SpreadsheetMock = vi.mocked(Spreadsheet);
export const SpreadSheetAppMock = vi.mocked(SpreadSheetApp);