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
'sourcecode' 카테고리의 다른 글
Java Spring Boot에서 log4j2.xml의 기본 위치를 변경하려면 어떻게 해야 합니까? (0) | 2023.03.29 |
---|---|
C#의 JSON 해석 (0) | 2023.03.19 |
module.config 내부 값의 AngularJS 의존성 주입 (0) | 2023.03.19 |
Spring Webflux + JPA :사후 대응 리포지토리는 JPA에서 지원되지 않습니다. (0) | 2023.03.19 |
MockMvc는 더 이상 스프링 부트 2.2.0에서 UTF-8 문자를 처리하지 않습니다.풀어주다 (0) | 2023.03.19 |