Skip to content

Commit

Permalink
ModuleResolver: Add some unit tests (#1305)
Browse files Browse the repository at this point in the history
* ModuleResolver: Add some unit tests

Add .prettier-vscode-root file, largely for testing purposes

* ModuleResolver: update path regex to be platform independent
  • Loading branch information
JHilker committed Apr 12, 2020
1 parent 995238c commit ee5bf40
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/ModuleResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ export class ModuleResolver implements Disposable {
if (fs.existsSync(path.join(dir, "node_modules", pkgName))) {
return dir;
}

if (fs.existsSync(path.join(dir, ".prettier-vscode-root"))) {
return findUp.stop;
}
},
{ cwd: finalPath, type: "directory" }
);
Expand Down
91 changes: 91 additions & 0 deletions src/test/suite/ModuleResolver.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import * as assert from "assert";
import * as path from "path";
import * as prettier from "prettier";
// tslint:disable-next-line: no-implicit-dependencies
import * as sinon from "sinon";

import { getWorkspaceFolderUri } from "./format.test";
import { ModuleResolver } from "../../ModuleResolver";
import { LoggingService } from "../../LoggingService";
import { NotificationService } from "../../NotificationService";

suite("Test ModuleResolver", function () {
let moduleResolver: ModuleResolver;
let logErrorSpy: sinon.SinonSpy;
let logInfoSpy: sinon.SinonSpy;

this.beforeEach(() => {
const loggingService = new LoggingService();
logErrorSpy = sinon.spy(loggingService, "logError");
logInfoSpy = sinon.spy(loggingService, "logInfo");
const notificationService = new NotificationService(loggingService);

moduleResolver = new ModuleResolver(loggingService, notificationService);
});

suite("getPrettierInstance", () => {
test("it returns the bundled version of Prettier if local isn't found", () => {
const fileName = path.join(
getWorkspaceFolderUri("no-dep").fsPath,
"index.js"
);
const prettierInstance = moduleResolver.getPrettierInstance(fileName, {
showNotifications: true,
});

assert.equal(prettierInstance, prettier);
assert(logInfoSpy.calledWith("Using bundled version of prettier."));
});

test("it returns the bundled version of Prettier if local is outdated", () => {
const fileName = path.join(
getWorkspaceFolderUri("outdated").fsPath,
"ugly.js"
);
const prettierInstance = moduleResolver.getPrettierInstance(fileName);

assert.equal(prettierInstance, prettier);
assert(
logErrorSpy.calledWith(
"Outdated version of prettier installed. Falling back to bundled version of prettier."
)
);
});

test("it returns prettier version from package.json", () => {
const fileName = path.join(
getWorkspaceFolderUri("eslint").fsPath,
"index.js"
);
const prettierInstance = moduleResolver.getPrettierInstance(fileName);

assert.notEqual(prettierInstance, prettier);
assert.equal(prettierInstance.version, "2.0.2");
assert(
logInfoSpy.calledWith(
sinon.match(
/Loaded module 'prettier@2.0.2' from '.*[\/\\]eslint[\/\\]node_modules[\/\\]prettier[\/\\]index.js'/
)
)
);
});

test("it returns prettier version from module dep", () => {
const fileName = path.join(
getWorkspaceFolderUri("module").fsPath,
"index.js"
);
const prettierInstance = moduleResolver.getPrettierInstance(fileName);

assert.notEqual(prettierInstance, prettier);
assert.equal(prettierInstance.version, "2.0.2");
assert(
logInfoSpy.calledWith(
sinon.match(
/Loaded module 'prettier@2\.0\.2' from '.*[\/\\]module[\/\\]node_modules[\/\\]prettier[\/\\]index\.js'/
)
)
);
});
});
});
2 changes: 1 addition & 1 deletion src/test/suite/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const readFileAsync: (
* gets the workspace folder by name
* @param name Workspace folder name
*/
const getWorkspaceFolderUri = (workspaceFolderName: string) => {
export const getWorkspaceFolderUri = (workspaceFolderName: string) => {
const workspaceFolder = vscode.workspace.workspaceFolders!.find((folder) => {
return folder.name === workspaceFolderName;
});
Expand Down
6 changes: 6 additions & 0 deletions src/test/suite/module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { format, getText } from "./format.test";
suite("Test module resolution", function () {
this.timeout(10000);

test("it formats without prettier dep using internal version", async () => {
const { actual } = await format("no-dep", "index.js");
const expected = await getText("no-dep", "index.result.js");
assert.equal(actual, expected);
});

test("it formats without prettier in package.json", async () => {
const { actual } = await format("module", "index.js");
const expected = await getText("module", "index.result.js");
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions test-fixtures/no-dep/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tabWidth": 2
}
7 changes: 7 additions & 0 deletions test-fixtures/no-dep/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function ugly(a , b ,c){


return a +

b;
}
3 changes: 3 additions & 0 deletions test-fixtures/no-dep/index.result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function ugly(a, b, c) {
return a + b;
}
11 changes: 11 additions & 0 deletions test-fixtures/no-dep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "test-no-dep",
"version": "1.0.0",
"description": "Test folder for no prettier dependency",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "jhilker",
"license": "MIT"
}
5 changes: 4 additions & 1 deletion test-fixtures/test.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
},
{
"path": "module-plugin"
},
{
"path": "no-dep"
}
],
"settings": {
Expand Down Expand Up @@ -86,4 +89,4 @@
},
"prettier.trailingComma": "all"
}
}
}

0 comments on commit ee5bf40

Please sign in to comment.