パルカワ2

最近はFlutterをやっています

Jest+TypeScriptでDOMを操作するテストを書く

ここに書かれてる方法だと document がないよ!って言われる
DOM Manipulation · Jest
JSDOMでやってあげるとよさそう。

// Hoge.test.ts
import {JSDOM} from "jsdom";

declare global {
    namespace NodeJS {
        interface Global {
            document: Document
        }
    }
}

test('js dom', () => {
    global.document = new JSDOM(`<span id="username">yoshitaka-yuriko</span>`).window.document;
    expect(document.querySelector(`#username`).textContent).toBe("yoshitaka-yuriko")
});