작성
·
77
0
package.json
{
"name": "jest-lecture",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"@types/jest": "^29.5.14",
"jest": "^29.7.0",
"jest-extended": "^4.0.2",
"ts-jest": "^29.2.5"
}
}
global.d.ts
import "jest-extended";
tsconfig.json
{
"files": ["global.d.ts"]
}
jest.config.js
/** @type {import('ts-jest').JestConfigWithTsJest} **/
module.exports = {
testEnvironment: "node",
transform: {
"^.+.tsx?$": ["ts-jest", {}],
},
setupFilesAfterEnv: ["./testSetup.js"],
};
testSetup.js
const matchers = require("jest-extended");
expect.extend(matchers);
위의 환경에서 설정하였으며
src/order.test.ts에서
import { first, second, third } from "./order";
test("first->second->third", () => {
const spy1 = jest.fn(first);
const spy2 = jest.fn(second);
const spy3 = jest.fn(third);
spy1();
spy2();
spy3();
console.log(spy1.mock.invocationCallOrder[0]);
console.log(spy2.mock.invocationCallOrder[0]);
console.log(spy3.mock.invocationCallOrder[0]);
// expect(spy1.mock.invocationCallOrder[0]).toBeLessThan(
// spy2.mock.invocationCallOrder[0]
// );
// expect(spy3.mock.invocationCallOrder[0]).toBeGreaterThan(
// spy2.mock.invocationCallOrder[0]
// );
expect(spy1).toHaveBeenCalledBefore(spy2);
expect(spy3).toHaveBeenCalledAfter(spy2);
});
위처럼 테스트를 실행하면
● first->second->third
TypeError: expect(...).toHaveBeenCalledBefore is not a function
19 | // spy2.mock.invocationCallOrder[0]
20 | // );
> 21 | expect(spy1).toHaveBeenCalledBefore(spy2);
| ^
22 | expect(spy3).toHaveBeenCalledAfter(spy2);
23 | });
24 |
at Object.<anonymous> (src/order.test.ts:21:16)
위와 같은 타입오류가 나오고있는데요,
무엇때문에 오류가 나는지 모르겠습니다ㅠㅠ
답변 1
0
안녕하세요, 인프런 AI 인턴입니다.
jest-extended
의 매처(matcher)가 제대로 설정되지 않았기 때문에 발생하는 오류 같습니다. jest-extended
매처를 사용하기 위해서는 expect.extend
를 호출하여 매처를 등록해야 하는데, testSetup.js
파일에서 정의한 코드가 올바르게 불러와지지 않거나 적용되지 않는 것 같아요.
다음 사항들을 점검해보세요:
setupFilesAfterEnv
설정 확인: jest.config.js
에 정의된 setupFilesAfterEnv
가 테스트 실행 전에 항상 올바르게 로드되는지 확인하세요. 해당 경로가 정확한지 체크해야 합니다.
파일 확장자 확인: matchers
를 불러오는 jest-extended
가 제대로 호환될 수 있도록 하고, testSetup.js
의 파일명이 잘못되어있는지 확인해보세요(예: testSetup.ts
로 되어야 할 가능성).
캐싱 문제 해결: Jest가 한 번 설정되면 캐시가 남아 제대로 업데이트되지 않을 수 있습니다. jest --clearCache
명령어를 실행하여 캐시를 삭제하고 테스트를 다시 시도해보세요.
Jest와 jest-extended 버전 확인: jest
와 jest-extended
의 호환성 문제일 수도 있습니다. 각 패키지의 최신 버전을 확인하고 업데이트하세요.
실행 확인: testSetup.js
의 console.log
추가하여 해당 파일이 실제로 실행되는지 여부를 체크해보세요.
위의 방법들로 문제가 해결되지 않는다면, jest-extended
의 다른 지원 방법이나 공식 문서를 참조하여 매처 확장을 시도해보세요.
도움이 되셨길 바랍니다! 문제 해결에 행운을 빌어요! 😊
jest-extended
관련 공식 URL: jest-extended