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
This commit is contained in:
Rafael Oleza 2018-10-10 12:44:51 -07:00 committed by Facebook Github Bot
parent 3184cab60f
commit e7b0590f18
4 changed files with 74 additions and 68 deletions

View File

@ -11,21 +11,22 @@
'use strict';
const getDependencyConfig = require('../getDependencyConfig');
const sinon = require('sinon');
describe('getDependencyConfig', () => {
it("should return an array of dependencies' rnpm config", () => {
const config = {
getDependencyConfig: sinon.stub(),
getDependencyConfig: jest.fn(),
};
expect(Array.isArray(getDependencyConfig(config, ['abcd']))).toBeTruthy();
expect(config.getDependencyConfig.callCount).toEqual(1);
expect(config.getDependencyConfig.mock.calls.length).toEqual(1);
});
it('should filter out invalid react-native projects', () => {
const config = {
getDependencyConfig: sinon.stub().throws(new Error('Cannot require')),
getDependencyConfig: jest.fn().mockImplementation(() => {
throw new Error('Cannot require');
}),
};
expect(getDependencyConfig(config, ['abcd'])).toEqual([]);

View File

@ -10,9 +10,7 @@
'use strict';
const sinon = require('sinon');
const log = require('npmlog');
const path = require('path');
jest.setMock('chalk', {grey: str => str});
describe('link', () => {
@ -37,14 +35,16 @@ describe('link', () => {
const config = {
getPlatformConfig: () => ({ios: {}, android: {}}),
getProjectConfig: () => ({assets: []}),
getDependencyConfig: sinon.stub().returns({assets: [], commands: {}}),
getDependencyConfig: jest
.fn()
.mockReturnValue({assets: [], commands: {}}),
};
const link = require('../link').func;
link(['react-native-gradient'], config).then(() => {
expect(
config.getDependencyConfig.calledWith('react-native-gradient'),
).toBeTruthy();
expect(config.getDependencyConfig.mock.calls[0]).toEqual([
'react-native-gradient',
]);
done();
});
});
@ -53,18 +53,20 @@ describe('link', () => {
const config = {
getPlatformConfig: () => ({ios: {}, android: {}}),
getProjectConfig: () => ({assets: []}),
getDependencyConfig: sinon.stub().returns({assets: [], commands: {}}),
getDependencyConfig: jest
.fn()
.mockReturnValue({assets: [], commands: {}}),
};
const link = require('../link').func;
await link(['@scope/something@latest'], config);
expect(
config.getDependencyConfig.calledWith('@scope/something'),
).toBeTruthy();
expect(config.getDependencyConfig.mock.calls[0]).toEqual([
'@scope/something',
]);
});
it('should register native module when android/ios projects are present', done => {
const registerNativeModule = sinon.stub();
const registerNativeModule = jest.fn();
const dependencyConfig = {android: {}, ios: {}, assets: [], commands: {}};
const androidLinkConfig = require('../android');
const iosLinkConfig = require('../ios');
@ -74,37 +76,37 @@ describe('link', () => {
android: {linkConfig: androidLinkConfig},
}),
getProjectConfig: () => ({android: {}, ios: {}, assets: []}),
getDependencyConfig: sinon.stub().returns(dependencyConfig),
getDependencyConfig: jest.fn().mockReturnValue(dependencyConfig),
};
jest.setMock('../android/isInstalled.js', sinon.stub().returns(false));
jest.setMock('../android/isInstalled.js', jest.fn().mockReturnValue(false));
jest.setMock('../android/registerNativeModule.js', registerNativeModule);
jest.setMock('../ios/isInstalled.js', sinon.stub().returns(false));
jest.setMock('../ios/isInstalled.js', jest.fn().mockReturnValue(false));
jest.setMock('../ios/registerNativeModule.js', registerNativeModule);
const link = require('../link').func;
link(['react-native-blur'], config).then(() => {
expect(registerNativeModule.calledTwice).toBeTruthy();
expect(registerNativeModule.mock.calls.length).toBe(2);
done();
});
});
it('should not register modules when they are already installed', done => {
const registerNativeModule = sinon.stub();
const registerNativeModule = jest.fn();
const dependencyConfig = {ios: {}, android: {}, assets: [], commands: {}};
const config = {
getPlatformConfig: () => ({ios: {}, android: {}}),
getProjectConfig: () => ({ios: {}, android: {}, assets: []}),
getDependencyConfig: sinon.stub().returns(dependencyConfig),
getDependencyConfig: jest.fn().mockReturnValue(dependencyConfig),
};
jest.setMock('../ios/isInstalled.js', sinon.stub().returns(true));
jest.setMock('../ios/isInstalled.js', jest.fn().mockReturnValue(true));
jest.setMock('../android/isInstalled.js', sinon.stub().returns(true));
jest.setMock('../android/isInstalled.js', jest.fn().mockReturnValue(true));
jest.setMock('../ios/registerNativeModule.js', registerNativeModule);
@ -113,13 +115,13 @@ describe('link', () => {
const link = require('../link').func;
link(['react-native-blur'], config).then(() => {
expect(registerNativeModule.callCount).toEqual(0);
expect(registerNativeModule.mock.calls.length).toEqual(0);
done();
});
});
it('should register native modules for plugins', done => {
const registerNativeModule = sinon.stub();
const registerNativeModule = jest.fn();
const dependencyConfig = {
ios: {},
android: {},
@ -138,23 +140,23 @@ describe('link', () => {
test: {linkConfig: () => linkPluginConfig},
}),
getProjectConfig: () => ({ios: {}, android: {}, test: {}, assets: []}),
getDependencyConfig: sinon.stub().returns(dependencyConfig),
getDependencyConfig: jest.fn().mockReturnValue(dependencyConfig),
};
jest.setMock('../ios/isInstalled.js', sinon.stub().returns(true));
jest.setMock('../ios/isInstalled.js', jest.fn().mockReturnValue(true));
jest.setMock('../android/isInstalled.js', sinon.stub().returns(true));
jest.setMock('../android/isInstalled.js', jest.fn().mockReturnValue(true));
const link = require('../link').func;
link(['react-native-blur'], config).then(() => {
expect(registerNativeModule.calledOnce).toBeTruthy();
expect(registerNativeModule.mock.calls.length).toBe(1);
done();
});
});
it('should not register native modules for plugins when already installed', done => {
const registerNativeModule = sinon.stub();
const registerNativeModule = jest.fn();
const dependencyConfig = {
ios: {},
android: {},
@ -173,35 +175,35 @@ describe('link', () => {
test: {linkConfig: () => linkPluginConfig},
}),
getProjectConfig: () => ({ios: {}, android: {}, test: {}, assets: []}),
getDependencyConfig: sinon.stub().returns(dependencyConfig),
getDependencyConfig: jest.fn().mockReturnValue(dependencyConfig),
};
jest.setMock('../ios/isInstalled.js', sinon.stub().returns(true));
jest.setMock('../ios/isInstalled.js', jest.fn().mockReturnValue(true));
jest.setMock('../android/isInstalled.js', sinon.stub().returns(true));
jest.setMock('../android/isInstalled.js', jest.fn().mockReturnValue(true));
const link = require('../link').func;
link(['react-native-blur'], config).then(() => {
expect(registerNativeModule.callCount).toEqual(0);
expect(registerNativeModule.mock.calls.length).toEqual(0);
done();
});
});
it('should run prelink and postlink commands at the appropriate times', done => {
const registerNativeModule = sinon.stub();
const prelink = sinon.stub().yieldsAsync();
const postlink = sinon.stub().yieldsAsync();
it('should run prelink and postlink commands at the appropriate times', async () => {
const registerNativeModule = jest.fn();
const prelink = jest.fn().mockImplementation(cb => cb());
const postlink = jest.fn().mockImplementation(cb => cb());
jest.setMock('../ios/registerNativeModule.js', registerNativeModule);
jest.setMock('../ios/isInstalled.js', sinon.stub().returns(false));
jest.setMock('../ios/isInstalled.js', jest.fn().mockReturnValue(false));
const linkConfig = require('../ios');
const config = {
getPlatformConfig: () => ({ios: {linkConfig: linkConfig}}),
getProjectConfig: () => ({ios: {}, assets: []}),
getDependencyConfig: sinon.stub().returns({
getDependencyConfig: jest.fn().mockReturnValue({
ios: {},
assets: [],
commands: {prelink, postlink},
@ -209,19 +211,21 @@ describe('link', () => {
};
const link = require('../link').func;
await link(['react-native-blur'], config);
link(['react-native-blur'], config).then(() => {
expect(prelink.calledBefore(registerNativeModule)).toBeTruthy();
expect(postlink.calledAfter(registerNativeModule)).toBeTruthy();
done();
});
expect(prelink.mock.invocationCallOrder[0]).toBeLessThan(
registerNativeModule.mock.invocationCallOrder[0],
);
expect(postlink.mock.invocationCallOrder[0]).toBeGreaterThan(
registerNativeModule.mock.invocationCallOrder[0],
);
});
it('should copy assets from both project and dependencies projects', done => {
const dependencyAssets = ['Fonts/Font.ttf'];
const dependencyConfig = {assets: dependencyAssets, ios: {}, commands: {}};
const projectAssets = ['Fonts/FontC.ttf'];
const copyAssets = sinon.stub();
const copyAssets = jest.fn();
jest.setMock('../ios/copyAssets.js', copyAssets);
@ -229,14 +233,14 @@ describe('link', () => {
const config = {
getPlatformConfig: () => ({ios: {linkConfig: linkConfig}}),
getProjectConfig: () => ({ios: {}, assets: projectAssets}),
getDependencyConfig: sinon.stub().returns(dependencyConfig),
getDependencyConfig: jest.fn().mockReturnValue(dependencyConfig),
};
const link = require('../link').func;
link(['react-native-blur'], config).then(() => {
expect(copyAssets.calledOnce).toBeTruthy();
expect(copyAssets.getCall(0).args[0]).toEqual(
expect(copyAssets.mock.calls.length).toBe(1);
expect(copyAssets.mock.calls[0][0]).toEqual(
projectAssets.concat(dependencyAssets),
);
done();

View File

@ -10,35 +10,38 @@
'use strict';
const sinon = require('sinon');
const promiseWaterfall = require('../promiseWaterfall');
describe('promiseWaterfall', () => {
it('should run promises in a sequence', done => {
const tasks = [sinon.stub(), sinon.stub()];
it('should run promises in a sequence', async () => {
const tasks = [jest.fn(), jest.fn()];
promiseWaterfall(tasks).then(() => {
expect(tasks[0].calledBefore(tasks[1])).toBeTruthy();
done();
});
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', done => {
const tasks = [sinon.stub().returns(1), sinon.stub().returns(2)];
it('should resolve with last promise value', async () => {
const tasks = [jest.fn().mockReturnValue(1), jest.fn().mockReturnValue(2)];
promiseWaterfall(tasks).then(value => {
expect(value).toEqual(2);
done();
});
expect(await promiseWaterfall(tasks)).toEqual(2);
});
it('should stop the sequence when one of promises is rejected', done => {
const error = new Error();
const tasks = [sinon.stub().throws(error), sinon.stub().returns(2)];
const tasks = [
jest.fn().mockImplementation(() => {
throw error;
}),
jest.fn().mockReturnValue(2),
];
promiseWaterfall(tasks).catch(err => {
expect(err).toEqual(error);
expect(tasks[1].callCount).toEqual(0);
expect(tasks[1].mock.calls.length).toEqual(0);
done();
});
});

View File

@ -85,8 +85,7 @@
"source-map",
"fastpath",
"denodeify",
"fbjs",
"sinon"
"fbjs"
],
"testEnvironment": "node"
},
@ -226,8 +225,7 @@
"react": "16.6.0-alpha.400d197",
"react-native-dummy": "0.1.0",
"react-test-renderer": "16.6.0-alpha.400d197",
"shelljs": "^0.7.8",
"sinon": "^2.2.0"
"shelljs": "^0.7.8"
},
"detox": {
"test-runner": "jest",