{"record":{"id":"9744bd7b01e747b7","repo":"angular/angular-cli","slug":"testprojecthost-must-be-initialized-before-being-u","errorCode":null,"errorMessage":"TestProjectHost must be initialized before being used.","messagePattern":"TestProjectHost must be initialized before being used\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/angular_devkit/architect/testing/test-project-host.ts","lineNumber":48,"sourceCode":"  of,\n  retry,\n  tap,\n} from 'rxjs';\n\n/**\n * @deprecated\n */\nexport class TestProjectHost extends NodeJsSyncHost {\n  private _currentRoot: Path | null = null;\n  private _scopedSyncHost: virtualFs.SyncDelegateHost<Stats> | null = null;\n\n  constructor(protected _templateRoot: Path) {\n    super();\n  }\n\n  root(): Path {\n    if (this._currentRoot === null) {\n      throw new Error('TestProjectHost must be initialized before being used.');\n    }\n\n    return this._currentRoot;\n  }\n\n  scopedSync(): virtualFs.SyncDelegateHost<Stats> {\n    if (this._currentRoot === null || this._scopedSyncHost === null) {\n      throw new Error('TestProjectHost must be initialized before being used.');\n    }\n\n    return this._scopedSyncHost;\n  }\n\n  initialize(): Observable<void> {\n    const recursiveList = (path: Path): Observable<Path> =>\n      this.list(path).pipe(\n        // Emit each fragment individually.\n        concatMap((fragments) => from(fragments)),","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/angular/angular-cli/blob/bb72145f9ab45aee29f523236b3a25cd0813a841/packages/angular_devkit/architect/testing/test-project-host.ts#L30-L66","documentation":"TestProjectHost (Architect testing utility) wraps a virtual filesystem whose effective root starts as null. root() throws this Error when read before initialize()/write() has established a real working directory, because there is no project root to resolve paths against.","triggerScenarios":"Calling host.root(), host.scopedSync(), or any path-based operation before host.initialize() or host.write() has been called; calling restore() before initialization; using the host in a beforeEach that only constructs it: new TestProjectHost(template) then immediately accessing scopedSync().","commonSituations":"Unit tests using ArchitectTestingModule or TestProjectHost directly where setup ordering is wrong; a test helper that reads host.root() to build paths before async initialize() completes (missing await); restoring the host in afterEach after an earlier failure skipped initialize().","solutions":["Call await host.initialize() (or host.write()) before any read, scopedSync(), or root() call, typically in beforeEach.","Await the initialize promise — initialize() is async, so a missing await leaves _currentRoot null even though initialize was 'called'.","Guard restore()/read paths so they only run if initialization succeeded (initialize in try/catch or check before restore)."],"exampleFix":"// before\nconst host = new TestProjectHost(templatePath);\nconst root = host.root(); // throws\n// after\nconst host = new TestProjectHost(templatePath);\nawait host.initialize();\nconst root = host.root();","handlingStrategy":"try-catch","validationCode":"// ensure initialization before use\nif (!('initialize' in host)) throw new Error('wrong host type');\nawait host.initialize();\nhost.root(); // safe now","typeGuard":"function isInitialized(host) {\n  try { host.root(); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  const root = host.root();\n  // ... use host\n} catch (e) {\n  if (e.message.includes('TestProjectHost must be initialized')) {\n    await host.initialize();\n    return runTest(host); // retry once after init\n  }\n  throw e;\n}","preventionTips":["Initialize the host in beforeEach with `await host.initialize()` (never omit await)","Call restore() only in afterEach after successful initialization","Never call root()/scopedSync() in the constructor or field initializers of a test class","Write a shared test helper that guarantees initialize-then-use ordering"],"tags":["angular","architect","testing","virtual-fs","lifecycle"],"backgroundTag":"not-initialized","analyzedSha":"bb72145f9ab45aee29f523236b3a25cd0813a841","analyzedAt":"2026-08-30T02:47:34.745Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}