Skip to content

Commit d4fc2b8

Browse files
committed
Update documentation
1 parent 99f0be3 commit d4fc2b8

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

docs/contributor/writing-tests-for-vscode-swift.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ Mocking file system access can be a challenging endeavor that is prone to fail w
121121
The [`mock-fs`](https://github.com/tschaub/mock-fs) module is a well-maintained library that can be used to mitigate these issues by temporarily replacing Node's built-in `fs` module with an in-memory file system. This can be useful for testing logic that uses the `fs` module without actually reaching out to the file system. Just a single function call can be used to configure what the fake file system will contain:
122122

123123
```typescript
124-
import * as chai from "chai";
124+
import { expect } from "chai";
125125
import * as mockFS from "mock-fs";
126-
import * as fs from "fs/promises";
126+
import * as vscode from "vscode";
127127

128128
suite("mock-fs example", () => {
129129
// This teardown step is also important to make sure your tests clean up the
@@ -137,8 +137,9 @@ suite("mock-fs example", () => {
137137
mockFS({
138138
"/path/to/some/file": "Some really cool file contents",
139139
});
140-
await expect(fs.readFile("/path/to/some/file", "utf-8"))
141-
.to.eventually.equal("Some really cool file contents");
140+
const uri = vscode.Uri.file("/path/to/some/file");
141+
expect(Buffer.from(await vscode.workspace.fs.readFile(uri)).toString("utf-8"))
142+
.to.equal("Some really cool file contents");
142143
});
143144
});
144145
```
@@ -148,7 +149,8 @@ In order to test failure paths, you can either create an empty file system or us
148149
```typescript
149150
test("file is not readable by the current user", async () => {
150151
mockFS({ "/path/to/file": mockFS.file({ mode: 0o000 }) });
151-
await expect(fs.readFile("/path/to/file", "utf-8")).to.eventually.be.rejected;
152+
const uri = vscode.Uri.file("/path/to/file");
153+
await expect(vscode.workspace.fs.readFile(uri)).to.eventually.be.rejected;
152154
});
153155
```
154156

test/unit-tests/mock-fs.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the VS Code Swift open source project
4+
//
5+
// Copyright (c) 2025 the VS Code Swift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import { expect } from "chai";
16+
import * as mockFS from "mock-fs";
17+
import * as vscode from "vscode";
18+
19+
suite("mock-fs example", () => {
20+
// This teardown step is also important to make sure your tests clean up the
21+
// mocked file system when they complete!
22+
teardown(() => {
23+
mockFS.restore();
24+
});
25+
26+
test("mock out a file on disk", async () => {
27+
// A single function call can be used to configure the file system
28+
mockFS({
29+
"/path/to/some/file": "Some really cool file contents",
30+
});
31+
expect(
32+
Buffer.from(
33+
await vscode.workspace.fs.readFile(vscode.Uri.file("/path/to/some/file"))
34+
).toString("utf-8")
35+
).to.equal("Some really cool file contents");
36+
});
37+
38+
test("file is not readable by the current user", async () => {
39+
mockFS({ "/path/to/file": mockFS.file({ mode: 0o000 }) });
40+
await expect(vscode.workspace.fs.readFile(vscode.Uri.file("/path/to/file"))).to.eventually
41+
.be.rejected;
42+
});
43+
});

0 commit comments

Comments
 (0)