Skip to content

Commit be757ea

Browse files
authored
copy (and update) package.json to out directory to enable mocking in integration tests (#482)
1 parent 6fe5b6c commit be757ea

File tree

5 files changed

+52
-18
lines changed

5 files changed

+52
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@
243243
"pretty": "eslint --fix --ext .ts",
244244
"pre-commit": "lint-staged",
245245
"compile-tests": "tsc -p . --outDir out",
246-
"pretest": "npm run compile-tests",
246+
"pretest": "npm run compile-tests && node ./out/test/createTestPackageJson.js",
247247
"test": "node ./out/test/runTest.js",
248248
"build": "npm install && npm run lint && npm run compile",
249249
"prepare": "husky install",

src/coverage-system/coverageservice.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,10 @@ export class CoverageService {
194194
}
195195

196196
private listenToEditorEvents() {
197-
this.editorWatcher = window.onDidChangeVisibleTextEditors(
197+
this.editorWatcher = window.onDidChangeActiveTextEditor(
198198
this.handleEditorEvents.bind(this),
199199
);
200+
201+
200202
}
201203
}

test/createTestPackageJson.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import path from "path";
2+
import fs from "fs";
3+
4+
const packageJson = JSON.parse(
5+
fs.readFileSync(path.resolve(__dirname, "../../package.json"), "utf-8")
6+
);
7+
8+
const testPackageJsonPath = path.resolve(__dirname, "..", "package.json");
9+
const testPackageJsonContents = JSON.stringify(
10+
{ ...packageJson, main: "./src/extension" },
11+
null,
12+
2
13+
);
14+
15+
fs.writeFileSync(testPackageJsonPath, testPackageJsonContents, "utf-8");

test/extension.test.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ suite("Extension Tests", function () {
3434
await checkCoverage(() => {
3535
// Look for exact coverage on the file
3636
const spyCall = decorationSpy.getCall(0);
37+
expect(spyCall).to.not.be.null;
3738
if (spyCall) {
3839
const cachedLines: ICoverageLines = spyCall.args[1];
3940
expect(cachedLines.full).to.have.lengthOf(3);
@@ -55,6 +56,7 @@ suite("Extension Tests", function () {
5556
await checkCoverage(() => {
5657
// Look for exact coverage on the file
5758
const spyCall = decorationSpy.getCall(0);
59+
expect(spyCall).to.not.be.null;
5860
if (spyCall) {
5961
const cachedLines: ICoverageLines = spyCall.args[1];
6062
expect(cachedLines.full).to.have.lengthOf(14);
@@ -76,6 +78,7 @@ suite("Extension Tests", function () {
7678
await checkCoverage(() => {
7779
// Look for exact coverage on the file
7880
const spyCall = decorationSpy.getCall(0);
81+
expect(spyCall).to.not.be.null;
7982
if (spyCall) {
8083
const cachedLines: ICoverageLines = spyCall.args[1];
8184
expect(cachedLines.full).to.have.lengthOf(3);
@@ -138,6 +141,7 @@ suite("Extension Tests", function () {
138141
await checkCoverage(() => {
139142
// Look for exact coverage on the file
140143
const spyCall = decorationSpy.getCall(0);
144+
expect(spyCall).to.not.be.null;
141145
if (spyCall) {
142146
const cachedLines: ICoverageLines = spyCall.args[1];
143147
expect(cachedLines.full).to.have.lengthOf(4);
@@ -159,6 +163,7 @@ suite("Extension Tests", function () {
159163
await checkCoverage(() => {
160164
// Look for exact coverage on the file
161165
const spyCall = decorationSpy.getCall(0);
166+
expect(spyCall).to.not.be.null;
162167
if (spyCall) {
163168
const cachedLines: ICoverageLines = spyCall.args[1];
164169
expect(cachedLines.none).to.have.lengthOf(6);
@@ -180,6 +185,7 @@ suite("Extension Tests", function () {
180185
await checkCoverage(() => {
181186
// Look for exact coverage on the file
182187
const spyCall = decorationSpy.getCall(0);
188+
expect(spyCall).to.not.be.null;
183189
if (spyCall) {
184190
const cachedLines: ICoverageLines = spyCall.args[1];
185191
expect(cachedLines.full).to.have.lengthOf(3);
@@ -201,6 +207,7 @@ suite("Extension Tests", function () {
201207
await checkCoverage(() => {
202208
// Look for exact coverage on the file
203209
const spyCall = decorationSpy.getCall(0);
210+
expect(spyCall).to.not.be.null;
204211
if (spyCall) {
205212
const cachedLines: ICoverageLines = spyCall.args[1];
206213
expect(cachedLines.full).to.have.lengthOf(4);
@@ -237,8 +244,10 @@ suite("Extension Tests", function () {
237244

238245
await checkCoverage(() => {
239246
// Look for exact coverage on the file
240-
if (decorationSpy.getCall(i)) {
241-
const cachedLines: ICoverageLines = decorationSpy.getCall(i).args[1];
247+
const spyCall = decorationSpy.getCall(i);
248+
expect(spyCall).to.not.be.null;
249+
if (spyCall) {
250+
const cachedLines: ICoverageLines = spyCall.args[1];
242251
expect(cachedLines.full).to.have.lengthOf(linesCovered);
243252
expect(cachedLines.none).to.have.lengthOf(linesNotCovered);
244253
}
@@ -258,8 +267,10 @@ suite("Extension Tests", function () {
258267

259268
await checkCoverage(() => {
260269
// Look for exact coverage on the ruby file
261-
if (decorationSpy.getCall(0)) {
262-
const cachedLines: ICoverageLines = decorationSpy.getCall(0).args[1];
270+
const spyCall = decorationSpy.getCall(0);
271+
expect(spyCall).to.not.be.null;
272+
if (spyCall) {
273+
const cachedLines: ICoverageLines = spyCall.args[1];
263274
expect(cachedLines.full).to.have.lengthOf(4);
264275
expect(cachedLines.partial).to.have.lengthOf(1);
265276
expect(cachedLines.none).to.have.lengthOf(1);
@@ -288,8 +299,10 @@ suite("Extension Tests", function () {
288299

289300
await checkCoverage(() => {
290301
// Look for exact coverage on the file
291-
if (decorationSpy.getCall(0)) {
292-
const cachedLines: ICoverageLines = decorationSpy.getCall(0).args[1];
302+
const spyCall = decorationSpy.getCall(0);
303+
expect(spyCall).to.not.be.null;
304+
if (spyCall) {
305+
const cachedLines: ICoverageLines = spyCall.args[1];
293306
expect(cachedLines.full).to.have.lengthOf(14);
294307
expect(cachedLines.none).to.have.lengthOf(4);
295308
expect(cachedLines.partial).to.have.lengthOf(7);
@@ -309,9 +322,11 @@ suite("Extension Tests", function () {
309322
await vscode.window.showTextDocument(testJSDocument);
310323

311324
await checkCoverage(() => {
312-
if (decorationSpy.getCall(0)) {
325+
const spyCall = decorationSpy.getCall(0);
326+
expect(spyCall).to.not.be.null;
327+
if (spyCall) {
313328
// Look for exact coverage on the file
314-
const jsCachedLines: ICoverageLines = decorationSpy.getCall(0).args[1];
329+
const jsCachedLines: ICoverageLines = spyCall.args[1];
315330
expect(jsCachedLines.full).to.have.lengthOf(14);
316331
expect(jsCachedLines.none).to.have.lengthOf(4);
317332
expect(jsCachedLines.partial).to.have.lengthOf(7);
@@ -326,9 +341,11 @@ suite("Extension Tests", function () {
326341
await wait(500);
327342

328343
await checkCoverage(() => {
329-
if (decorationSpy.getCall(1)) {
344+
const spyCall = decorationSpy.getCall(1);
345+
expect(spyCall).to.not.be.null;
346+
if (spyCall) {
330347
// Look for exact coverage on the file
331-
const javaCachedLines: ICoverageLines = decorationSpy.getCall(1).args[1];
348+
const javaCachedLines: ICoverageLines = spyCall.args[1];
332349
expect(javaCachedLines.full).to.have.lengthOf(4);
333350
expect(javaCachedLines.none).to.have.lengthOf(3);
334351
}
@@ -353,25 +370,25 @@ suite("Extension Tests", function () {
353370
setCoverageSpy.resetHistory();
354371
await vscode.window.showTextDocument(testJSDocument, vscode.ViewColumn.One);
355372

356-
expect(setCoverageSpy.calledWith(84))
373+
expect(setCoverageSpy.calledWith(84)).to.be.true;
357374
setCoverageSpy.resetHistory();
358375

359376
const [testJavaCoverage] = await vscode.workspace.findFiles("**/App.java", "**/node_modules/**");
360377
const testJavaDocument = await vscode.workspace.openTextDocument(testJavaCoverage);
361378

362-
await vscode.window.showTextDocument(testJavaDocument, vscode.ViewColumn.Two);
379+
await vscode.window.showTextDocument(testJavaDocument, vscode.ViewColumn.Two, false);
363380

364-
expect(setCoverageSpy.calledWith(57));
381+
expect(setCoverageSpy.calledWith(57)).to.be.true;
365382
setCoverageSpy.resetHistory();
366383

367384
await vscode.commands.executeCommand("workbench.action.previousEditor");
368385

369-
expect(setCoverageSpy.calledWith(84));
386+
expect(setCoverageSpy.calledWith(84)).to.be.true;
370387
setCoverageSpy.resetHistory();
371388

372389
await vscode.commands.executeCommand("workbench.action.closeAllEditors");
373390

374-
expect(setCoverageSpy.calledWith(undefined));
391+
expect(setCoverageSpy.calledWith(undefined)).to.be.true;
375392

376393
setCoverageSpy.restore();
377394
return vscode.commands.executeCommand("coverage-gutters.removeWatch");

test/runTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88

99
async function main() {
1010
try {
11-
const extensionDevelopmentPath = path.resolve(__dirname, "../../");
11+
const extensionDevelopmentPath = path.resolve(__dirname, "../../out");
1212
const extensionTestsPath = path.resolve(__dirname, "index");
1313

1414
// Add the dependent extension for test coverage preview functionality

0 commit comments

Comments
 (0)