test(language-service): [ivy] Add mock service to overwrite files (#36923)
Add a mechanism to replace file contents for a specific file. This allows us to write custom test scenarios in code without modifying the test project. Since we are no longer mocking the language service host, the file overwrite needs to happen via the project service. Project service manages a set of script infos, and overwriting the files is a matter of updating the relevant script infos. Note that the actual project service is wrapped inside a Mock Service. Tests should not have direct access to the project service. All manipulations should take place via the Mock Service. The MockService provides a `reset()` method to undo temporary overwrites after each test. PR Close #36923
This commit is contained in:

committed by
Alex Rickabaugh

parent
1b8752e595
commit
da79e0433f
@ -11,9 +11,17 @@ import * as ts from 'typescript/lib/tsserverlibrary';
|
||||
import {APP_MAIN, setup, TEST_SRCDIR} from './mock_host';
|
||||
|
||||
describe('mock host', () => {
|
||||
const {project, service, tsLS} = setup();
|
||||
|
||||
beforeEach(() => {
|
||||
service.reset();
|
||||
});
|
||||
|
||||
it('can load test project from Bazel runfiles', () => {
|
||||
const {project, tsLS} = setup();
|
||||
expect(project).toBeInstanceOf(ts.server.ConfiguredProject);
|
||||
const configPath = (project as ts.server.ConfiguredProject).getConfigFilePath();
|
||||
expect(configPath.substring(TEST_SRCDIR.length))
|
||||
.toBe('/angular/packages/language-service/test/project/tsconfig.json');
|
||||
const program = tsLS.getProgram();
|
||||
expect(program).toBeDefined();
|
||||
const sourceFiles = program!.getSourceFiles().map(sf => {
|
||||
@ -36,7 +44,6 @@ describe('mock host', () => {
|
||||
});
|
||||
|
||||
it('produces no TS error for test project', () => {
|
||||
const {project, tsLS} = setup();
|
||||
const errors = project.getAllProjectErrors();
|
||||
expect(errors).toEqual([]);
|
||||
const globalErrors = project.getGlobalProjectErrors();
|
||||
@ -44,4 +51,24 @@ describe('mock host', () => {
|
||||
const diags = tsLS.getSemanticDiagnostics(APP_MAIN);
|
||||
expect(diags).toEqual([]);
|
||||
});
|
||||
|
||||
it('can overwrite test file', () => {
|
||||
service.overwrite(APP_MAIN, `const x: string = 0`);
|
||||
const scriptInfo = service.getScriptInfo(APP_MAIN);
|
||||
expect(getText(scriptInfo)).toBe('const x: string = 0');
|
||||
});
|
||||
|
||||
it('can find the cursor', () => {
|
||||
const content = service.overwrite(APP_MAIN, `const fo¦o = 'hello world';`);
|
||||
// content returned by overwrite() is the original content with cursor
|
||||
expect(content).toBe(`const fo¦o = 'hello world';`);
|
||||
const scriptInfo = service.getScriptInfo(APP_MAIN);
|
||||
// script info content should not contain cursor
|
||||
expect(getText(scriptInfo)).toBe(`const foo = 'hello world';`);
|
||||
});
|
||||
});
|
||||
|
||||
function getText(scriptInfo: ts.server.ScriptInfo): string {
|
||||
const snapshot = scriptInfo.getSnapshot();
|
||||
return snapshot.getText(0, snapshot.getLength());
|
||||
}
|
||||
|
Reference in New Issue
Block a user