Skip to content

Commit 6e134de

Browse files
committed
Add test for project reference and auto import
Test for #34677
1 parent 292d018 commit 6e134de

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

src/testRunner/unittests/tsserver/projectReferences.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,5 +2173,114 @@ foo;`
21732173
});
21742174
});
21752175
});
2176+
2177+
describe("auto import with referenced project", () => {
2178+
function verifyAutoImport(built: boolean, disableSourceOfProjectReferenceRedirect?: boolean) {
2179+
const solnConfig: File = {
2180+
path: `${tscWatch.projectRoot}/tsconfig.json`,
2181+
content: JSON.stringify({
2182+
files: [],
2183+
references: [
2184+
{ path: "shared/src/library" },
2185+
{ path: "app/src/program" }
2186+
]
2187+
})
2188+
};
2189+
const sharedConfig: File = {
2190+
path: `${tscWatch.projectRoot}/shared/src/library/tsconfig.json`,
2191+
content: JSON.stringify({
2192+
compilerOptions: {
2193+
composite: true,
2194+
outDir: "../../bld/library"
2195+
}
2196+
})
2197+
};
2198+
const sharedIndex: File = {
2199+
path: `${tscWatch.projectRoot}/shared/src/library/index.ts`,
2200+
content: `export function foo() {}`
2201+
};
2202+
const sharedPackage: File = {
2203+
path: `${tscWatch.projectRoot}/shared/package.json`,
2204+
content: JSON.stringify({
2205+
name: "shared",
2206+
version: "1.0.0",
2207+
main: "bld/library/index.js",
2208+
types: "bld/library/index.d.ts"
2209+
})
2210+
};
2211+
const appConfig: File = {
2212+
path: `${tscWatch.projectRoot}/app/src/program/tsconfig.json`,
2213+
content: JSON.stringify({
2214+
compilerOptions: {
2215+
composite: true,
2216+
outDir: "../../bld/program",
2217+
disableSourceOfProjectReferenceRedirect
2218+
},
2219+
references: [
2220+
{ path: "../../../shared/src/library" }
2221+
]
2222+
})
2223+
};
2224+
const appBar: File = {
2225+
path: `${tscWatch.projectRoot}/app/src/program/bar.ts`,
2226+
content: `import {foo} from "shared";`
2227+
};
2228+
const appIndex: File = {
2229+
path: `${tscWatch.projectRoot}/app/src/program/index.ts`,
2230+
content: `foo`
2231+
};
2232+
const sharedSymlink: SymLink = {
2233+
path: `${tscWatch.projectRoot}/node_modules/shared`,
2234+
symLink: `${tscWatch.projectRoot}/shared`
2235+
};
2236+
const files = [solnConfig, sharedConfig, sharedIndex, sharedPackage, appConfig, appBar, appIndex, sharedSymlink, libFile];
2237+
const host = createServerHost(files);
2238+
if (built) {
2239+
const solutionBuilder = tscWatch.createSolutionBuilder(host, [solnConfig.path], {});
2240+
solutionBuilder.build();
2241+
host.clearOutput();
2242+
}
2243+
const session = createSession(host);
2244+
openFilesForSession([appIndex], session);
2245+
const response = session.executeCommandSeq<protocol.CodeFixRequest>({
2246+
command: protocol.CommandTypes.GetCodeFixes,
2247+
arguments: {
2248+
file: appIndex.path,
2249+
startLine: 1,
2250+
startOffset: 1,
2251+
endLine: 1,
2252+
endOffset: 4,
2253+
errorCodes: [Diagnostics.Cannot_find_name_0.code],
2254+
}
2255+
}).response as protocol.CodeFixAction[];
2256+
assert.deepEqual(response, [
2257+
{
2258+
fixName: "import",
2259+
description: `Import 'foo' from module "shared"`,
2260+
changes: [{
2261+
fileName: appIndex.path,
2262+
textChanges: [{
2263+
start: { line: 1, offset: 1 },
2264+
end: { line: 1, offset: 1 },
2265+
newText: 'import { foo } from "shared";\n\n',
2266+
}],
2267+
}],
2268+
commands: undefined,
2269+
fixAllDescription: undefined,
2270+
fixId: undefined
2271+
}
2272+
]);
2273+
}
2274+
2275+
it("when project is built", () => {
2276+
verifyAutoImport(/*built*/ true);
2277+
});
2278+
it("when project is not built", () => {
2279+
verifyAutoImport(/*built*/ false);
2280+
});
2281+
it("when disableSourceOfProjectReferenceRedirect is true", () => {
2282+
verifyAutoImport(/*built*/ true, /*disableSourceOfProjectReferenceRedirect*/ true);
2283+
});
2284+
});
21762285
});
21772286
}

0 commit comments

Comments
 (0)