jestjs

react typescript testing TypeError: MutationObserver is not a constructor

╄→гoц情女王★ 提交于 2020-12-02 02:00:49
问题 I created a React app with create-react last week. I have a simple form that displays a message when I click submit. I would like to test it, this is the test i created SampleForm.test.tsx : import React from "react"; import { render, fireEvent, screen, waitFor } from "@testing-library/react"; import SampleForm from "./SampleForm"; import "@testing-library/jest-dom/extend-expect" test("renders submits form", async () => { const str = "THIS DIDNT DO ANYTHING"; const { container, getByText,

react typescript testing TypeError: MutationObserver is not a constructor

我与影子孤独终老i 提交于 2020-12-02 01:58:09
问题 I created a React app with create-react last week. I have a simple form that displays a message when I click submit. I would like to test it, this is the test i created SampleForm.test.tsx : import React from "react"; import { render, fireEvent, screen, waitFor } from "@testing-library/react"; import SampleForm from "./SampleForm"; import "@testing-library/jest-dom/extend-expect" test("renders submits form", async () => { const str = "THIS DIDNT DO ANYTHING"; const { container, getByText,

react typescript testing TypeError: MutationObserver is not a constructor

六眼飞鱼酱① 提交于 2020-12-02 01:57:48
问题 I created a React app with create-react last week. I have a simple form that displays a message when I click submit. I would like to test it, this is the test i created SampleForm.test.tsx : import React from "react"; import { render, fireEvent, screen, waitFor } from "@testing-library/react"; import SampleForm from "./SampleForm"; import "@testing-library/jest-dom/extend-expect" test("renders submits form", async () => { const str = "THIS DIDNT DO ANYTHING"; const { container, getByText,

How can I test if a prop is passed to child?

寵の児 提交于 2020-11-29 23:53:33
问题 My component looks something like this: (It has more functionality as well as columns, but I have not included that to make the example simpler) const WeatherReport: FunctionComponent<Props> = ({ cityWeatherCollection, loading, rerender }) => { /* some use effects skipped */ /* some event handlers skipped */ const columns = React.useMemo(() => [ { header: 'City', cell: ({ name, title }: EnhancedCityWeather) => <Link to={`/${name}`} className="city">{title}</Link> }, { header: 'Temp', cell: ({

How can I test if a prop is passed to child?

余生长醉 提交于 2020-11-29 23:52:39
问题 My component looks something like this: (It has more functionality as well as columns, but I have not included that to make the example simpler) const WeatherReport: FunctionComponent<Props> = ({ cityWeatherCollection, loading, rerender }) => { /* some use effects skipped */ /* some event handlers skipped */ const columns = React.useMemo(() => [ { header: 'City', cell: ({ name, title }: EnhancedCityWeather) => <Link to={`/${name}`} className="city">{title}</Link> }, { header: 'Temp', cell: ({

Jest: shared async code between test blocks

旧时模样 提交于 2020-11-29 09:38:29
问题 I have some test code like this: test('Test', async () => { const someData = await setup() const actual = myFunc(someData.x) expect(actual.a).toEqual(someData.y) expect(actual.b).toEqual(someData.y) ... many more like this } I would like to break apart the code into multiple test blocks (because I can't even add a description message to each expect statement). If Jest supported async describe , I could do this: describe('Some group of tests', async () => { const someData = await setup() test(

Test get request in javascript

你。 提交于 2020-11-29 03:52:34
问题 I have the next get request: const getReq = () => { fetch( 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits', ) .then((response) => { let status = null; if (response.status === 200) { status = response.status; } else { status = 'error'; } if (status === 200) { alert('success') } else { alert('Error') } return response.json(); }) .then((commits) =>{ return commits.map(i=> i.sha[0]) }); }; getReq() I want to make 3 tests for this request: if I get the first sha from

Necessary to use expect.assertions() if you're awaiting any async function calls?

陌路散爱 提交于 2020-11-26 06:29:36
问题 I've found a lot of this sort of thing when refactoring our Jest test suites: it('calls the API and throws an error', async () => { expect.assertions(2); try { await login('email', 'password'); } catch (error) { expect(error.name).toEqual('Unauthorized'); expect(error.status).toEqual(401); } }); I believe the expect.assertions(2) line is redundant here, and can safely be removed, because we already await the async call to login() . Am I correct, or have I misunderstood how expect.assertions

Necessary to use expect.assertions() if you're awaiting any async function calls?

*爱你&永不变心* 提交于 2020-11-26 06:22:32
问题 I've found a lot of this sort of thing when refactoring our Jest test suites: it('calls the API and throws an error', async () => { expect.assertions(2); try { await login('email', 'password'); } catch (error) { expect(error.name).toEqual('Unauthorized'); expect(error.status).toEqual(401); } }); I believe the expect.assertions(2) line is redundant here, and can safely be removed, because we already await the async call to login() . Am I correct, or have I misunderstood how expect.assertions

Mocking methods on a Vue instance during TDD

你说的曾经没有我的故事 提交于 2020-11-24 16:15:25
问题 I'm learning TDD whilst building my Vue app, and trying to abide by the strict laws of only writing enough production code to satisfy a failing unit test. I am really enjoying this approach, but I have run into a roadblock in regards to adding methods to a Vue instance, and testing that they have been called when the event fires from the element in the template. I cannot find any suggestions as to how I can mock a Vue method given that if I mock the proxied method, it ends up not being called