Jestでマウントせずに関数のテストをする
概要
Jestでマウントせずに、関数のみ実行してテストする方法。
方法
基本形
import 任意の名前1 "@/pages/ファイル名"; describe("任意の名前2", () => { it("任意の名前3", () => { const thisVal = {}; 任意の名前1.options.methods.テスト対象の関数名.call(thisVal); }) })
テスト対象の関数に引数がある場合
import 任意の名前1 "@/pages/ファイル名"; describe("任意の名前2", () => { it("任意の名前3", () => { const thisVal = {}; 任意の名前1.options.methods.テスト対象の関数名.call(thisVal, テスト対象の関数の引数1, テスト対象の関数の引数2...); }) })
関数の処理を変更したい場合
下記の例は、何も処理しないように変更する方法。
import 任意の名前1 "@/pages/ファイル名"; describe("任意の名前2", () => { it("任意の名前3", () => { const thisVal = { 処理を変更したい関数名(): void { } }; 任意の名前1.options.methods.テスト対象の関数名.call(thisVal); }) })
Dataの定義を変更する場合
import 任意の名前1 "@/pages/ファイル名"; describe("任意の名前2", () => { it("任意の名前3", () => { const thisVal = { Dataの定義名 = 変更したい値 }; 任意の名前1.options.methods.テスト対象の関数名.call(thisVal); }) })