From 57e683b90a197be8a1ef6fd51c7b6fbb51cd29c3 Mon Sep 17 00:00:00 2001 From: equiman Date: Sun, 26 Feb 2023 23:56:00 -0500 Subject: [PATCH] add better mock and spy snippets --- .vscode/settings.json | 3 +++ CHANGELOG.md | 6 ++++++ README.md | 16 ++++++++++++---- package.json | 2 +- snippets/mock.json | 44 +++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 64 insertions(+), 7 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 2c9e468..feeabad 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,8 +1,10 @@ { "cSpell.words": [ + "cfrv", "deinsoftware", "Equiman", "github", + "mrvo", "paypal", "tbct", "tbgt", @@ -16,6 +18,7 @@ "tmis", "ttemis", "ttems", + "vfrv", "volta" ], "markdownlint.config": { diff --git a/CHANGELOG.md b/CHANGELOG.md index 4721e23..4e126ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,12 @@ Fixed for any bug fixes. Security to invite users to upgrade in case of vulnerabilities. --> +## 1.1.0 - 2023/02/26 + +### Added + +- Better mock and spy snippets + ## 1.0.0 - 2023/02/26 ### Added diff --git a/README.md b/README.md index 28713b5..4ff5e7f 100644 --- a/README.md +++ b/README.md @@ -96,10 +96,18 @@ Below is a list of all available snippets and the triggers of each one. The ** ### Mock -| Trigger | Result | -| -------: | ------------- | -| `vf→` | `vi.fn(█)` | -| `vs→` | `vi.spyOn(█)` | +| Trigger | Result | +| -------: | --------------------------------------------------------------------------- | +| `vf→` | `vi.fn()█` | +| `vfrv→` | `vi.fn().mockResolvedValue(█)` | +| `cf→` | `const nameMock = vi.fn()█` | +| `cfrv→` | `const nameMock = vi.fn().mockResolvedValue(█)` | +| `mrv→` | `mock.mockReturnValue(█)` | +| `mrvo→` | `mock.mockReturnValueOnce(█)` | +| `vs→` | `vi.spyOn(global, 'method')█` | +| `vsi→` | `vi.spyOn(global, 'method').mockImplementation(() => █)` | +| `cs→` | `const methodSpy = vi.spyOn(global, 'method')█` | +| `csi→` | `const methodSpy = vi.spyOn(global, 'method').mockImplementation(() => █)` | ### It diff --git a/package.json b/package.json index d78ae70..f731bd6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vitest-snippets", "description": "VS Code Vitest snippets for JS and TS", - "version": "1.0.0", + "version": "1.1.0", "displayName": "Vitest Snippets", "publisher": "deinsoftware", "icon": "images/light-icon.png", diff --git a/snippets/mock.json b/snippets/mock.json index b473faf..3824bd3 100644 --- a/snippets/mock.json +++ b/snippets/mock.json @@ -1,12 +1,52 @@ { "vi.fn": { "prefix": "vf", - "body": "vi.fn($0)", + "body": "vi.fn()$0", "description": "creates vi.fn()" }, + "vi.fn.mockResolvedValue": { + "prefix": "vfrv", + "body": "vi.fn().mockResolvedValue($0)", + "description": "creates vi.fn() with resolved value" + }, + "const.fn": { + "prefix": "cf", + "body": "const {$1:name}Mock = vi.fn()$0", + "description": "creates a mock variable for a vi.fn()" + }, + "const.fn.mockResolvedValue": { + "prefix": "cfrv", + "body": "const {$1:name}Mock = vi.fn().mockResolvedValue($0)", + "description": "creates a mock variable for a vi.fn() with resolved value" + }, + "mock.mockReturnValue": { + "prefix": "mrv", + "body": "{$1:mock}.mockReturnValue($0)", + "description": "assign a return value" + }, + "mock.mockReturnValueOnce": { + "prefix": "mrv", + "body": "{$1:mock}.mockReturnValueOnce($0)", + "description": "assign a return value for only one call" + }, "vi.spyOn": { "prefix": "vs", - "body": "vi.spyOn($0)", + "body": "vi.spyOn({$1:global}, '{$2:method}'))$0", "description": "creates vi.spyOn()" + }, + "vi.spyOn.mockImplementation": { + "prefix": "vsi", + "body": "vi.spyOn({$1:global}, '{$2:method}')).mockImplementation(() => $0)", + "description": "creates vi.spyOn() with mock implementation" + }, + "const.spyOn": { + "prefix": "cs", + "body": "const {$2:method}Spy = vi.spyOn({$1:global}, '{$2:method}'))$0", + "description": "creates a spy variable for a vi.spyOn()" + }, + "const.spyOn.mockImplementation": { + "prefix": "csi", + "body": "const {$2:method}Spy = vi.spyOn({$1:global}, '{$2:method}')).mockImplementation(() => $0)", + "description": "creates a spy variable for a vi.spyOn() with mock implementation" } }