Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock Uri in Jest with vscode-uri #245

Merged
merged 10 commits into from
Apr 7, 2021
6 changes: 5 additions & 1 deletion extension/src/__mocks__/vscode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { join } from 'path'
import { URI, Utils } from 'vscode-uri'

export const Extension = jest.fn()
export const extensions = jest.fn()
Expand All @@ -13,4 +14,7 @@ export const workspace = {
}
]
}
export const Uri = { file: jest.fn().mockImplementation(file => file) }
export const Uri = {
file: jest.fn(URI.file),
joinPath: jest.fn(Utils.joinPath)
}
9 changes: 6 additions & 3 deletions extension/src/git.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getAllUntracked } from './git'
import { ensureFile, remove } from 'fs-extra'
import { join, resolve } from 'path'
import { mapPaths } from './util/testHelpers'

describe('getAllUntracked', () => {
it('should return a list of all untracked paths', async () => {
Expand Down Expand Up @@ -28,7 +29,7 @@ describe('getAllUntracked', () => {

await Promise.all([remove(untrackedDir), remove(untrackedPython)])

expect(gitUntracked).toEqual(
expect(mapPaths(gitUntracked)).toEqual(
expect.arrayContaining([
untrackedDir,
untrackedPerl,
Expand All @@ -37,9 +38,11 @@ describe('getAllUntracked', () => {
])
)

expect(dvcUntracked).toEqual(
expect(mapPaths(dvcUntracked)).toEqual(
expect.arrayContaining([untrackedDir, untrackedPerl, untrackedText])
)
expect(dvcUntracked).not.toEqual(expect.arrayContaining([untrackedPython]))
expect(mapPaths(dvcUntracked)).not.toEqual(
expect.arrayContaining([untrackedPython])
)
})
})
5 changes: 5 additions & 0 deletions extension/src/util/testHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Uri } from 'vscode'

export function mapPaths(uris: Uri[]): string[] {
return uris.map(uri => uri.path)
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"prettier": "^1.19.1",
"prettier-config-standard": "^4.0.0",
"ts-jest": "^26.4.4",
"typescript": "^4.1.3"
"typescript": "^4.1.3",
"vscode-uri": "^3.0.2"
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14129,6 +14129,11 @@ vscode-test@^1.5.1:
rimraf "^3.0.2"
unzipper "^0.10.11"

vscode-uri@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/vscode-uri/-/vscode-uri-3.0.2.tgz#ecfd1d066cb8ef4c3a208decdbab9a8c23d055d0"
integrity sha512-jkjy6pjU1fxUvI51P+gCsxg1u2n8LSt0W6KrCNQceaziKzff74GoWmjVG46KieVzybO1sttPQmYfrwSHey7GUA==

w3c-hr-time@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/w3c-hr-time/-/w3c-hr-time-1.0.2.tgz#0a89cdf5cc15822df9c360543676963e0cc308cd"
Expand Down