sourcecode

joke가 구현되지 않았습니다.경보()

codebag 2023. 3. 19. 18:09
반응형

joke가 구현되지 않았습니다.경보()

나는 나의 API에 대한 테스트를 농담으로 작성했다.테스트 파일에 아래와 같이 API를 호출하는 기능을 추가하였습니다.

import AuthManager from "../Client/Modules/Auth/AuthManager";

다음과 같이 사용합니다.

test("login api resolves true", () => {
  return expect(AuthManager.login("test", "test")).resolves.toMatchObject(
    expect.objectContaining({
      accessToken: expect.any(String),
      email: expect.any(String),
      expiresIn: expect.any(Number),
      refreshToken: expect.any(String),
      userFullName: expect.any(String),
      userId: expect.any(Number)
    })
  );
});

테스트는 성공했지만 다음과 같은 오류가 발생합니다.

오류: 구현되지 않음: 창.경계.

이 문제는 어떻게 해결하나요?

의 기본 테스트 환경Jest는 에 의해 제공되는 브라우저와 같은 환경입니다.jsdom.

jsdom실제 브라우저가 제공하는 기능의 대부분을 구현합니다(글로벌 포함).window오브젝트)의 경우, 모든 것을 실장하는 것은 아닙니다.

특히 이번 경우에는jsdom실장하지 않다window.alert, 대신, swo를 던집니다.Error여기 소스코드에서 볼 수 있는 것처럼 호출됩니다.


당신의 코드가 왜 시작하는지 알고 있다면alert테스트가 정상적으로 동작하고 있는 것을 알 수 있습니다.Error그 후, 그 기능을 억제할 수 있습니다.Error공허한 실장을 제공함으로써window.alert:

test("login api resolves true", () => {
  const jsdomAlert = window.alert;  // remember the jsdom alert
  window.alert = () => {};  // provide an empty implementation for window.alert
  return expect(AuthManager.login("test", "test")).resolves.toMatchObject(
    expect.objectContaining({
      accessToken: expect.any(String),
      email: expect.any(String),
      expiresIn: expect.any(Number),
      refreshToken: expect.any(String),
      userFullName: expect.any(String),
      userId: expect.any(Number)
    })
  );  // SUCCESS
  window.alert = jsdomAlert;  // restore the jsdom alert
});

제가 이 문제를 어떻게 해결했냐면window.alert테스트 파일 맨 위에 있는 메서드(joke spy)입니다.이것은 모든 윈도우 방식에서 작동해야 합니다(내 경우 실제로 테스트 중).window.open).

꼭 전화주세요mockClear()이 오브젝트는 글로벌 오브젝트이며 콜은 테스트 후에도 지속되기 때문에 테스트에서 사용됩니다.

window.alert = jest.fn();

test("login api resolves true", () => {
  window.alert.mockClear();
  /* ... */
})

에 직면했다window.confirm이것은 각도 fw에 대한 저의 해결책입니다.

let spyOnWindow: jasmine.Spy;

beforeEach((() => {
    TestBed.configureTestingModule({
      declarations: [...],
      imports: [...],
      providers: [...]
    }).compileComponents().then(() => {
      ...
      spyOnWindow = spyOn(window,'confirm');
      ...
    });

테스트 케이스

it('showModal testing function with delete an event', () => {
spyOnWindow.and.returnValue(true);
...
}

it('showModal testing function with delete an event', () => {
spyOnWindow.and.returnValue(false);
...
}

비슷한 오류가 있었습니다만, 테스트의 경우window.print방법.

저 같은 경우에는 들어올리는 게 문제였어요.

jest.spyOn(window, 'print');

설명 블록 아래까지 이동한다.

이게 도움이 됐으면 좋겠네요.

언급URL : https://stackoverflow.com/questions/55088482/jest-not-implemented-window-alert

반응형