refactor-jasmine-vitest removes done() from asynchronous callbacks, so migrated tests no longer wait for them

未關閉
#34,730 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
74/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
angular, typescript
領域
testing, tooling

研究方向

Read packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle.ts and the first relevant case in transformers/jasmine-lifecycle_spec.ts. Trace how transformDoneCallback handles done() inside nested functions, then run the focused transformer spec. Done means migrated tests still await nested asynchronous callbacks or the schematic leaves those cases unchanged with a TODO.

由索引模型根據 Issue 內容生成。

描述

area: @schematics/angular
Command

e2e, generate

Is this a regression?
  • Yes, this behavior used to work in the previous version
The previous version in which this bug was not present was

No response

Description

When a spec uses Jasmine's done callback inside an asynchronous callback — subscribe(...),
setTimeout(...), an event listener — the refactor-jasmine-vitest schematic deletes the done()
call and converts the test to a plain async function. The migrated test then resolves immediately,
before the callback ever runs, so it no longer waits for — or asserts anything about — the
asynchronous work:

// before (fails under Jasmine if the observable never emits: done timeout;
// fails if the assertion inside the callback throws)
it('emits the loaded data', (done) => {
  service.getData().subscribe((data) => {
    expect(data).toEqual(expected);
    done();
  });
});

// after migration (passes unconditionally if the emission is not synchronous)
it('emits the loaded data', async () => {
  service.getData().subscribe((data) => {
    expect(data).toEqual(expected);
  });
});

Two guarantees are lost at once:

  1. The completion guarantee. In Jasmine, a test whose done is never called fails with a
    timeout. The migrated test passes instantly, so "the event/emission never happened" bugs are no
    longer caught.
  2. Failure attribution. If the callback does fire later, an assertion failure inside it throws
    outside any test. Depending on timing it surfaces as an unattributed unhandled error or not at
    all (when the run ends first) — instead of failing this test.

The transformation is correct for done() at the top level of the test body and for the explicitly
handled promise forms (.then(done), .then(() => done()), .catch(done.fail)), which are
rewritten to await. The problem is only done() inside a nested function that the transformer
cannot await. The existing safety check (unhandled-done-usage) does not catch this, because
removing the nested done() counts as a handled usage.

Since Vitest has no done callback, a faithful migration for the nested case would wrap the body in
an awaited promise whose resolver takes done's place:

it('emits the loaded data', () =>
  new Promise<void>((done) => {
    service.getData().subscribe((data) => {
      expect(data).toEqual(expected);
      done();
    });
  }));

Alternatively, the schematic could leave the test unchanged with a TODO (like
unhandled-done-usage) whenever done is referenced inside a nested function. Happy to send a PR
for whichever direction you prefer.

Minimal Reproduction
  1. ng new repro --defaults && cd repro

  2. Replace the body of src/app/app.spec.ts (or any spec) with a test whose callback never fires:

    import { Subject } from 'rxjs';
    
    describe('done in async callback', () => {
      it('fails when the data never arrives', (done) => {
        const data$ = new Subject<number>();
        data$.subscribe(() => done());
        // nothing ever emits — under Jasmine this test fails with a done timeout
      });
    });
    
  3. Run it with Karma/Jasmine (ng test with the Karma builder): the test fails after the timeout —
    Async function did not complete within 5000ms.

  4. ng generate @schematics/angular:refactor-jasmine-vitest

  5. The spec becomes it('fails when the data never arrives', async () => { data$.subscribe(() => {}); });
    and ng test (Vitest runner) reports it as passing.

Exception or Error

Your Environment
_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI       : 22.3.0-next.5
Node.js           : 24.15.0
Package Manager   : yarn 1.22.22
Operating System  : darwin arm64

┌──────────────┬───────────────────┬───────────────────┐
│ Package      │ Installed Version │ Requested Version │
├──────────────┼───────────────────┼───────────────────┤
│ @angular/cli │ 22.3.0-next.5     │ ^22.3.0-next.5    │
Anything else relevant?
  • packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle.ts —
    transformDoneCallback: the body visitor recurses into nested functions and replaces any
    done() statement with an empty statement, then marks the test async.
  • The current output is asserted by the first case in
    transformers/jasmine-lifecycle_spec.ts (setTimeout with done() → async with done()
    removed), so I wanted to raise this before proposing a behavior change.
  • Not OS- or browser-specific.
主要語言
TypeScript
星號
27k
分支
11.8k
平均合併
1 天 2 小時
30 天內合併 PR
225

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

angular/angular-cli 的其他 Issue

查看 angular/angular-cli 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。