Skip to content

Commit

Permalink
test: add test for vls config
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Feb 6, 2022
1 parent 3111c46 commit c6501f2
Show file tree
Hide file tree
Showing 7 changed files with 617 additions and 679 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const config = {
},
watchPathIgnorePatterns: ['<rootDir>/temp'],
snapshotSerializers: ['jest-serializer-path'],
forceExit: true,
}

module.exports = config
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@types/babel__code-frame": "^7.0.2",
"@types/debug": "^4.1.5",
"@types/fs-extra": "^9.0.11",
"@types/jest": "^26.0.23",
"@types/jest": "^27.4.0",
"@types/node": "^14.14.31",
"@types/prompts": "^2.0.13",
"@types/rimraf": "^3.0.0",
Expand All @@ -46,8 +46,8 @@
"fast-json-stable-stringify": "^2.1.0",
"fs-extra": "^10.0.0",
"husky": "4.3.7",
"jest": "^27.0.4",
"jest-environment-node": "^26.0.0",
"jest": "^27.4.7",
"jest-environment-node": "^27.4.6",
"jest-serializer-path": "^0.1.15",
"lint-staged": "^11.0.0",
"minimist": "^1.2.5",
Expand All @@ -57,7 +57,7 @@
"rimraf": "^3.0.2",
"semver": "^7.3.5",
"strip-ansi": "^7.0.0",
"ts-jest": "^27.0.3",
"ts-jest": "^27.1.3",
"typescript": "^4.2.2",
"vite": "^2.7.9",
"vite-plugin-checker": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`VLS config customized config 1`] = `
Object {
"css": Object {},
"emmet": Object {},
"html": Object {
"suggest": Object {},
},
"javascript": Object {
"format": Object {},
},
"stylusSupremacy": Object {},
"typescript": Object {
"format": Object {},
"tsdk": null,
},
"vetur": Object {
"completion": Object {
"autoImport": false,
"scaffoldSnippetSources": Object {
"user": "🗒️",
"vetur": "",
"workspace": "💼",
},
"tagCasing": "initial",
},
"dev": Object {
"logLevel": "INFO",
"vlsPath": "",
"vlsPort": -1,
},
"experimental": Object {
"templateInterpolationService": true,
},
"format": Object {
"defaultFormatter": Object {},
"defaultFormatterOptions": Object {},
"enable": true,
"options": Object {
"tabSize": 2,
"useTabs": false,
},
"scriptInitialIndent": false,
"styleInitialIndent": false,
},
"grammar": Object {
"customBlocks": Object {},
},
"ignoreProjectWarning": false,
"languageFeatures": Object {
"codeActions": true,
"semanticTokens": true,
"updateImportOnFileMove": true,
},
"trace": Object {
"server": "off",
},
"useWorkspaceDependencies": false,
"validation": Object {
"interpolation": false,
"script": true,
"style": false,
"template": false,
"templateProps": false,
},
},
}
`;

exports[`VLS config default config 1`] = `
Object {
"css": Object {},
"emmet": Object {},
"html": Object {
"suggest": Object {},
},
"javascript": Object {
"format": Object {},
},
"stylusSupremacy": Object {},
"typescript": Object {
"format": Object {},
"tsdk": null,
},
"vetur": Object {
"completion": Object {
"autoImport": false,
"scaffoldSnippetSources": Object {
"user": "🗒️",
"vetur": "",
"workspace": "💼",
},
"tagCasing": "initial",
},
"dev": Object {
"logLevel": "INFO",
"vlsPath": "",
"vlsPort": -1,
},
"experimental": Object {
"templateInterpolationService": true,
},
"format": Object {
"defaultFormatter": Object {},
"defaultFormatterOptions": Object {},
"enable": true,
"options": Object {
"tabSize": 2,
"useTabs": false,
},
"scriptInitialIndent": false,
"styleInitialIndent": false,
},
"grammar": Object {
"customBlocks": Object {},
},
"ignoreProjectWarning": false,
"languageFeatures": Object {
"codeActions": true,
"semanticTokens": true,
"updateImportOnFileMove": true,
},
"trace": Object {
"server": "off",
},
"useWorkspaceDependencies": false,
"validation": Object {
"interpolation": true,
"script": true,
"style": true,
"template": true,
"templateProps": true,
},
},
}
`;
47 changes: 47 additions & 0 deletions packages/vite-plugin-checker/__tests__/unit/vlsConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import path from 'path'
import { ShutdownRequest } from 'vscode-languageserver/node'
import { URI } from 'vscode-uri'

import { prepareClientConnection } from '../../src/checkers/vls/diagnostics'

async function testVslConfig(overrideConfig?: any) {
const workspaceUri = URI.file(path.join(__dirname, 'fixtures'))
const { clientConnection, serverConnection, vls, up, down, logger } =
await prepareClientConnection(workspaceUri, {
watch: false,
verbose: false,
config: overrideConfig || null,
})

// @ts-expect-error
expect(vls.workspaceConfig).toMatchSnapshot()

// TODO: this test case will let Jest hang with out --forceExit option
// current enable forceExit but we should find which resource is not released
await clientConnection.sendRequest(ShutdownRequest.type)
clientConnection.dispose()
clientConnection.end()
serverConnection.dispose()
up.destroy()
down.destroy()
vls.dispose()
}

describe('VLS config', () => {
it('default config', async () => {
await testVslConfig()
})

it('customized config', async () => {
await testVslConfig({
vetur: {
validation: {
template: false,
templateProps: false,
interpolation: false,
style: false,
},
},
})
})
})
14 changes: 7 additions & 7 deletions packages/vite-plugin-checker/src/checkers/vls/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class NullLogger implements Logger {
public log(_message: string): void {}
}

class TestStream extends Duplex {
export class TestStream extends Duplex {
public _write(chunk: string, _encoding: string, done: () => void) {
this.emit('data', chunk)
done()
Expand All @@ -133,7 +133,7 @@ function suppressConsole() {
}
}

async function prepareClientConnection(workspaceUri: URI, options: DiagnosticOptions) {
export async function prepareClientConnection(workspaceUri: URI, options: DiagnosticOptions) {
const up = new TestStream()
const down = new TestStream()
const logger = new NullLogger()
Expand Down Expand Up @@ -200,24 +200,24 @@ async function prepareClientConnection(workspaceUri: URI, options: DiagnosticOpt
vls.listen()
clientConnection.listen()

const init = getInitParams(workspaceUri)
const initParams = getInitParams(workspaceUri)

if (options.config) {
// Merge in used-provided VLS settings if present
mergeDeep(init.initializationOptions.config, options.config)
mergeDeep(initParams.initializationOptions.config, options.config)
}

await clientConnection.sendRequest(InitializeRequest.type, init)
await clientConnection.sendRequest(InitializeRequest.type, initParams)

return clientConnection
return { clientConnection, serverConnection, vls, up, down, logger }
}

async function getDiagnostics(
workspaceUri: URI,
severity: DiagnosticSeverity,
options: DiagnosticOptions
) {
const clientConnection = await prepareClientConnection(workspaceUri, options)
const { clientConnection } = await prepareClientConnection(workspaceUri, options)

const files = glob.sync('**/*.vue', { cwd: workspaceUri.fsPath, ignore: ['node_modules/**'] })

Expand Down
2 changes: 1 addition & 1 deletion playground/vue2-vls/__tests__/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('vue2-vls', () => {

await viteServe({
cwd: testDir,
// @ts-expect-error
// @ts-expect-errorƒ
wsSend: (_payload) => (err = _payload.err),
})

Expand Down
Loading

0 comments on commit c6501f2

Please sign in to comment.