-
-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ModuleResolver: Add some unit tests (#1305)
* 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
Showing
10 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'/ | ||
) | ||
) | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"tabWidth": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function ugly(a , b ,c){ | ||
|
||
|
||
return a + | ||
|
||
b; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function ugly(a, b, c) { | ||
return a + b; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters