react-native/local-cli/link/__tests__/promiseWaterfall.spec.js
Rafael Oleza e7b0590f18 Remove sinon dependency
Summary:
This diff removes the `sinon` dependency from `metro` and `react-native-github`. It was only used in a handful of tests and having to learn and remember another mocking API just for these cases was not worth it IMO.

While doing the migration, most of the things that `sinon` provides can be done with `jest` in a very similar (and user friendly) way.

I've found, though, two small things that are more user friendly with `sinon`. I'm documenting them here because it may be worth adding them to jest:

With `sinon`:

```
stub.throws(new Error('foo'));
```

With `jest`:

```
mock.mockImplementation(() => {
  throw new Error('foo');
});
```

Taking into account that `jest` has a `mockRejectedValue` method for mocks (to return a rejected promise) I don't see any reason why it does not have a `mockThrowError` method.

With `sinon`:

```
expect(mock1.calledBefore(mock2)).toBeTruthy();
```

With `jest`:

```
expect(mock1.mock.invocationCallOrder[0]).toBeLessThan(
  mock2.mock.invocationCallOrder[0],
);
```

There's a community matcher that adds this matcher in `jest-extended`: https://github.com/jest-community/jest-extended#tohavebeencalledbefore, but we're not using `jest-extended` in `xplat/js`.

Reviewed By: jeanlauliac

Differential Revision: D10238331

fbshipit-source-id: 5441125b69596ad85bf8f56d203cfd20759bc358
2018-10-10 13:02:12 -07:00

49 lines
1.2 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @emails oncall+javascript_foundation
*/
'use strict';
const promiseWaterfall = require('../promiseWaterfall');
describe('promiseWaterfall', () => {
it('should run promises in a sequence', async () => {
const tasks = [jest.fn(), jest.fn()];
await promiseWaterfall(tasks);
// Check that tasks[0] is executed before tasks[1].
expect(tasks[0].mock.invocationCallOrder[0]).toBeLessThan(
tasks[1].mock.invocationCallOrder[0],
);
});
it('should resolve with last promise value', async () => {
const tasks = [jest.fn().mockReturnValue(1), jest.fn().mockReturnValue(2)];
expect(await promiseWaterfall(tasks)).toEqual(2);
});
it('should stop the sequence when one of promises is rejected', done => {
const error = new Error();
const tasks = [
jest.fn().mockImplementation(() => {
throw error;
}),
jest.fn().mockReturnValue(2),
];
promiseWaterfall(tasks).catch(err => {
expect(err).toEqual(error);
expect(tasks[1].mock.calls.length).toEqual(0);
done();
});
});
});