frontend_testing.md 37,6 КБ
Newer Older
1
2
3
# Frontend testing standards and style guidelines

There are two types of test suites you'll encounter while developing frontend code
GitLab Bot's avatar
GitLab Bot включено в состав коммита
4
at GitLab. We use Karma with Jasmine and Jest for JavaScript unit and integration testing,
5
6
7
and RSpec feature tests with Capybara for e2e (end-to-end) integration testing.

Unit and feature tests need to be written for all new features.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
8
Most of the time, you should use [RSpec](https://github.com/rspec/rspec-rails#feature-specs) for your feature tests.
9
10
11
12
13
14
15

Regression tests should be written for bug fixes to prevent them from recurring
in the future.

See the [Testing Standards and Style Guidelines](index.md) page for more
information on general testing practices at GitLab.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
16
17
## Vue.js testing

GitLab Bot's avatar
GitLab Bot включено в состав коммита
18
If you are looking for a guide on Vue component testing, you can jump right away to this [section](../fe_guide/vue.md#testing-vue-components).
GitLab Bot's avatar
GitLab Bot включено в состав коммита
19

Luke Bennett's avatar
Luke Bennett включено в состав коммита
20
21
## Jest

Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
22
23
We have started to migrate frontend tests to the [Jest](https://jestjs.io) testing framework (see also the corresponding
[epic](https://gitlab.com/groups/gitlab-org/-/epics/895)).
Luke Bennett's avatar
Luke Bennett включено в состав коммита
24
25
26

Jest tests can be found in `/spec/frontend` and `/ee/spec/frontend` in EE.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
27
28
NOTE: **Note:**
Most examples have a Jest and Karma example. See the Karma examples only as explanation to what's going on in the code, should you stumble over some use cases during your discovery. The Jest examples are the one you should follow.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
29
30
31

## Karma test suite

GitLab Bot's avatar
GitLab Bot включено в состав коммита
32
While GitLab is switching over to [Jest](https://jestjs.io) you'll still find Karma tests in our application. [Karma](http://karma-runner.github.io/) is a test runner which uses [Jasmine](https://jasmine.github.io/) as its test
GitLab Bot's avatar
GitLab Bot включено в состав коммита
33
34
35
36
37
38
39
40
41
42
43
44
framework. Jest also uses Jasmine as foundation, that's why it's looking quite similar.

Karma tests live in `spec/javascripts/` and `/ee/spec/javascripts` in EE.

`app/assets/javascripts/behaviors/autosize.js`
might have a corresponding `spec/javascripts/behaviors/autosize_spec.js` file.

Keep in mind that in a CI environment, these tests are run in a headless
browser and you will not have access to certain APIs, such as
[`Notification`](https://developer.mozilla.org/en-US/docs/Web/API/notification),
which have to be stubbed.

Paul Slaughter's avatar
Paul Slaughter включено в состав коммита
45
46
47
48
49
50
### When should I use Jest over Karma?

If you need to update an existing Karma test file (found in `spec/javascripts`), you do not
need to migrate the whole spec to Jest. Simply updating the Karma spec to test your change
is fine. It is probably more appropriate to migrate to Jest in a separate merge request.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
51
If you create a new test file, it needs to be created in Jest. This will
Paul Slaughter's avatar
Paul Slaughter включено в состав коммита
52
53
54
55
56
help support our migration and we think you'll love using Jest.

As always, please use discretion. Jest solves a lot of issues we experienced in Karma and
provides a better developer experience, however there are potentially unexpected issues
which could arise (especially with testing against browser specific features).
Luke Bennett's avatar
Luke Bennett включено в состав коммита
57

Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
58
59
### Differences to Karma

GitLab Bot's avatar
GitLab Bot включено в состав коммита
60
- Jest runs in a Node.js environment, not in a browser. Support for running Jest tests in a browser [is planned](https://gitlab.com/gitlab-org/gitlab/-/issues/26982).
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
61
- Because Jest runs in a Node.js environment, it uses [jsdom](https://github.com/jsdom/jsdom) by default. See also its [limitations](#limitations-of-jsdom) below.
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
62
- Jest does not have access to Webpack loaders or aliases.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
63
  The aliases used by Jest are defined in its [own configuration](https://gitlab.com/gitlab-org/gitlab/blob/master/jest.config.js).
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
64
65
- All calls to `setTimeout` and `setInterval` are mocked away. See also [Jest Timer Mocks](https://jestjs.io/docs/en/timer-mocks).
- `rewire` is not required because Jest supports mocking modules. See also [Manual Mocks](https://jestjs.io/docs/en/manual-mocks).
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
66
67
68
- No [context object](https://jasmine.github.io/tutorials/your_first_suite#section-The_%3Ccode%3Ethis%3C/code%3E_keyword) is passed to tests in Jest.
  This means sharing `this.something` between `beforeEach()` and `it()` for example does not work.
  Instead you should declare shared variables in the context that they are needed (via `const` / `let`).
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
69
70
71
72
73
- The following will cause tests to fail in Jest:
  - Unmocked requests.
  - Unhandled Promise rejections.
  - Calls to `console.warn`, including warnings from libraries like Vue.

Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
74
75
76
77
78
79
80
81
82
### Limitations of jsdom

As mentioned [above](#differences-to-karma), Jest uses jsdom instead of a browser for running tests.
This comes with a number of limitations, namely:

- [No scrolling support](https://github.com/jsdom/jsdom/blob/15.1.1/lib/jsdom/browser/Window.js#L623-L625)
- [No element sizes or positions](https://github.com/jsdom/jsdom/blob/15.1.1/lib/jsdom/living/nodes/Element-impl.js#L334-L371)
- [No layout engine](https://github.com/jsdom/jsdom/issues/1322) in general

GitLab Bot's avatar
GitLab Bot включено в состав коммита
83
See also the issue for [support running Jest tests in browsers](https://gitlab.com/gitlab-org/gitlab/-/issues/26982).
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
84

Simon Knox's avatar
Simon Knox включено в состав коммита
85
86
87
88
### Debugging Jest tests

Running `yarn jest-debug` will run Jest in debug mode, allowing you to debug/inspect as described in the [Jest docs](https://jestjs.io/docs/en/troubleshooting#tests-are-failing-and-you-don-t-know-why).

Luke Bennett's avatar
Luke Bennett включено в состав коммита
89
90
91
### Timeout error

The default timeout for Jest is set in
GitLab Bot's avatar
GitLab Bot включено в состав коммита
92
[`/spec/frontend/test_setup.js`](https://gitlab.com/gitlab-org/gitlab/blob/master/spec/frontend/test_setup.js).
Luke Bennett's avatar
Luke Bennett включено в состав коммита
93
94
95
96
97

If your test exceeds that time, it will fail.

If you cannot improve the performance of the tests, you can increase the timeout
for a specific test using
GitLab Bot's avatar
GitLab Bot включено в состав коммита
98
[`setTestTimeout`](https://gitlab.com/gitlab-org/gitlab/blob/master/spec/frontend/helpers/timeout.js).
Luke Bennett's avatar
Luke Bennett включено в состав коммита
99
100

```javascript
Luke Bennett's avatar
Luke Bennett включено в состав коммита
101
import { setTestTimeout } from 'helpers/timeout';
Luke Bennett's avatar
Luke Bennett включено в состав коммита
102
103

describe('Component', () => {
Luke Bennett's avatar
Luke Bennett включено в состав коммита
104
105
106
107
  it('does something amazing', () => {
    setTestTimeout(500);
    // ...
  });
Luke Bennett's avatar
Luke Bennett включено в состав коммита
108
109
110
111
112
});
```

Remember that the performance of each test depends on the environment.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
113
## What and how to test
Mike Greiling's avatar
Mike Greiling включено в состав коммита
114

GitLab Bot's avatar
GitLab Bot включено в состав коммита
115
Before jumping into more gritty details about Jest-specific workflows like mocks and spies, we should briefly cover what to test with Jest.
Martin Hanzel's avatar
Martin Hanzel включено в состав коммита
116

GitLab Bot's avatar
GitLab Bot включено в состав коммита
117
### Don't test the library
Martin Hanzel's avatar
Martin Hanzel включено в состав коммита
118

GitLab Bot's avatar
GitLab Bot включено в состав коммита
119
120
Libraries are an integral part of any JavaScript developer's life. The general advice would be to not test library internals, but expect that the library knows what it's supposed to do and has test coverage on its own.
A general example could be something like this
Martin Hanzel's avatar
Martin Hanzel включено в состав коммита
121

GitLab Bot's avatar
GitLab Bot включено в состав коммита
122
123
```javascript
import { convertToFahrenheit } from 'temperatureLibrary'
Martin Hanzel's avatar
Martin Hanzel включено в состав коммита
124

GitLab Bot's avatar
GitLab Bot включено в состав коммита
125
126
127
128
function getFahrenheit(celsius) {
  return convertToFahrenheit(celsius)
}
```
Martin Hanzel's avatar
Martin Hanzel включено в состав коммита
129

GitLab Bot's avatar
GitLab Bot включено в состав коммита
130
It does not make sense to test our `getFahrenheit` function because underneath it does nothing else but invoking the library function, and we can expect that one is working as intended. (Simplified, I know)
Martin Hanzel's avatar
Martin Hanzel включено в состав коммита
131

GitLab Bot's avatar
GitLab Bot включено в состав коммита
132
Let's take a short look into Vue land. Vue is a critical part of the GitLab JavaScript codebase. When writing specs for Vue components, a common gotcha is to actually end up testing Vue provided functionality, because it appears to be the easiest thing to test. Here's an example taken from our codebase.
Mike Greiling's avatar
Mike Greiling включено в состав коммита
133

GitLab Bot's avatar
GitLab Bot включено в состав коммита
134
135
136
137
138
139
140
141
142
```javascript
// Component
{
  computed: {
    hasMetricTypes() {
      return this.metricTypes.length;
    },
}
```
Martin Hanzel's avatar
Martin Hanzel включено в состав коммита
143

GitLab Bot's avatar
GitLab Bot включено в состав коммита
144
and here's the corresponding spec
Mike Greiling's avatar
Mike Greiling включено в состав коммита
145

GitLab Bot's avatar
GitLab Bot включено в состав коммита
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
```javascript
 describe('computed', () => {
    describe('hasMetricTypes', () => {
      it('returns true if metricTypes exist', () => {
        factory({ metricTypes });
        expect(wrapper.vm.hasMetricTypes).toBe(2);
      });

      it('returns true if no metricTypes exist', () => {
        factory();
        expect(wrapper.vm.hasMetricTypes).toBe(0);
      });
    });
});
```
Martin Hanzel's avatar
Martin Hanzel включено в состав коммита
161

GitLab Bot's avatar
GitLab Bot включено в состав коммита
162
Testing the `hasMetricTypes` computed prop would seem like a given, but to test if the computed property is returning the length of `metricTypes`, is testing the Vue library itself. There is no value in this, besides it adding to the test suite. Better is to test it in the way the user interacts with it. Probably through the template.
Martin Hanzel's avatar
Martin Hanzel включено в состав коммита
163

GitLab Bot's avatar
GitLab Bot включено в состав коммита
164
Keep an eye out for these kinds of tests, as they just make updating logic more fragile and tedious than it needs to be. This is also true for other libraries.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
165

GitLab Bot's avatar
GitLab Bot включено в состав коммита
166
Some more examples can be found in the [Frontend unit tests section](testing_levels.md#frontend-unit-tests)
GitLab Bot's avatar
GitLab Bot включено в состав коммита
167

GitLab Bot's avatar
GitLab Bot включено в состав коммита
168
### Don't test your mock
GitLab Bot's avatar
GitLab Bot включено в состав коммита
169

GitLab Bot's avatar
GitLab Bot включено в состав коммита
170
Another common gotcha is that the specs end up verifying the mock is working. If you are using mocks, the mock should support the test, but not be the target of the test.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
171
172

```javascript
GitLab Bot's avatar
GitLab Bot включено в состав коммита
173
174
const spy = jest.spyOn(idGenerator, 'create')
spy.mockImplementation = () = '1234'
GitLab Bot's avatar
GitLab Bot включено в состав коммита
175

GitLab Bot's avatar
GitLab Bot включено в состав коммита
176
// Bad
GitLab Bot's avatar
GitLab Bot включено в состав коммита
177
178
expect(idGenerator.create()).toBe('1234')

GitLab Bot's avatar
GitLab Bot включено в состав коммита
179
// Good: actually focusing on the logic of your component and just leverage the controllable mocks output
GitLab Bot's avatar
GitLab Bot включено в состав коммита
180
expect(wrapper.find('div').html()).toBe('<div id="1234">...</div>')
GitLab Bot's avatar
GitLab Bot включено в состав коммита
181
182
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
183
### Follow the user
184

GitLab Bot's avatar
GitLab Bot включено в состав коммита
185
The line between unit and integration tests can be quite blurry in a component heavy world. The most important guideline to give is the following:
186

GitLab Bot's avatar
GitLab Bot включено в состав коммита
187
188
- Write clean unit tests if there is actual value in testing a complex piece of logic in isolation to prevent it from breaking in the future
- Otherwise, try to write your specs as close to the user's flow as possible
189

GitLab Bot's avatar
GitLab Bot включено в состав коммита
190
For example, it's better to use the generated markup to trigger a button click and validate the markup changed accordingly than to call a method manually and verify data structures or computed properties. There's always the chance of accidentally breaking the user flow, while the tests pass and provide a false sense of security.
191

GitLab Bot's avatar
GitLab Bot включено в состав коммита
192
## Common practices
193

GitLab Bot's avatar
GitLab Bot включено в состав коммита
194
Following you'll find some general common practices you will find as part of our test suite. Should you stumble over something not following this guide, ideally fix it right away. 🎉
GitLab Bot's avatar
GitLab Bot включено в состав коммита
195

GitLab Bot's avatar
GitLab Bot включено в состав коммита
196
197
### How to query DOM elements

GitLab Bot's avatar
GitLab Bot включено в состав коммита
198
199
200
201
202
203
When it comes to querying DOM elements in your tests, it is best to uniquely and semantically target
the element.

Preferentially, this is done by targeting text the user actually sees using [DOM Testing Library](https://testing-library.com/docs/dom-testing-library/intro).
When selecting by text it is best to use [`getByRole` or `findByRole`](https://testing-library.com/docs/dom-testing-library/api-queries#byrole)
as these enforce accessibility best practices as well. The examples below demonstrate the order of preference.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
204

GitLab Bot's avatar
GitLab Bot включено в состав коммита
205
206
Sometimes this cannot be done feasibly. In these cases, adding test attributes to simplify the
selectors might be the best option.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
207

GitLab Bot's avatar
GitLab Bot включено в состав коммита
208
- A semantic attribute like `name` (also verifies that `name` was setup properly)
GitLab Bot's avatar
GitLab Bot включено в состав коммита
209
210
211
- A `data-testid` attribute ([recommended by maintainers of `@vue/test-utils`](https://github.com/vuejs/vue-test-utils/issues/1498#issuecomment-610133465))
- a Vue `ref` (if using `@vue/test-utils`)

GitLab Bot's avatar
GitLab Bot включено в состав коммита
212
```javascript
GitLab Bot's avatar
GitLab Bot включено в состав коммита
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { mount, shallowMount } from '@vue/test-utils'
import { getByRole, getByText } from '@testing-library/dom'

let wrapper
let el

const createComponent = (mountFn = shallowMount) => {
  wrapper = mountFn(Component)
  el = wrapper.vm.$el // reference to the container element
}

beforeEach(() => {
  createComponent()
})


it('exists', () => {
  // Best

GitLab Bot's avatar
GitLab Bot включено в состав коммита
232
  // NOTE: both mount and shallowMount work as long as a DOM element is available
GitLab Bot's avatar
GitLab Bot включено в состав коммита
233
  // Finds a properly formatted link with an accessible name of "Click Me"
GitLab Bot's avatar
GitLab Bot включено в состав коммита
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
  getByRole(el, 'link', { name: /Click Me/i })
  getByRole(el, 'link', { name: 'Click Me' })
  // Finds any element with the text "Click Me"
  getByText(el, 'Click Me')
  // Regex is also available
  getByText(el, /Click Me/i)

  // Good
  wrapper.find('input[name=foo]');
  wrapper.find('[data-testid="foo"]');
  wrapper.find({ ref: 'foo'});

  // Bad
  wrapper.find('.js-foo');
  wrapper.find('.btn-primary');
  wrapper.find('.qa-foo-component');
  wrapper.find('[data-qa-selector="foo"]');
});

// Good
GitLab Bot's avatar
GitLab Bot включено в состав коммита
254
255
256
it('exists', () => {
    wrapper.find(FooComponent);
    wrapper.find('input[name=foo]');
GitLab Bot's avatar
GitLab Bot включено в состав коммита
257
    wrapper.find('[data-testid="foo"]');
GitLab Bot's avatar
GitLab Bot включено в состав коммита
258
259
260
261
262
263
264
265
    wrapper.find({ ref: 'foo'});
});
```

It is not recommended that you add `.js-*` classes just for testing purposes. Only do this if there are no other feasible options available.

Do not use a `.qa-*` class or `data-qa-selector` attribute for any tests other than QA end-to-end testing.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
### Querying for child components

When testing Vue components with `@vue/test-utils` another possible approach is querying for child
components instead of querying for DOM nodes. This assumes that implementation details of behavior
under test should be covered by that component's individual unit test. There is no strong preference
in writing DOM or component queries as long as your tests reliably cover expected behavior for the
component under test.

Example:

```javascript
it('exists', () => {
    wrapper.find(FooComponent);
});
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
282
### Naming unit tests
283
284
285
286

When writing describe test blocks to test specific functions/methods,
please use the method name as the describe block name.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
287
288
**Bad**:

289
```javascript
GitLab Bot's avatar
GitLab Bot включено в состав коммита
290
describe('#methodName', () => {
291
292
293
294
295
  it('passes', () => {
    expect(true).toEqual(true);
  });
});

GitLab Bot's avatar
GitLab Bot включено в состав коммита
296
describe('.methodName', () => {
297
298
299
300
  it('passes', () => {
    expect(true).toEqual(true);
  });
});
GitLab Bot's avatar
GitLab Bot включено в состав коммита
301
```
302

GitLab Bot's avatar
GitLab Bot включено в состав коммита
303
304
305
306
**Good**:

```javascript
describe('methodName', () => {
307
308
309
310
311
  it('passes', () => {
    expect(true).toEqual(true);
  });
});
```
Mike Greiling's avatar
Mike Greiling включено в состав коммита
312

GitLab Bot's avatar
GitLab Bot включено в состав коммита
313
### Testing promises
314

GitLab Bot's avatar
GitLab Bot включено в состав коммита
315
316
317
318
319
320
321
322
323
When testing Promises you should always make sure that the test is asynchronous and rejections are handled. It's now possible to use the `async/await` syntax in the test suite:

```javascript
it('tests a promise', async () => {
  const users = await fetchUsers()
  expect(users.length).toBe(42)
});

it('tests a promise rejection', async () => {
GitLab Bot's avatar
GitLab Bot включено в состав коммита
324
  await expect(user.getUserName(1)).rejects.toThrow('User with 1 not found.');
GitLab Bot's avatar
GitLab Bot включено в состав коммита
325
326
327
});
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
328
329
330
331
332
You can also simply return a promise from the test function.

NOTE: **Note:**
Using the `done` and `done.fail` callbacks is discouraged when working with
promises. They should only be used when testing callback-based code.
333

GitLab Bot's avatar
GitLab Bot включено в состав коммита
334
335
**Bad**:

336
```javascript
GitLab Bot's avatar
GitLab Bot включено в состав коммита
337
// missing return
GitLab Bot's avatar
GitLab Bot включено в состав коммита
338
339
340
341
342
343
it('tests a promise', () => {
  promise.then(data => {
    expect(data).toBe(asExpected);
  });
});

GitLab Bot's avatar
GitLab Bot включено в состав коммита
344
// uses done/done.fail
GitLab Bot's avatar
GitLab Bot включено в состав коммита
345
it('tests a promise', done => {
346
  promise
GitLab Bot's avatar
GitLab Bot включено в состав коммита
347
348
    .then(data => {
      expect(data).toBe(asExpected);
349
350
    })
    .then(done)
GitLab Bot's avatar
GitLab Bot включено в состав коммита
351
    .catch(done.fail);
352
});
GitLab Bot's avatar
GitLab Bot включено в состав коммита
353
```
354

GitLab Bot's avatar
GitLab Bot включено в состав коммита
355
356
357
**Good**:

```javascript
GitLab Bot's avatar
GitLab Bot включено в состав коммита
358
359
360
// verifying a resolved promise
it('tests a promise', () => {
  return promise
Mike Greiling's avatar
Mike Greiling включено в состав коммита
361
    .then(data => {
362
      expect(data).toBe(asExpected);
GitLab Bot's avatar
GitLab Bot включено в состав коммита
363
    });
364
365
});

GitLab Bot's avatar
GitLab Bot включено в состав коммита
366
367
368
369
370
371
372
373
// verifying a resolved promise using Jest's `resolves` matcher
it('tests a promise', () => {
  return expect(promise).resolves.toBe(asExpected);
});

// verifying a rejected promise using Jest's `rejects` matcher
it('tests a promise rejection', () => {
  return expect(promise).rejects.toThrow(expectedError);
374
375
376
});
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
377
### Manipulating Time
378

GitLab Bot's avatar
GitLab Bot включено в состав коммита
379
Sometimes we have to test time-sensitive code. For example, recurring events that run every X amount of seconds or similar. Here you'll find some strategies to deal with that:
380

GitLab Bot's avatar
GitLab Bot включено в состав коммита
381
#### `setTimeout()` / `setInterval()` in application
Mike Greiling's avatar
Mike Greiling включено в состав коммита
382

GitLab Bot's avatar
GitLab Bot включено в состав коммита
383
384
385
386
If the application itself is waiting for some time, mock await the waiting. In Jest this is already
[done by default](https://gitlab.com/gitlab-org/gitlab/blob/a2128edfee799e49a8732bfa235e2c5e14949c68/jest.config.js#L47)
(see also [Jest Timer Mocks](https://jestjs.io/docs/en/timer-mocks)). In Karma you can use the
[Jasmine mock clock](https://jasmine.github.io/api/2.9/Clock.html).
Mike Greiling's avatar
Mike Greiling включено в состав коммита
387

GitLab Bot's avatar
GitLab Bot включено в состав коммита
388
389
390
391
392
393
```javascript
const doSomethingLater = () => {
  setTimeout(() => {
    // do something
  }, 4000);
};
Luke Bennett's avatar
Luke Bennett включено в состав коммита
394
```
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
395

GitLab Bot's avatar
GitLab Bot включено в состав коммита
396
**in Jest:**
Mike Greiling's avatar
Mike Greiling включено в состав коммита
397

GitLab Bot's avatar
GitLab Bot включено в состав коммита
398
399
400
401
```javascript
it('does something', () => {
  doSomethingLater();
  jest.runAllTimers();
Mike Greiling's avatar
Mike Greiling включено в состав коммита
402

GitLab Bot's avatar
GitLab Bot включено в состав коммита
403
  expect(something).toBe('done');
Mike Greiling's avatar
Mike Greiling включено в состав коммита
404
405
406
});
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
407
**in Karma:**
Mike Greiling's avatar
Mike Greiling включено в состав коммита
408

GitLab Bot's avatar
GitLab Bot включено в состав коммита
409
410
411
```javascript
it('does something', () => {
  jasmine.clock().install();
412

GitLab Bot's avatar
GitLab Bot включено в состав коммита
413
414
415
416
417
418
419
420
421
  doSomethingLater();
  jasmine.clock().tick(4000);

  expect(something).toBe('done');
  jasmine.clock().uninstall();
});
```

### Waiting in tests
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
422

Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
423
424
Sometimes a test needs to wait for something to happen in the application before it continues.
Avoid using [`setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout)
GitLab Bot's avatar
GitLab Bot включено в состав коммита
425
because it makes the reason for waiting unclear and if used within Karma with a time larger than zero it will slow down our test suite.
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
426
427
Instead use one of the following approaches.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
428
#### Promises and Ajax calls
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447

Register handler functions to wait for the `Promise` to be resolved.

```javascript
const askTheServer = () => {
  return axios
    .get('/endpoint')
    .then(response => {
      // do something
    })
    .catch(error => {
      // do something else
    });
};
```

**in Jest:**

```javascript
GitLab Bot's avatar
GitLab Bot включено в состав коммита
448
449
450
it('waits for an Ajax call', async () => {
  await askTheServer()
  expect(something).toBe('done');
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
});
```

**in Karma:**

```javascript
it('waits for an Ajax call', done => {
  askTheServer()
    .then(() => {
      expect(something).toBe('done');
    })
    .then(done)
    .catch(done.fail);
});
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
467
If you are not able to register handlers to the `Promise`, for example because it is executed in a synchronous Vue life cycle hook, please take a look at the [waitFor](#wait-until-axios-requests-finish) helpers or you can flush all pending `Promise`s:
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
468
469
470
471
472
473
474
475
476
477
478
479

**in Jest:**

```javascript
it('waits for an Ajax call', () => {
  synchronousFunction();
  jest.runAllTicks();

  expect(something).toBe('done');
});
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
480
#### Vue rendering
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512

To wait until a Vue component is re-rendered, use either of the equivalent
[`Vue.nextTick()`](https://vuejs.org/v2/api/#Vue-nextTick) or `vm.$nextTick()`.

**in Jest:**

```javascript
it('renders something', () => {
  wrapper.setProps({ value: 'new value' });

  return wrapper.vm.$nextTick().then(() => {
    expect(wrapper.text()).toBe('new value');
  });
});
```

**in Karma:**

```javascript
it('renders something', done => {
  wrapper.setProps({ value: 'new value' });

  wrapper.vm
    .$nextTick()
    .then(() => {
      expect(wrapper.text()).toBe('new value');
    })
    .then(done)
    .catch(done.fail);
});
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
513
#### Events
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543

If the application triggers an event that you need to wait for in your test, register an event handler which contains
the assertions:

```javascript
it('waits for an event', done => {
  eventHub.$once('someEvent', eventHandler);

  someFunction();

  function eventHandler() {
    expect(something).toBe('done');
    done();
  }
});
```

In Jest you can also use a `Promise` for this:

```javascript
it('waits for an event', () => {
  const eventTriggered = new Promise(resolve => eventHub.$once('someEvent', resolve));

  someFunction();

  return eventTriggered.then(() => {
    expect(something).toBe('done');
  });
});
```
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
544

GitLab Bot's avatar
GitLab Bot включено в состав коммита
545
### Ensuring that tests are isolated
GitLab Bot's avatar
GitLab Bot включено в состав коммита
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575

Tests are normally architected in a pattern which requires a recurring setup and breakdown of the component under test. This is done by making use of the `beforeEach` and `afterEach` hooks.

Example

```javascript
  let wrapper;

  beforeEach(() => {
    wrapper = mount(Component);
  });

  afterEach(() => {
    wrapper.destroy();
  });
```

When looking at this initially you'd suspect that the component is setup before each test and then broken down afterwards, providing isolation between tests.

This is however not entirely true as the `destroy` method does not remove everything which has been mutated on the `wrapper` object. For functional components, destroy only removes the rendered DOM elements from the document.

In order to ensure that a clean wrapper object and DOM are being used in each test, the breakdown of the component should rather be performed as follows:

```javascript
  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
  });
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
576
See also the [Vue Test Utils documentation on `destroy`](https://vue-test-utils.vuejs.org/api/wrapper/#destroy).
GitLab Bot's avatar
GitLab Bot включено в состав коммита
577

GitLab Bot's avatar
GitLab Bot включено в состав коммита
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
### Jest best practices

> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/34209) in GitLab 13.2.

#### Prefer `toBe` over `toEqual` when comparing primitive values

Jest has [`toBe`](https://jestjs.io/docs/en/expect#tobevalue) and
[`toEqual`](https://jestjs.io/docs/en/expect#toequalvalue) matchers.
As [`toBe`](https://jestjs.io/docs/en/expect#tobevalue) uses
[`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is)
to compare values, it's faster (by default) than using `toEqual`.
While the latter will eventually fallback to leverage [`Object.is`](https://github.com/facebook/jest/blob/master/packages/expect/src/jasmineUtils.ts#L91),
for primitive values, it should only be used when complex objects need a comparison.

Examples:

```javascript
const foo = 1;

GitLab Bot's avatar
GitLab Bot включено в состав коммита
597
// Bad
GitLab Bot's avatar
GitLab Bot включено в состав коммита
598
expect(foo).toEqual(1);
GitLab Bot's avatar
GitLab Bot включено в состав коммита
599
600
601

// Good
expect(foo).toBe(1);
GitLab Bot's avatar
GitLab Bot включено в состав коммита
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
```

#### Prefer more befitting matchers

Jest provides useful matchers like `toHaveLength` or `toBeUndefined` to make your tests more
readable and to produce more understandable error messages. Check their docs for the
[full list of matchers](https://jestjs.io/docs/en/expect#methods).

Examples:

```javascript
const arr = [1, 2];

// prints:
// Expected length: 1
// Received length: 2
expect(arr).toHaveLength(1);

// prints:
// Expected: 1
// Received: 2
expect(arr.length).toBe(1);

// prints:
// expect(received).toBe(expected) // Object.is equality
// Expected: undefined
// Received: "bar"
const foo = 'bar';
expect(foo).toBe(undefined);

// prints:
// expect(received).toBeUndefined()
// Received: "bar"
const foo = 'bar';
expect(foo).toBeUndefined();
```

#### Avoid using `toBeTruthy` or `toBeFalsy`

Jest also provides following matchers: `toBeTruthy` and `toBeFalsy`. We should not use them because
they make tests weaker and produce false-positive results.

For example, `expect(someBoolean).toBeFalsy()` passes when `someBoolean === null`, and when
`someBoolean === false`.

#### Tricky `toBeDefined` matcher

Jest has the tricky `toBeDefined` matcher that can produce false positive test. Because it
[validates](https://github.com/facebook/jest/blob/master/packages/expect/src/matchers.ts#L204)
the given value for `undefined` only.

```javascript
GitLab Bot's avatar
GitLab Bot включено в состав коммита
654
// Bad: if finder returns null, the test will pass
GitLab Bot's avatar
GitLab Bot включено в состав коммита
655
expect(wrapper.find('foo')).toBeDefined();
GitLab Bot's avatar
GitLab Bot включено в состав коммита
656
657
658

// Good
expect(wrapper.find('foo').exists()).toBe(true);
GitLab Bot's avatar
GitLab Bot включено в состав коммита
659
660
661
662
663
664
665
666
667
668
669
```

#### Avoid using `setImmediate`

Try to avoid using `setImmediate`. `setImmediate` is an ad-hoc solution to run your callback after
the I/O completes. And it's not part of the Web API, hence, we target NodeJS environments in our
unit tests.

Instead of `setImmediate`, use `jest.runAllTimers` or `jest.runOnlyPendingTimers` to run pending timers.
The latter is useful when you have `setInterval` in the code. **Remember:** our Jest configuration uses fake timers.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
## Avoid non-deterministic specs

Non-determinism is the breeding ground for flaky and brittle specs. Such specs end up breaking the CI pipeline, interrupting the work flow of other contributors.

1. Make sure your test subject's collaborators (e.g., axios, apollo, lodash helpers) and test environment (e.g., Date) behave consistently across systems and over time.
1. Make sure tests are focused and not doing "extra work" (e.g., needlessly creating the test subject more than once in an individual test)

### Faking `Date` for determinism

Consider using `useFakeDate` to ensure a consistent value is returned with every `new Date()` or `Date.now()`.

```javascript
import { useFakeDate } from 'helpers/fake_date';

describe('cool/component', () => {
  useFakeDate();

  // ...
});
```

### Faking `Math.random` for determinism

Consider replacing `Math.random` with a fake when the test subject depends on it.

```javascript
beforeEach(() => {
  // https://xkcd.com/221/
  jest.spyOn(Math, 'random').mockReturnValue(0.4);
});
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
702
703
704
705
706
707
708
709
710
711
## Factories

TBU

## Mocking Strategies with Jest

### Stubbing and Mocking

Jasmine provides stubbing and mocking capabilities. There are some subtle differences in how to use it within Karma and Jest.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
712
713
Stubs or spies are often used synonymously. In Jest it's quite easy thanks to the `.spyOn` method.
[Official docs](https://jestjs.io/docs/en/jest-object#jestspyonobject-methodname)
GitLab Bot's avatar
GitLab Bot включено в состав коммита
714
715
716
717
The more challenging part are mocks, which can be used for functions or even dependencies.

### Manual module mocks

GitLab Bot's avatar
GitLab Bot включено в состав коммита
718
Manual mocks are used to mock modules across the entire Jest environment. This is a very powerful testing tool that helps simplify
GitLab Bot's avatar
GitLab Bot включено в состав коммита
719
unit testing by mocking out modules which cannot be easily consumed in our test environment.
Luke Bennett's avatar
Luke Bennett включено в состав коммита
720

GitLab Bot's avatar
GitLab Bot включено в состав коммита
721
722
723
> **WARNING:** Do not use manual mocks if a mock should not be consistently applied in every spec (i.e. it's only needed by a few specs).
> Instead, consider using [`jest.mock(..)`](https://jestjs.io/docs/en/jest-object#jestmockmodulename-factory-options)
> (or a similar mocking function) in the relevant spec file.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
724
725
726
727
728
729
730
731
732
733

#### Where should I put manual mocks?

Jest supports [manual module mocks](https://jestjs.io/docs/en/manual-mocks) by placing a mock in a `__mocks__/` directory next to the source module
(e.g. `app/assets/javascripts/ide/__mocks__`). **Don't do this.** We want to keep all of our test-related code in one place (the `spec/` folder).

If a manual mock is needed for a `node_modules` package, please use the `spec/frontend/__mocks__` folder. Here's an example of
a [Jest mock for the package `monaco-editor`](https://gitlab.com/gitlab-org/gitlab/blob/b7f914cddec9fc5971238cdf12766e79fa1629d7/spec/frontend/__mocks__/monaco-editor/index.js#L1).

If a manual mock is needed for a CE module, please place it in `spec/frontend/mocks/ce`.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
734
735
736
737

- Files in `spec/frontend/mocks/ce` will mock the corresponding CE module from `app/assets/javascripts`, mirroring the source module's path.
  - Example: `spec/frontend/mocks/ce/lib/utils/axios_utils` will mock the module `~/lib/utils/axios_utils`.
- We don't support mocking EE modules yet.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
738
- If a mock is found for which a source module doesn't exist, the test suite will fail. 'Virtual' mocks, or mocks that don't have a 1-to-1 association with a source module, are not supported yet.
Luke Bennett's avatar
Luke Bennett включено в состав коммита
739

GitLab Bot's avatar
GitLab Bot включено в состав коммита
740
#### Manual mock examples
741

GitLab Bot's avatar
GitLab Bot включено в состав коммита
742
743
744
- [`mocks/axios_utils`](https://gitlab.com/gitlab-org/gitlab/blob/bd20aeb64c4eed117831556c54b40ff4aee9bfd1/spec/frontend/mocks/ce/lib/utils/axios_utils.js#L1) -
  This mock is helpful because we don't want any unmocked requests to pass any tests. Also, we are able to inject some test helpers such as `axios.waitForAll`.
- [`__mocks__/mousetrap/index.js`](https://gitlab.com/gitlab-org/gitlab/blob/cd4c086d894226445be9d18294a060ba46572435/spec/frontend/__mocks__/mousetrap/index.js#L1) -
GitLab Bot's avatar
GitLab Bot включено в состав коммита
745
  This mock is helpful because the module itself uses AMD format which webpack understands, but is incompatible with the jest environment. This mock doesn't remove
GitLab Bot's avatar
GitLab Bot включено в состав коммита
746
747
  any behavior, only provides a nice es6 compatible wrapper.
- [`__mocks__/monaco-editor/index.js`](https://gitlab.com/gitlab-org/gitlab/blob/b7f914cddec9fc5971238cdf12766e79fa1629d7/spec/frontend/__mocks__/monaco-editor/index.js) -
GitLab Bot's avatar
GitLab Bot включено в состав коммита
748
  This mock is helpful because the Monaco package is completely incompatible in a Jest environment. In fact, webpack requires a special loader to make it work. This mock
GitLab Bot's avatar
GitLab Bot включено в состав коммита
749
  simply makes this package consumable by Jest.
750

GitLab Bot's avatar
GitLab Bot включено в состав коммита
751
### Keep mocks light
GitLab Bot's avatar
GitLab Bot включено в состав коммита
752

GitLab Bot's avatar
GitLab Bot включено в состав коммита
753
Global mocks introduce magic and technically can reduce test coverage. When mocking is deemed profitable:
GitLab Bot's avatar
GitLab Bot включено в состав коммита
754

GitLab Bot's avatar
GitLab Bot включено в состав коммита
755
756
- Keep the mock short and focused.
- Please leave a top-level comment in the mock on why it is necessary.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
757

GitLab Bot's avatar
GitLab Bot включено в состав коммита
758
### Additional mocking techniques
GitLab Bot's avatar
GitLab Bot включено в состав коммита
759

GitLab Bot's avatar
GitLab Bot включено в состав коммита
760
Please consult the [official Jest docs](https://jestjs.io/docs/en/jest-object#mock-modules) for a full overview of the available mocking features.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
761
762

## Running Frontend Tests
763

Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
764
For running the frontend tests, you need the following commands:
765

Mike Greiling's avatar
Mike Greiling включено в состав коммита
766
- `rake frontend:fixtures` (re-)generates [fixtures](#frontend-test-fixtures).
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
767
- `yarn test` executes the tests.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
768
- `yarn jest` executes only the Jest tests.
769

Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
770
As long as the fixtures don't change, `yarn test` is sufficient (and saves you some time).
771

GitLab Bot's avatar
GitLab Bot включено в состав коммита
772
773
### Live testing and focused testing -- Jest

GitLab Bot's avatar
GitLab Bot включено в состав коммита
774
While you work on a test suite, you may want to run these specs in watch mode, so they rerun automatically on every save.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
775

GitLab Bot's avatar
GitLab Bot включено в состав коммита
776
```shell
GitLab Bot's avatar
GitLab Bot включено в состав коммита
777
778
779
780
781
782
783
784
785
# Watch and rerun all specs matching the name icon
yarn jest --watch icon

# Watch and rerun one specifc file
yarn jest --watch path/to/spec/file.spec.js
```

You can also run some focused tests without the `--watch` flag

GitLab Bot's avatar
GitLab Bot включено в состав коммита
786
```shell
GitLab Bot's avatar
GitLab Bot включено в состав коммита
787
788
789
790
791
792
793
# Run specific jest file
yarn jest ./path/to/local_spec.js
# Run specific jest folder
yarn jest ./path/to/folder/
# Run all jest files which path contain term
yarn jest term
```
794

GitLab Bot's avatar
GitLab Bot включено в состав коммита
795
796
797
798
799
### Live testing and focused testing -- Karma

Karma allows something similar, but it's way more costly.

Running Karma with `yarn run karma-start` will compile the JavaScript
800
assets and run a server at `http://localhost:9876/` where it will automatically
GitLab Bot's avatar
GitLab Bot включено в состав коммита
801
run the tests on any browser which connects to it. You can enter that URL on
802
803
multiple browsers at once to have it run the tests on each in parallel.

Luke Bennett's avatar
Luke Bennett включено в состав коммита
804
While Karma is running, any changes you make will instantly trigger a recompile
GitLab Bot's avatar
GitLab Bot включено в состав коммита
805
and retest of the **entire test suite**, so you can see instantly if you've broken
GitLab Bot's avatar
GitLab Bot включено в состав коммита
806
a test with your changes. You can use [Jasmine focused](https://jasmine.github.io/2.5/focused_specs.html) or
Luke Bennett's avatar
Luke Bennett включено в состав коммита
807
excluded tests (with `fdescribe` or `xdescribe`) to get Karma to run only the
808
809
810
tests you want while you're working on a specific feature, but make sure to
remove these directives when you commit your code.

Luke Bennett's avatar
Luke Bennett включено в состав коммита
811
It is also possible to only run Karma on specific folders or files by filtering
Lukas Eipert's avatar
Lukas Eipert включено в состав коммита
812
the run tests via the argument `--filter-spec` or short `-f`:
Lukas Eipert's avatar
Lukas Eipert включено в состав коммита
813

GitLab Bot's avatar
GitLab Bot включено в состав коммита
814
```shell
Lukas Eipert's avatar
Lukas Eipert включено в состав коммита
815
816
817
# Run all files
yarn karma-start
# Run specific spec files
Lukas Eipert's avatar
Lukas Eipert включено в состав коммита
818
yarn karma-start --filter-spec profile/account/components/update_username_spec.js
Lukas Eipert's avatar
Lukas Eipert включено в состав коммита
819
# Run specific spec folder
Lukas Eipert's avatar
Lukas Eipert включено в состав коммита
820
821
822
yarn karma-start --filter-spec profile/account/components/
# Run all specs which path contain vue_shared or vie
yarn karma-start -f vue_shared -f vue_mr_widget
Lukas Eipert's avatar
Lukas Eipert включено в состав коммита
823
824
```

Mike Greiling's avatar
Mike Greiling включено в состав коммита
825
826
827
You can also use glob syntax to match files. Remember to put quotes around the
glob otherwise your shell may split it into multiple arguments:

GitLab Bot's avatar
GitLab Bot включено в состав коммита
828
```shell
Mike Greiling's avatar
Mike Greiling включено в состав коммита
829
830
831
832
# Run all specs named `file_spec` within the IDE subdirectory
yarn karma -f 'spec/javascripts/ide/**/file_spec.js'
```

Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
833
834
## Frontend test fixtures

GitLab Bot's avatar
GitLab Bot включено в состав коммита
835
836
837
838
839
840
841
Frontend fixtures are files containing responses from backend controllers. These responses can be either HTML
generated from haml templates or JSON payloads. Frontend tests that rely on these responses are
often using fixtures to validate correct integration with the backend code.

### Generate fixtures

You can find code to generate test fixtures in:
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
842

Mike Greiling's avatar
Mike Greiling включено в состав коммита
843
844
- `spec/frontend/fixtures/`, for running tests in CE.
- `ee/spec/frontend/fixtures/`, for running tests in EE.
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
845

GitLab Bot's avatar
GitLab Bot включено в состав коммита
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
You can generate fixtures by running:

- `bin/rake frontend:fixtures` to generate all fixtures
- `bin/rspec spec/frontend/fixtures/merge_requests.rb` to generate specific fixtures (in this case for `merge_request.rb`)

You can find generated fixtures are in `tmp/tests/frontend/fixtures-ee`.

#### Creating new fixtures

For each fixture, you can find the content of the `response` variable in the output file.
For example, test named `"merge_requests/diff_discussion.json"` in `spec/frontend/fixtures/merge_requests.rb`
will produce output file `tmp/tests/frontend/fixtures-ee/merge_requests/diff_discussion.json`.
The `response` variable gets automatically set if the test is marked as `type: :request` or `type: :controller`.

When creating a new fixture, it often makes sense to take a look at the corresponding tests for the
endpoint in `(ee/)spec/controllers/` or `(ee/)spec/requests/`.

### Use fixtures

Jest and Karma test suites import fixtures in different ways:
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
866
867
868
869
870
871
872
873

- The Karma test suite are served by [jasmine-jquery](https://github.com/velesin/jasmine-jquery).
- Jest use `spec/frontend/helpers/fixtures.js`.

The following are examples of tests that work for both Karma and Jest:

```javascript
it('makes a request', () => {
Mike Greiling's avatar
Mike Greiling включено в состав коммита
874
  const responseBody = getJSONFixture('some/fixture.json'); // loads spec/frontend/fixtures/some/fixture.json
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
875
  axiosMock.onGet(endpoint).reply(200, responseBody);
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
876

Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
877
  myButton.click();
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
878

Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
879
880
881
882
  // ...
});

it('uses some HTML element', () => {
Mike Greiling's avatar
Mike Greiling включено в состав коммита
883
  loadFixtures('some/page.html'); // loads spec/frontend/fixtures/some/page.html and adds it to the DOM
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
884

Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
885
  const element = document.getElementById('#my-id');
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
886

Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
887
888
889
890
  // ...
});
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
## Data-driven tests

Similar to [RSpec's parameterized tests](best_practices.md#table-based--parameterized-tests),
Jest supports data-driven tests for:

- Individual tests using [`test.each`](https://jestjs.io/docs/en/api#testeachtable-name-fn-timeout) (aliased to `it.each`).
- Groups of tests using [`describe.each`](https://jestjs.io/docs/en/api#describeeachtable-name-fn-timeout).

These can be useful for reducing repetition within tests. Each option can take an array of
data values or a tagged template literal.

For example:

```javascript
// function to test
const icon = status => status ? 'pipeline-passed' : 'pipeline-failed'
const message = status => status ? 'pipeline-passed' : 'pipeline-failed'

// test with array block
it.each([
    [false, 'pipeline-failed'],
    [true, 'pipeline-passed']
])('icon with %s will return %s',
 (status, icon) => {
    expect(renderPipeline(status)).toEqual(icon)
 }
);
```

```javascript
// test suite with tagged template literal block
describe.each`
    status   | icon                 | message
    ${false} | ${'pipeline-failed'} | ${'Pipeline failed - boo-urns'}
    ${true}  | ${'pipeline-passed'} | ${'Pipeline succeeded - win!'}
`('pipeline component', ({ status, icon, message }) => {
    it(`returns icon ${icon} with status ${status}`, () => {
        expect(icon(status)).toEqual(message)
    })

    it(`returns message ${message} with status ${status}`, () => {
        expect(message(status)).toEqual(message)
    })
});
```

937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
## Gotchas

### RSpec errors due to JavaScript

By default RSpec unit tests will not run JavaScript in the headless browser
and will simply rely on inspecting the HTML generated by rails.

If an integration test depends on JavaScript to run correctly, you need to make
sure the spec is configured to enable JavaScript when the tests are run. If you
don't do this you'll see vague error messages from the spec runner.

To enable a JavaScript driver in an `rspec` test, add `:js` to the
individual spec or the context block containing multiple specs that need
JavaScript enabled:

```ruby
# For one spec
it 'presents information about abuse report', :js do
  # assertions...
end

describe "Admin::AbuseReports", :js do
  it 'presents information about abuse report' do
    # assertions...
  end
  it 'shows buttons for adding to abuse report' do
    # assertions...
  end
end
```

Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
968
969
## Overview of Frontend Testing Levels

GitLab Bot's avatar
GitLab Bot включено в состав коммита
970
971
Main information on frontend testing levels can be found in the [Testing Levels page](testing_levels.md).

Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
972
973
Tests relevant for frontend development can be found at the following places:

GitLab Bot's avatar
GitLab Bot включено в состав коммита
974
975
976
977
978
- `spec/javascripts/`, for Karma tests
- `spec/frontend/`, for Jest tests
- `spec/features/`, for RSpec tests

RSpec runs complete [feature tests](testing_levels.md#frontend-feature-tests), while the Jest and Karma directories contain [frontend unit tests](testing_levels.md#frontend-unit-tests), [frontend component tests](testing_levels.md#frontend-component-tests), and [frontend integration tests](testing_levels.md#frontend-integration-tests).
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
979

GitLab Bot's avatar
GitLab Bot включено в состав коммита
980
All tests in `spec/javascripts/` will eventually be migrated to `spec/frontend/` (see also [#52483](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/52483)).
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
981

GitLab Bot's avatar
GitLab Bot включено в состав коммита
982
Before May 2018, `features/` also contained feature tests run by Spinach. These tests were removed from the codebase in May 2018 ([#23036](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/23036)).
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
983

GitLab Bot's avatar
GitLab Bot включено в состав коммита
984
See also [Notes on testing Vue components](../fe_guide/vue.md#testing-vue-components).
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
985
986
987
988
989
990
991

## Test helpers

### Vuex Helper: `testAction`

We have a helper available to make testing actions easier, as per [official documentation](https://vuex.vuejs.org/guide/testing.html):

Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
992
```javascript
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
993
994
995
996
997
998
999
1000
testAction(
  actions.actionName, // action
  { }, // params to be passed to action
  state, // state
  [
    { type: types.MUTATION},
    { type: types.MUTATION_1, payload: {}},
  ], // mutations committed
Для ускорения просмотра не вся история отображается Просмотреть всю вину