@@ -187,6 +187,7 @@ export default function doSomething() {
...
@@ -187,6 +187,7 @@ export default function doSomething() {
visitUrl('/foo/bar');
visitUrl('/foo/bar');
}
}
```
```
```js
```js
// my_module_spec.js
// my_module_spec.js
importdoSomethingfrom'~/my_module';
importdoSomethingfrom'~/my_module';
...
@@ -213,7 +214,187 @@ Further documentation on the babel rewire pluign API can be found on
...
@@ -213,7 +214,187 @@ Further documentation on the babel rewire pluign API can be found on
#### Waiting in tests
#### Waiting in tests
If you cannot avoid using [`setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout) in tests, please use the [Jasmine mock clock](https://jasmine.github.io/api/2.9/Clock.html).
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)
because it makes the reason for waiting unclear and if passed a time larger than zero it will slow down our test suite.
Instead use one of the following approaches.
##### Promises and Ajax calls
Register handler functions to wait for the `Promise` to be resolved.
```javascript
constaskTheServer=()=>{
returnaxios
.get('/endpoint')
.then(response=>{
// do something
})
.catch(error=>{
// do something else
});
};
```
**in Jest:**
```javascript
it('waits for an Ajax call',()=>{
returnaskTheServer().then(()=>{
expect(something).toBe('done');
});
});
```
**in Karma:**
```javascript
it('waits for an Ajax call',done=>{
askTheServer()
.then(()=>{
expect(something).toBe('done');
})
.then(done)
.catch(done.fail);
});
```
If you are not able to register handlers to the `Promise`—for example because it is executed in a synchronous Vue life
cycle hook—you can flush all pending `Promise`s:
**in Jest:**
```javascript
it('waits for an Ajax call',()=>{
synchronousFunction();
jest.runAllTicks();
expect(something).toBe('done');
});
```
**in Karma:**
You are out of luck. The following only works sometimes and may lead to flaky failures:
```javascript
it('waits for an Ajax call',done=>{
synchronousFunction();
// create a new Promise and hope that it resolves after the rest
Promise.resolve()
.then(()=>{
expect(something).toBe('done');
})
.then(done)
.catch(done.fail);
});
```
##### Vue rendering
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'});
returnwrapper.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);
});
```
##### `setTimeout()` / `setInterval()` in application
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-ce/blob/a2128edfee799e49a8732bfa235e2c5e14949c68/jest.config.js#L47)
(see also [Jest Timer Mocks](https://jestjs.io/docs/en/timer-mocks)). In Karma you can use the