Skip to content

Commit

Permalink
Work in progress #124 :
Browse files Browse the repository at this point in the history
- Migrated more tests to proxyquire:
  - AppManager
  - apis/accountsAPI
  - apis/blocksAPI

- fix ByteBuffer stub in tests/unit/logic/transactions/createmultisig
- Remove remaining references to rewire on logic/account.spec.ts
- Remove remaining references to rewire on modules/forge.spec.ts
- Remove remaining references to rewire on modules/loader.spec.ts
- Remove remaining references to rewire on modules/transactions.spec.ts
- Removed rewire from devDependecies
- Re-enabled tests in .travis.yml
  • Loading branch information
mcanever committed Mar 22, 2018
1 parent 55955dd commit 38b1d54
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 174 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -12,7 +12,7 @@ before_script:
- psql -c "CREATE USER test WITH PASSWORD 'test';" -U postgres
script:
- npm run test-integration
# - npm run test-cover-unit
- npm run test-cover-unit
#cache:
# directories:
# - node_modules
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -89,7 +89,6 @@
"nyc": "^11.5.0",
"proxyquire": "^2.0.0",
"reflect-metadata": "^0.1.10",
"rewire": "^3.0.2",
"shx": "^0.2.2",
"sinon": "=4.4.2",
"supertest": "=3.0.0",
Expand Down
215 changes: 68 additions & 147 deletions tests/unit/AppManager.spec.ts

Large diffs are not rendered by default.

17 changes: 7 additions & 10 deletions tests/unit/apis/accountsAPI.spec.ts
Expand Up @@ -2,7 +2,7 @@ import * as chai from 'chai';
import { expect } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { Container } from 'inversify';
import * as rewire from 'rewire';
import * as proxyquire from 'proxyquire';
import { SinonSandbox, SinonStub } from 'sinon';
import * as sinon from 'sinon';
import { AccountsAPI } from '../../../src/apis/accountsAPI';
Expand All @@ -15,10 +15,12 @@ import { createContainer } from '../../utils/containerCreator';

chai.use(chaiAsPromised);

// tslint:disable no-unused-expression max-line-length

const AccountsAPIRewire = rewire('../../../src/apis/accountsAPI');
let isEmptyStub: SinonStub;
const ProxyAccountsAPI = proxyquire('../../../src/apis/accountsAPI', {
'is-empty': (...args) => isEmptyStub.apply(this, args),
});

