Skip to content

Increase server test coverage from 53 to 70% #191

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

Merged
merged 2 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module.exports = {
'prefer-const': 'error',
'prefer-template': 'error',
'simple-import-sort/sort': 'error',
'object-shorthand': 'error',

'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-member-accessibility': 'off',
Expand Down
15 changes: 15 additions & 0 deletions server/src/__tests__/__snapshots__/server.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`server initializes and responds to capabilities 1`] = `
Object {
"completionProvider": Object {
"resolveProvider": true,
},
"definitionProvider": true,
"documentHighlightProvider": true,
"documentSymbolProvider": true,
"hoverProvider": true,
"referencesProvider": true,
"textDocumentSync": 1,
}
`;
178 changes: 178 additions & 0 deletions server/src/__tests__/server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import * as lsp from 'vscode-languageserver'

import { FIXTURE_FOLDER, FIXTURE_URI } from '../../../testing/fixtures'
import LspServer from '../server'

async function initializeServer() {
const diagnostics: Array<lsp.PublishDiagnosticsParams | undefined> = undefined
const console: any = {
error: jest.fn(),
warn: jest.fn(),
info: jest.fn(),
log: jest.fn(),
}

const connection: jest.Mocked<lsp.Connection> = {
client: {} as any,
console,
dispose: jest.fn(),
listen: jest.fn(),
onCodeAction: jest.fn(),
onCodeLens: jest.fn(),
onCodeLensResolve: jest.fn(),
onColorPresentation: jest.fn(),
onCompletion: jest.fn(),
onCompletionResolve: jest.fn(),
onDeclaration: jest.fn(),
onDefinition: jest.fn(),
onDidChangeConfiguration: jest.fn(),
onDidChangeTextDocument: jest.fn(),
onDidChangeWatchedFiles: jest.fn(),
onDidCloseTextDocument: jest.fn(),
onDidOpenTextDocument: jest.fn(),
onDidSaveTextDocument: jest.fn(),
onDocumentColor: jest.fn(),
onDocumentFormatting: jest.fn(),
onDocumentHighlight: jest.fn(),
onDocumentLinkResolve: jest.fn(),
onDocumentLinks: jest.fn(),
onDocumentOnTypeFormatting: jest.fn(),
onDocumentRangeFormatting: jest.fn(),
onDocumentSymbol: jest.fn(),
onExecuteCommand: jest.fn(),
onExit: jest.fn(),
onFoldingRanges: jest.fn(),
onHover: jest.fn(),
onImplementation: jest.fn(),
onInitialize: jest.fn(),
onInitialized: jest.fn(),
onNotification: jest.fn(),
onPrepareRename: jest.fn(),
onReferences: jest.fn(),
onRenameRequest: jest.fn(),
onRequest: jest.fn(),
onShutdown: jest.fn(),
onSignatureHelp: jest.fn(),
onTypeDefinition: jest.fn(),
onWillSaveTextDocument: jest.fn(),
onWillSaveTextDocumentWaitUntil: jest.fn(),
onWorkspaceSymbol: jest.fn(),
sendDiagnostics: jest.fn(),
sendNotification: jest.fn(),
sendRequest: jest.fn(),
telemetry: {} as any,
tracer: {} as any,
window: {} as any,
workspace: {} as any,
}

const server = await LspServer.initialize(connection, {
rootPath: FIXTURE_FOLDER,
rootUri: undefined,
processId: 42,
capabilities: {} as any,
workspaceFolders: null,
})

return {
connection,
console,
diagnostics,
server,
}
}

describe('server', () => {
it('initializes and responds to capabilities', async () => {
const { server } = await initializeServer()
expect(server.capabilities()).toMatchSnapshot()
})

it('register LSP connection', async () => {
const { connection, server } = await initializeServer()

server.register(connection)

expect(connection.onHover).toHaveBeenCalledTimes(1)
expect(connection.onDefinition).toHaveBeenCalledTimes(1)
expect(connection.onDocumentSymbol).toHaveBeenCalledTimes(1)
expect(connection.onDocumentHighlight).toHaveBeenCalledTimes(1)
expect(connection.onReferences).toHaveBeenCalledTimes(1)
expect(connection.onCompletion).toHaveBeenCalledTimes(1)
expect(connection.onCompletionResolve).toHaveBeenCalledTimes(1)
})

it('responds to onHover', async () => {
const { connection, server } = await initializeServer()
server.register(connection)

const onHover = connection.onHover.mock.calls[0][0]

const result = await onHover(
{
textDocument: {
uri: FIXTURE_URI.INSTALL,
},
position: {
line: 25,
character: 5,
},
},
{} as any,
)

expect(result).toBeDefined()
expect(result).toEqual({
contents: {
kind: 'markdown',
value: expect.stringContaining('RM(1)'),
},
})
})

it('responds to onCompletion when word is found', async () => {
const { connection, server } = await initializeServer()
server.register(connection)

const onCompletion = connection.onCompletion.mock.calls[0][0]

const result = await onCompletion(
{
textDocument: {
uri: FIXTURE_URI.INSTALL,
},
position: {
line: 25,
character: 5,
},
},
{} as any,
)

// Entire list
expect('length' in result && result.length > 50)
})

it('responds to onCompletion when no word is found', async () => {
const { connection, server } = await initializeServer()
server.register(connection)

const onCompletion = connection.onCompletion.mock.calls[0][0]

const result = await onCompletion(
{
textDocument: {
uri: FIXTURE_URI.INSTALL,
},
position: {
line: 2,
character: 1,
},
},
{} as any,
)

// Entire list
expect('length' in result && result.length > 50)
})
})
23 changes: 15 additions & 8 deletions testing/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ import * as LSP from 'vscode-languageserver'

export const FIXTURE_FOLDER = path.join(__dirname, './fixtures/')

function getFixture(filename: string) {
function getDocument(uri: string) {
return LSP.TextDocument.create(
'foo',
'bar',
0,
fs.readFileSync(path.join(FIXTURE_FOLDER, filename), 'utf8'),
fs.readFileSync(uri.replace('file://', ''), 'utf8'),
)
}

const FIXTURES = {
MISSING_NODE: getFixture('missing-node.sh'),
ISSUE101: getFixture('issue101.sh'),
INSTALL: getFixture('install.sh'),
PARSE_PROBLEMS: getFixture('parse-problems.sh'),
export const FIXTURE_URI = {
MISSING_NODE: `file://${path.join(FIXTURE_FOLDER, 'missing-node.sh')}`,
ISSUE101: `file://${path.join(FIXTURE_FOLDER, 'issue101.sh')}`,
INSTALL: `file://${path.join(FIXTURE_FOLDER, 'install.sh')}`,
PARSE_PROBLEMS: `file://${path.join(FIXTURE_FOLDER, 'parse-problems.sh')}`,
}

export default FIXTURES
export const FIXTURE_DOCUMENT = {
MISSING_NODE: getDocument(FIXTURE_URI.MISSING_NODE),
ISSUE101: getDocument(FIXTURE_URI.ISSUE101),
INSTALL: getDocument(FIXTURE_URI.INSTALL),
PARSE_PROBLEMS: getDocument(FIXTURE_URI.PARSE_PROBLEMS),
}

export default FIXTURE_DOCUMENT
2 changes: 0 additions & 2 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"compilerOptions": {
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"strict": true,
Expand Down