μ€λ₯
HTMLInputElement {} HTMLInputElement {}
Error: Not implemented: HTMLFormElement.prototype.submit
at module.exports (/home/xlmnxp/Documents/Nodejs/DownFrom99/node_modules/jsdom/lib/jsdom/browser/not-implemented.js:9:17)
at HTMLFormElementImpl.submit (/home/xlmnxp/Documents/Nodejs/DownFrom99/node_modules/jsdom/lib/jsdom/living/nodes/HTMLFormElement-impl.js:68:5)
at HTMLFormElement.submit (/home/xlmnxp/Documents/Nodejs/DownFrom99/node_modules/jsdom/lib/jsdom/living/generated/HTMLFormElement.js:18:21)
at /home/xlmnxp/Documents/Nodejs/DownFrom99/app.js:12:10
at process._tickCallback (internal/process/next_tick.js:109:7) undefined
import { JSDOM } from "jsdom";
JSDOM.fromURL("http://localhost/").then((dom:JSDOM) => {
let document = dom.window.document;
let window = dom.window;
let form = <HTMLFormElement> document.querySelector("form");
let urlField = <HTMLInputElement> document.querySelector("input[name='formvalue']");
let submitButton = <HTMLInputElement> document.querySelector("input[type='submit']");
console.log(submitButton,urlField)
urlField.value = "https://www.youtube.com/watch?v=RWjDLfx8Lww";
form.submit()
})
μ, μ€μ λ‘ μμ μ μΆμ ꡬνλμ§ μμ΅λλ€. μμλλ‘ μλν©λλ€.
@runbb μ΄μ λν λ€λ₯Έ ν΄κ²°μ± μ μ°Ύμμ΅λκΉ? κ°μ μΌμ νλ €κ³ ν©λλ€.
@domenic μ΄ κΈ°λ₯μ μννλ λ°©λ²μ λν μμ΄λμ΄κ° μμ΅λκΉ? μλ§λ λ€λ₯Έ λμκ΄?
@stefanbuck μλμ :(
@domenic μ΄κ²μ΄ ꡬνλμ§ μμ μ΄μ κ° μμ΅λκΉ? μ΄κ²μ ꡬννκΈ° μν ν 리νμ€νΈλ₯Ό μλ½νμκ² μ΅λκΉ?
https://github.com/jsdom/jsdom#unimplemented -parts-of-the-web-platformμ μ°Έμ‘° https://html.spec.whatwg.org/multipage/browsing-the-web.htmlμ ꡬνν΄μΌ νλ€λ μ μ μ΄ν΄ν΄ μ£Όμμμ€
@domenic μ΄ μ€λ₯ λ©μμ§λ₯Ό λΉνμ±ννλ κ²μ΄ μ’μ΅λλ€.
κ·Έλ κ² ν μ μμ΅λλ€. jsdomErrorsμ λν μΆκ° μ 보λ₯Ό μ°Έμ‘°νμμμ€.
μμμ΄ μ μΆλ λ μ
λ ₯ μ²λ¦¬λ₯Ό ꡬνν μ μμ΅λκΉ?
event.target[name]
μ΄(κ°) μ μλμ§ μμμ΅λλ€.
νμ¬ ν΄κ²° λ°©λ²μ form.dispatchEvent(new Event('submit'));
Jestλ₯Ό μ¬μ©νμ¬ ν
μ€νΈλ₯Ό μμ±ν λ μ΄ λ¬Έμ μ λΆλͺμ³€μ΅λλ€.
λ΄ ν΄κ²° λ°©λ²:
window.HTMLFormElement.prototype.submit = () => {}
ν
μ€νΈ μΌμ΄μ€μ κ΅¬μ± μμλ₯Ό λ§μ΄νΈνκΈ° μ μ μ΄κ²μ μΆκ°νμ΅λλ€.
μ μ΄λ λ΄ μ½μμ κΉ¨λνκ² μ μ§ν©λλ€.
λ€μμ μννμ¬ μ€λ₯λ₯Ό μ¨κΈΈ μ μμμ΅λλ€.
describe('submit', () => {
let emit;
beforeAll(() => {
({ emit } = window._virtualConsole);
});
beforeEach(() => {
window._virtualConsole.emit = jest.fn();
});
afterAll(() => {
window._virtualConsole.emit = emit;
});
it('should handle submit ', () => {
...
});
});
μ¬κΈ°μ μ’μ ν΄κ²° λ°©λ²μ΄ μμ΅λλ€. https://kula.blog/posts/test_on_submit_in_react_testing_library/
Jestλ₯Ό μ¬μ©νμ¬ ν μ€νΈλ₯Ό μμ±ν λ μ΄ λ¬Έμ μ λΆλͺμ³€μ΅λλ€.
λ΄ ν΄κ²° λ°©λ²:window.HTMLFormElement.prototype.submit = () => {}
ν μ€νΈ μΌμ΄μ€μ κ΅¬μ± μμλ₯Ό λ§μ΄νΈνκΈ° μ μ μ΄κ²μ μΆκ°νμ΅λλ€.
μ μ΄λ λ΄ μ½μμ κΉ¨λνκ² μ μ§ν©λλ€.
μλ νμΈμ! ν΄κ²° λ°©λ²μ΄ λ§μμ λ€μ§λ§ μ΄λμ ν΄μΌ ν μ§ μ λͺ¨λ₯΄κ² μ΅λλ€. λμμ£Όμκ² μ΅λκΉ? λ€μ μ½λκ° μμ΅λλ€. (main()μ 첫 λ²μ§Έ λͺ λ Ήλ¬ΈμΌλ‘ "μμ "μ μΆκ°νμ΅λλ€ μ°Έμ‘°)
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
JSDOM.fromFile("index.html", null).then(dom => {
main(dom.window);
});
function main(window){
window.HTMLFormElement.prototype.submit = () => {}
var oldAlert = window.alert;
window.alert = function() {
console.log("Alert fired with arguments: " + arguments);
oldAlert.apply(window, arguments);
};
window.document.getElementsByTagName("button")[0].click();
}
νμ§λ§ μ¬μ ν κ°μ Error: Not implemented: HTMLFormElement.prototype.submit
λͺ¨μΌκ³ μμ΅λλ€.
κ°λ₯ν ν΄κ²°μ± :
// Following code mocks window.console.error
// to ignore the "Not implemented: HTMLFormElement.prototype.submit".
//
// Problem: We use "form.onsubmit" event listener in some tests,
// but HTMLFormElement.prototype.submit is not implemented in JSDOM,
// although the tests are passing and handler fires.
//
// More:
// https://github.com/jsdom/jsdom/issues/1937
// https://github.com/facebook/jest/issues/5223#issuecomment-489422244
let origErrorConsole;
beforeEach(() => {
origErrorConsole = window.console.error;
window.console.error = (...args) => {
const firstArg = args.length > 0 && args[0];
const shouldBeIgnored =
firstArg &&
typeof firstArg === 'string' &&
firstArg.includes('Not implemented: HTMLFormElement.prototype.submit');
if (!shouldBeIgnored) {
origErrorConsole(...args);
}
}
})
afterEach(() => {
window.console.error = origErrorConsole;
})
submit
μ λν΄ μ κ²½ μ°μ§ μλλ€λ©΄ λ€μμ μ¬μ©νμ¬ μμ μ μΆμ λΉνμ±νν μλ μμ΅λλ€.
<form onsubmit="return false;">
λ μ΄μ μ€λ₯ λ©μμ§ μμ
κ°λ₯ν ν΄κ²°μ± :
// Following code mocks window.console.error // to ignore the "Not implemented: HTMLFormElement.prototype.submit". // // Problem: We use "form.onsubmit" event listener in some tests, // but HTMLFormElement.prototype.submit is not implemented in JSDOM, // although the tests are passing and handler fires. // // More: // https://github.com/jsdom/jsdom/issues/1937 // https://github.com/facebook/jest/issues/5223#issuecomment-489422244 let origErrorConsole; beforeEach(() => { origErrorConsole = window.console.error; window.console.error = (...args) => { const firstArg = args.length > 0 && args[0]; const shouldBeIgnored = firstArg && typeof firstArg === 'string' && firstArg.includes('Not implemented: HTMLFormElement.prototype.submit'); if (!shouldBeIgnored) { origErrorConsole(...args); } } }) afterEach(() => { window.console.error = origErrorConsole; })
μ΄ μ루μ μ μ€μ μ€λ₯μ μ€ν μΆμ μ κΉ¨κ³ μ°ΎκΈ°κ° λ μ΄λ €μΈ κ²μ λλ€.
μ μ
νμ
κ°μ μ½μ μ μ¬μ©νκ³ "jsdomError"
λ₯Ό μμ νλ κ²½μ° μ€ν μΆμ μ΄ μλ μμΈ κ°μ²΄λ₯Ό κ°μ ΈμμΌ ν©λλ€.
@domenic , ν μ€νΈ νμΌ μ체μμ κ°μ μ½μμ μ¬μ©νλ λ°©λ²μ μλ €μ£Όμκ² μ΅λκΉ?
ν μ€νΈ νμΌμ΄ λ€μμμ μ μν©μλ€.
import React from 'react'
import {render} from '@testing-library/react'
import ComponentWithSubmit from './ComponentWithSubmit'
test('Component with form', () => {
const {getByText} = render(<ComponentWithSubmit />)
expect(getByText('I am inside form')).toBeInTheDocument()
})
μΆμ : ν μ€νΈ λ¬λλ‘μ λλ΄μ΄ μμ΅λλ€.
μ£μ‘ν©λλ€. Jest μ¬μ© λ°©λ²μ λν μ§μμ Jestμ μμ²ν΄μΌ ν©λλ€. jsdomμ jsdomμ μ’ μμ±μΌλ‘ μ¬μ©νλ ν¨ν€μ§κ° μλ jsdom μ체μ λν μ§μλ§ μ 곡ν μ μμ΅λλ€.
μμμ μ¬μ©νλ κ΅¬μ± μμκ° μμ§λ§ μμμμ μ μΆμ μ¬μ©νμ§ μλλ° μ¬μ ν μ΄ μ€λ₯κ° νμλ©λλ€. μμ λ΄μ λ€λ₯Έ λ²νΌμ type="button"μ μΆκ°νμ¬ λ¬Έμ λ₯Ό ν΄κ²°νμ΅λλ€.
κ°μ₯ μ μ©ν λκΈ
Jestλ₯Ό μ¬μ©νμ¬ ν μ€νΈλ₯Ό μμ±ν λ μ΄ λ¬Έμ μ λΆλͺμ³€μ΅λλ€.
λ΄ ν΄κ²° λ°©λ²:
ν μ€νΈ μΌμ΄μ€μ κ΅¬μ± μμλ₯Ό λ§μ΄νΈνκΈ° μ μ μ΄κ²μ μΆκ°νμ΅λλ€.
μ μ΄λ λ΄ μ½μμ κΉ¨λνκ² μ μ§ν©λλ€.