// tslint:disable no-unused-expression max-line-length
describe('apis/accountsAPI', () => {

let sandbox: SinonSandbox;
Expand All @@ -32,7 +34,7 @@ describe('apis/accountsAPI', () => {
beforeEach(() => {
sandbox = sinon.sandbox.create();
container = createContainer();
container.bind(Symbols.api.accounts).to(AccountsAPIRewire.AccountsAPI);
container.bind(Symbols.api.accounts).to(ProxyAccountsAPI.AccountsAPI);

schema = container.get(Symbols.generic.zschema);
accountsModule = container.get(Symbols.modules.accounts);
Expand All @@ -46,9 +48,6 @@ describe('apis/accountsAPI', () => {
});

describe('getAccount', () => {

let isEmptyStub: SinonStub;

let accData;
let query;
let generatedAddress;
Expand Down Expand Up @@ -79,8 +78,6 @@ describe('apis/accountsAPI', () => {
.onCall(1).returns(false)
.onCall(2).returns(true)
.onCall(3).returns(true);

AccountsAPIRewire.__set__('isEmpty', isEmptyStub);
});

it('should call isEmpty', async () => {
Expand Down
9 changes: 3 additions & 6 deletions tests/unit/apis/blocksAPI.spec.ts
Expand Up @@ -2,12 +2,13 @@ import * as chai from 'chai';
import { expect } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { Container } from 'inversify';
import * as rewire from 'rewire';
import { SinonSandbox, SinonSpy, SinonStub } from 'sinon';
import * as sinon from 'sinon';
import { BlocksAPI } from '../../../src/apis/blocksAPI';
import { OrderBy } from '../../../src/helpers';
import * as helpers from '../../../src/helpers';
import { Symbols } from '../../../src/ioc/symbols';
import sql from '../../../src/sql/blocks';
import {
BlockRewardLogicStub, BlocksModuleStub, DbStub, SequenceStub, SystemModuleStub, ZSchemaStub,
} from '../../stubs';
Expand All @@ -16,8 +17,6 @@ import { createContainer } from '../../utils/containerCreator';

chai.use(chaiAsPromised);

const BlocksAPIRewire = rewire('../../../src/apis/blocksAPI');

// tslint:disable no-unused-expression max-line-length

describe('apis/blocksAPI', () => {
Expand All @@ -37,7 +36,7 @@ describe('apis/blocksAPI', () => {
beforeEach(() => {
sandbox = sinon.sandbox.create();
container = createContainer();
container.bind(Symbols.api.blocks).to(BlocksAPIRewire.BlocksAPI);
container.bind(Symbols.api.blocks).to(BlocksAPI);

schema = container.get(Symbols.generic.zschema);
db = container.get(Symbols.generic.db);
Expand Down Expand Up @@ -373,8 +372,6 @@ describe('apis/blocksAPI', () => {
blockRows = ['row1', 'row2'];

instance = instance as any;
const helpers = BlocksAPIRewire.__get__('helpers_1');
const sql = BlocksAPIRewire.__get__('blocks_2.default');
OrderBySpy = sandbox.spy(helpers, 'OrderBy');
countListSpy = sandbox.spy(sql, 'countList');
listSpy = sandbox.spy(sql, 'list');
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/logic/account.spec.ts
Expand Up @@ -7,7 +7,7 @@ import { SinonStub } from 'sinon';
import { DbStub, LoggerStub, ZSchemaStub } from '../../stubs';

const pgpStub = { QueryFile : () => { return; }} as any;
const RewireAccount = proxyquire('../../../src/logic/account', {
const ProxyAccount = proxyquire('../../../src/logic/account', {
'pg-promise': pgpStub,
});
const jsonSql = jsonSqlCreator();
Expand All @@ -33,7 +33,7 @@ describe('logic/account', () => {
beforeEach(() => {
loggerStub = new LoggerStub();
zSchemaStub = new ZSchemaStub();
account = new RewireAccount.AccountLogic();
account = new ProxyAccount.AccountLogic();
// Inject the dependencies
(account as any).db = dbStub;
(account as any).logger = loggerStub;
Expand Down
7 changes: 5 additions & 2 deletions tests/unit/logic/transactions/createmultisig.spec.ts
Expand Up @@ -111,25 +111,28 @@ describe('logic/transactions/createmultisig', () => {

before(() => {
toRestore.writeByte = ByteBuffer.prototype.writeByte;
toRestore.writeInt = ByteBuffer.prototype.writeInt;
toRestore.writeLong = (ByteBuffer.prototype as any).writeLong;
toRestore.toBuffer = ByteBuffer.prototype.toBuffer;
toRestore.flip = ByteBuffer.prototype.flip;
});
beforeEach(() => {
lastBB = false;
lastBB = false;
sequence = [];
(ByteBuffer.prototype as any).writeByte = function(b) {
sequence.push(b);
lastBB = this;
};
ByteBuffer.prototype.flip = sandbox.stub();
ByteBuffer.prototype.toBuffer = sandbox.stub().returns(expectedBuffer);
ByteBuffer.prototype.flip = sandbox.stub();
});

after(() => {
(ByteBuffer.prototype as any).writeByte = toRestore.writeByte;
(ByteBuffer.prototype as any).writeInt = toRestore.writeInt;
(ByteBuffer.prototype as any).writeLong = toRestore.writeLong;
ByteBuffer.prototype.flip = toRestore.flip;
ByteBuffer.prototype.toBuffer = toRestore.toBuffer;
});

it('should call Buffer.from', () => {
Expand Down
1 change: 0 additions & 1 deletion tests/unit/modules/forge.spec.ts
Expand Up @@ -242,7 +242,6 @@ describe('modules/forge', () => {
});

describe('onBlockchainReady', () => {
// We cannot use rewire here due to incompatibility with FakeTimers.
let inst: ForgeModule;
let forgeStub: SinonStub;
beforeEach(() => {
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/modules/loader.spec.ts
Expand Up @@ -47,7 +47,7 @@ let promiseRetryStub: any;
// tslint:disable no-unused-expression max-line-length
// tslint:disable no-unused-expression object-literal-sort-keys

const LoaderModuleRewire = proxyquire('../../../src/modules/loader', {
const ProxyLoaderModule = proxyquire('../../../src/modules/loader', {
'promise-retry': (...args) => {
return promiseRetryStub.apply(this, args);
},
Expand Down Expand Up @@ -123,7 +123,7 @@ describe('modules/loader', () => {
container.bind(Symbols.modules.transactions).to(TransactionsModuleStub).inSingletonScope();
container.bind(Symbols.modules.transport).to(TransportModuleStub).inSingletonScope();

container.bind(Symbols.modules.loader).to(LoaderModuleRewire.LoaderModule);
container.bind(Symbols.modules.loader).to(ProxyLoaderModule.LoaderModule);

instance = container.get(Symbols.modules.loader);
});
Expand Down Expand Up @@ -1766,7 +1766,6 @@ describe('modules/loader', () => {
});

describe('.syncTrigger', () => {
// We cannot use rewire here due to incompatibility with FakeTimers.
let loggerStub: LoggerStub;
let appStateStub: AppStateStub;
let socketIoStub: SocketIOStub;
Expand Down
1 change: 0 additions & 1 deletion tests/unit/modules/transactions.spec.ts
Expand Up @@ -2,7 +2,6 @@ import * as chai from 'chai';
import { expect } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { Container } from 'inversify';
import * as rewire from 'rewire';
import * as sinon from 'sinon';
import { SinonSandbox, SinonSpy } from 'sinon';
import * as helpers from '../../../src/helpers';
Expand Down

0 comments on commit 38b1d54

Please sign in to comment.