FrontEnd/Vue

[vue3/jest] 🚫에러 일지 - ReferenceError: location is not defined, ReferenceError: document is not defined

Grace 2023. 2. 7. 14:43

지난 게시글에 이어 발생한 jest 에러입니다.
지난 에러를 해결한 후 다시 실행했더니 아래와 같은 에러가 발생했습니다.

ReferenceError: location is not defined / ReferenceError: document is not defined

The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string.
    Consider using the "jsdom" test environment.
    
    ReferenceError: document is not defined

사실 이 에러는 에러 메세지를 읽어보시면 아실 수 있을 정도로 너무 간단한 에러인데요.
현재 저희가 jest를 진행하면 test environmnet는 node 환경을 기본으로 test가 실행됩니다.
하지만 지금 저희는 Vue 컴포넌트를 가지고와서 test를 진행하려고 하기 때문에 dom을 읽어오지 못해 발생한 에러인 듯 합니다.
test environment를 "jsdom"으로 사용하는 것을 고려하라고 하고 있네요.
보여주는 주소를 참조해서 들어가면 내용이 너무 친절하게 나와있는데요

마찬가지로 jest.config.js 파일로 들어가서 해당 코드를 추가해줍니다.

module.exports = {
... ,
    testEnvironment: 'jsdom'
}

너무 간단하고 친절한 에러였습니다.

하지만 이렇게 해도 완벽하게 에러가 핸들링 되지 않습니다.
다음 게시글에서 마지막으로 만난 vue3/jest 에러를 가지고 오겠습니다!