|
1 | 1 | ---
|
2 | 2 | id: testing-environments
|
3 |
| -title: Testing Environments |
| 3 | +title: Środowiska testujące |
4 | 4 | permalink: docs/testing-environments.html
|
5 | 5 | prev: testing-recipes.html
|
6 | 6 | ---
|
7 | 7 |
|
8 |
| -<!-- This document is intended for folks who are comfortable with JavaScript, and have probably written tests with it. It acts as a reference for the differences in testing environments for React components, and how those differences affect the tests that they write. This document also assumes a slant towards web-based react-dom components, but has notes for other renderers. --> |
| 8 | +<!-- Ten dokument został napisany dla osób zaznajomionych z JavaScriptem, którzy także prawdopodobnie pisali już w nim testy. Służy on za punkt odniesienia w kwestii różnic między środowiskami testującymi komponenty reactowe i tego, jak poszczególne różnice wpływają na tworzone testy. W rozdziale tym faworyzujemy komponenty webowe renderowane przez react-dom, ale dodajemy też informacje dotyczące innych silników renderujących. --> |
9 | 9 |
|
10 |
| -This document goes through the factors that can affect your environment and recommendations for some scenarios. |
| 10 | +W tym rozdziale opisujemy czynniki wpływające na środowisko testujące i nasze rekomendacje dla niektórych scenariuszy. |
11 | 11 |
|
12 |
| -### Test runners {#test-runners} |
| 12 | +### Narzędzia uruchamiające testy (ang. *test runners*) {#test-runners} |
13 | 13 |
|
14 |
| -Test runners like [Jest](https://jestjs.io/), [mocha](https://mochajs.org/), [ava](https://github.com/avajs/ava) let you write test suites as regular JavaScript, and run them as part of your development process. Additionally, test suites are run as part of continuous integration. |
| 14 | +Narzędzia uruchamiające testy, jak np. [Jest](https://jestjs.io/), [mocha](https://mochajs.org/) czy [ava](https://github.com/avajs/ava), pozwalają tworzyć zestawy testowe przy użyciu samego JavaScriptu, a także uruchamiać je jako część procesu tworzenia oprogramowania. Dodatkowo, testy mogą być uruchamiane w ramach procesu "ciągłej integracji" (ang. *Continuous Integration*, CI). |
15 | 15 |
|
16 |
| -- Jest is widely compatible with React projects, supporting features like mocked [modules](#mocking-modules) and [timers](#mocking-timers), and [`jsdom`](#mocking-a-rendering-surface) support. **If you use Create React App, [Jest is already included out of the box](https://facebook.github.io/create-react-app/docs/running-tests) with useful defaults.** |
17 |
| -- Libraries like [mocha](https://mochajs.org/#running-mocha-in-the-browser) work well in real browser environments, and could help for tests that explicitly need it. |
18 |
| -- End-to-end tests are used for testing longer flows across multiple pages, and require a [different setup](#end-to-end-tests-aka-e2e-tests). |
| 16 | +- Jest ma wysoką kompatybilność z projektami reactowymi i obsługuje wiele przydatnych funkcjonalności, jak [mockowanie modułów](#mocking-modules) czy [sztuczne timery](#mocking-timers). Dobrze współpracuje również z [`jsdom`](#mocking-a-rendering-surface). **Jeśli używasz Create React App, [domyślnie masz już dostęp do Jesta](https://facebook.github.io/create-react-app/docs/running-tests) z odpowiednią konfiguracją.** |
| 17 | +- Biblioteki takie jak [mocha](https://mochajs.org/#running-mocha-in-the-browser) świetnie spisują się w środowiskach przeglądarkowych, dzięki czemu mogą okazać się pomocne w przypadku niektórych testów. |
| 18 | +- Testy kompleksowe end-to-end, stosowane w przypadku dłuższych ścieżek rozciągających się na wiele stron aplikacji, wymagają [innej konfiguracji](#end-to-end-tests-aka-e2e-tests). |
19 | 19 |
|
20 |
| -### Mocking a rendering surface {#mocking-a-rendering-surface} |
| 20 | +### Mockowanie warstwy renderującej {#mocking-a-rendering-surface} |
21 | 21 |
|
22 |
| -Tests often run in an environment without access to a real rendering surface like a browser. For these environments, we recommend simulating a browser with [`jsdom`](https://github.com/jsdom/jsdom), a lightweight browser implementation that runs inside Node.js. |
| 22 | +Testy często uruchamiane są w środowiskach niemających dostępu do prawdziwej warstwy renderującej, np. przeglądarki. Zalecamy więc symulowanie zachowań przeglądarki za pomocą [`jsdom`](https://github.com/jsdom/jsdom), niewielkiej implementacji przeglądarki działającej na Node.js. |
23 | 23 |
|
24 |
| -In most cases, jsdom behaves like a regular browser would, but doesn't have features like [layout and navigation](https://github.com/jsdom/jsdom#unimplemented-parts-of-the-web-platform). This is still useful for most web-based component tests, since it runs quicker than having to start up a browser for each test. It also runs in the same process as your tests, so you can write code to examine and assert on the rendered DOM. |
| 24 | +W większości przypadków `jsdom` zachowuje się jak prawdziwa przeglądarka, lecz nie posiada niektórych funkcjonalności, jak np. [generowanie układu strony czy nawigacja](https://github.com/jsdom/jsdom#unimplemented-parts-of-the-web-platform). Mimo tego paczka z powodzeniem sprawdza się dla większości komponentów pisanych pod przeglądarkę; działa szybciej niż uruchamianie przeglądarki dla każdego testu z osobna. Ponadto, uruchamia ona testy w tym samym procesie, umożliwiając pisanie kodu sprawdzającego wyrenderowane drzewo DOM. |
25 | 25 |
|
26 |
| -Just like in a real browser, jsdom lets us model user interactions; tests can dispatch events on DOM nodes, and then observe and assert on the side effects of these actions [<small>(example)</small>](/docs/testing-recipes.html#events). |
| 26 | +Podobnie jak prawdziwa przeglądarka, `jsdom` pozwala na modelowanie interakcji użytkownika; testy mogą wywoływać zdarzenia na węzłach DOM, a następnie obserwować i sprawdzać wyniki tych akcji [<small>(przykład)</small>](/docs/testing-recipes.html#events). |
27 | 27 |
|
28 |
| -A large portion of UI tests can be written with the above setup: using Jest as a test runner, rendered to jsdom, with user interactions specified as sequences of browser events, powered by the `act()` helper [<small>(example)</small>](/docs/testing-recipes.html). For example, a lot of React's own tests are written with this combination. |
| 28 | +Przy takiej konfiguracji można śmiało napisać większość testów dla UI: Jest jako narzędzie uruchamiające testy, jsdom służący do renderowania, interakcje użytkownika określone jako sekwencje zdarzeń przeglądarkowych - a to wszystko "spięte" za pomocą funkcji pomocniczej `act()` [<small>(przykład)</small>](/docs/testing-recipes.html). Spora część testów samego Reacta jest napisana przy użyciu powyższej kombinacji. |
29 | 29 |
|
30 |
| -If you're writing a library that tests mostly browser-specific behavior, and requires native browser behavior like layout or real inputs, you could use a framework like [mocha.](https://mochajs.org/) |
| 30 | +Jeśli piszesz bibliotekę, która testuje głównie zachowania charakterystyczne dla przeglądarki, a w dodatku wymaga natywnych mechanizmów przeglądarki, jak generowanie układu strony, zalecamy skorzystanie z frameworka [mocha](https://mochajs.org/). |
31 | 31 |
|
32 |
| -In an environment where you _can't_ simulate a DOM (e.g. testing React Native components on Node.js), you could use [event simulation helpers](https://reactjs.org/docs/test-utils.html#simulate) to simulate interactions with elements. Alternately, you could use the `fireEvent` helper from [`@testing-library/react-native`](https://testing-library.com/docs/native-testing-library). |
| 32 | +W środowisku, które _uniemożliwia_ symulowanie modelu DOM (np. podczas testowania komponentów napisanych w React Native na Node.js), możesz skorzystać z [narzędzi do symulowania zdarzeń](https://reactjs.org/docs/test-utils.html#simulate) do symulowania interakcji z elementami. Alternatywnie możesz skorzystać z funkcji `fireEvent` dostarczonej przez [`@testing-library/react-native`](https://testing-library.com/docs/native-testing-library). |
33 | 33 |
|
34 |
| -Frameworks like [Cypress](https://www.cypress.io/), [puppeteer](https://github.com/GoogleChrome/puppeteer) and [webdriver](https://www.seleniumhq.org/projects/webdriver/) are useful for running [end-to-end tests](#end-to-end-tests-aka-e2e-tests). |
| 34 | +Frameworki jak [Cypress](https://www.cypress.io/), [puppeteer](https://github.com/GoogleChrome/puppeteer) czy [webdriver](https://www.seleniumhq.org/projects/webdriver/) służą do uruchamiania testów [end-to-end](#end-to-end-tests-aka-e2e-tests). |
35 | 35 |
|
36 |
| -### Mocking functions {#mocking-functions} |
| 36 | +### Mockowanie funkcji {#mocking-functions} |
37 | 37 |
|
38 |
| -When writing tests, we'd like to mock out the parts of our code that don't have equivalents inside our testing environment (e.g. checking `navigator.onLine` status inside Node.js). Tests could also spy on some functions, and observe how other parts of the test interact with them. It is then useful to be able to selectively mock these functions with test-friendly versions. |
| 38 | +Podczas pisania testów czasami chcemy podmienić części naszego kodu, które nie posiadają odpowiedników w używanym przez nas środowisku (np. sprawdzanie statusu `navigator.onLine` w Node.js). Testy mogą również śledzić niektóre funkcje i obserwować, jak pozostałe części kodu wchodzą z nimi w interakcje. Pomocna okazuje się wtedy możliwość wybiórczego zastąpienia niektórych funkcji wersjami odpowiednimi dla testów. |
39 | 39 |
|
40 |
| -This is especially useful for data fetching. It is usually preferable to use "fake" data for tests to avoid the slowness and flakiness due to fetching from real API endpoints [<small>(example)</small>](/docs/testing-recipes.html#data-fetching). This helps make the tests predictable. Libraries like [Jest](https://jestjs.io/) and [sinon](https://sinonjs.org/), among others, support mocked functions. For end-to-end tests, mocking network can be more difficult, but you might also want to test the real API endpoints in them anyway. |
| 40 | +Szczególnie przydatne okazuje się to przy pobieraniu danych. Zazwyczaj lepiej w testach używać "sztucznych" danych, aby uniknąć spowolnień czy niestabilności z powodu odwołań do prawdziwego API [<small>(przykład)</small>](/docs/testing-recipes.html#data-fetching). Dzięki takiemu zabiegowi testy są przewidywalne. Biblioteki typu [Jest](https://jestjs.io/) czy [sinon](https://sinonjs.org/) wspierają mockowanie funkcji. W przypadku testów end-to-end, mockowanie sieci może okazać się trudniejsze, choć należy tego unikać i zamiast tego testować korzystając z prawdziwego API. |
41 | 41 |
|
42 |
| -### Mocking modules {#mocking-modules} |
| 42 | +### Mockowanie modułów {#mocking-modules} |
43 | 43 |
|
44 |
| -Some components have dependencies for modules that may not work well in test environments, or aren't essential to our tests. It can be useful to selectively mock these modules out with suitable replacements [<small>(example)</small>](/docs/testing-recipes.html#mocking-modules). |
| 44 | +Niektóre komponenty mają zależności w modułach, które mogą nie działać w środowisku testowym lub które zwyczajnie nie są istotne z punktu widzenia naszych testów. Warto wtedy zastąpić te moduły czymś odpowiednim dla danego przypadku [<small>(przykład)</small>](/docs/testing-recipes.html#mocking-modules). |
45 | 45 |
|
46 |
| -On Node.js, runners like Jest [support mocking modules](https://jestjs.io/docs/en/manual-mocks). You could also use libraries like [`mock-require`](https://www.npmjs.com/package/mock-require). |
| 46 | +W Node.js [mockowanie modułów](https://jestjs.io/docs/en/manual-mocks) jest wspierane np. przez bibliotekę Jest. Można to również osiągnąć z pomocą paczki [`mock-require`](https://www.npmjs.com/package/mock-require). |
47 | 47 |
|
48 |
| -### Mocking timers {#mocking-timers} |
| 48 | +### Mockowanie timerów {#mocking-timers} |
49 | 49 |
|
50 |
| -Components might be using time-based functions like `setTimeout`, `setInterval`, or `Date.now`. In testing environments, it can be helpful to mock these functions out with replacements that let you manually "advance" time. This is great for making sure your tests run fast! Tests that are dependent on timers would still resolve in order, but quicker [<small>(example)</small>](/docs/testing-recipes.html#timers). Most frameworks, including [Jest](https://jestjs.io/docs/en/timer-mocks), [sinon](https://sinonjs.org/releases/v7.3.2/fake-timers/) and [lolex](https://github.com/sinonjs/lolex), let you mock timers in your tests. |
| 50 | +Komponenty mogą korzystać z funkcji opartych na czasie, np. `setTimeout`, `setInterval` czy `Date.now`. W środowisku testowym warto zamieniać tego typu funkcje na ich zastępniki, które pozwalają ręcznie "sterować czasem". Testy korzystające z timerów nadal będą wykonywać się w odpowiedniej kolejności, ale zdecydowanie szybciej [<small>(przykład)</small>](/docs/testing-recipes.html#timers). Większość frameworków, również [Jest](https://jestjs.io/docs/en/timer-mocks), [sinon](https://sinonjs.org/releases/v7.3.2/fake-timers/) oraz [lolex](https://github.com/sinonjs/lolex), pozwalają na mockowanie timerów w testach. |
51 | 51 |
|
52 |
| -Sometimes, you may not want to mock timers. For example, maybe you're testing an animation, or interacting with an endpoint that's sensitive to timing (like an API rate limiter). Libraries with timer mocks let you enable and disable them on a per test/suite basis, so you can explicitly choose how these tests would run. |
| 52 | +Niekiedy jednak możesz chcieć skorzystać z prawdziwych timerów, na przykład, gdy testujesz animację lub interakcję z endpointem, który zależy od czasu (np. ogranicza częstość odpytywania API). Biblioteki zawierające sztuczne timery pozwalają na łatwe włączanie i wyłączanie tego mechanizmu dla każdego zestawu testowego lub pojedynczego testu. Dzięki temu możesz zdecydować, jak poszczególne testy mają być uruchamiane. |
53 | 53 |
|
54 |
| -### End-to-end tests {#end-to-end-tests-aka-e2e-tests} |
| 54 | +### Testy end-to-end {#end-to-end-tests-aka-e2e-tests} |
55 | 55 |
|
56 |
| -End-to-end tests are useful for testing longer workflows, especially when they're critical to your business (such as payments or signups). For these tests, you'd probably want to test both how a real browser renders the whole app, fetches data from the real API endpoints, uses sessions and cookies, navigates between different links. You might also likely want to make assertions not just on the DOM state, but on the backing data as well (e.g. to verify whether the updates have been persisted to the database). |
| 56 | +Testy end-to-end są efektywne przy testowaniu dłuższych sekwencji interakcji, zwłaszcza jeśli są one krytyczne dla twojego produktu (np. płatność czy rejestracja). W takich przypadkach konieczne jest przetestowanie, jak przeglądarka renderuje całą aplikację, jak pobiera dane z API, korzysta z sesji i ciasteczek lub nawiguje pomiędzy poszczególnymi stronami. Możesz w nich sprawdzać nie tylko stan drzewa DOM, lecz także sterujące nim dane (np. weryfikując, czy dane zostały zapisane w bazie danych). |
57 | 57 |
|
58 |
| -In this scenario, you would use a framework like [Cypress](https://www.cypress.io/) or a library like [puppeteer](https://github.com/GoogleChrome/puppeteer) so you can navigate between multiple routes and assert on side effects not just in the browser, but potentially on the backend as well. |
| 58 | +Do takich scenariuszy możesz skorzystać z frameworka [Cypress](https://www.cypress.io/) lub biblioteki [puppeteer](https://github.com/GoogleChrome/puppeteer), które pozwalają nawigować pomiędzy stronami i sprawdzać rezultaty nie tylko w samej przeglądarce, ale potencjalnie również na backendzie. |
0 commit comments