bitpay / bitcore

A full stack for bitcoin and blockchain-based applications
https://bitcore.io/
MIT License
4.81k stars 2.08k forks source link

How to run any test code? #3607

Open zydjohnHotmail opened 1 year ago

zydjohnHotmail commented 1 year ago

Hi, I have downloaded bitcore packages and use yarn to add 'mocha' for bitcore-lib. Here is the package.json: { "name": "bitcore-lib", "version": "10.0.5", "description": "A pure and powerful JavaScript Bitcoin library.", "author": "BitPay dev@bitpay.com", "main": "index.js", "scripts": { "test": "gulp test", "test:ci": "npm run test", "coverage": "gulp coverage", "build": "gulp", "pub": "npm run build && npm publish" }, "keywords": [ "bitcoin", "transaction", "address", "p2p", "ecies", "cryptocurrency", "blockchain", "payment", "bip21", "bip32", "bip37", "bip69", "bip70", "multisig" ], "repository": { "type": "git", "url": "https://github.com/bitpay/bitcore/tree/master/packages/bitcore-lib" }, "browser": { "request": "browser-request" }, "dependencies": { "bech32": "=2.0.0", "bip-schnorr": "=0.6.4", "bn.js": "=4.11.8", "bs58": "^4.0.1", "buffer-compare": "=1.1.1", "elliptic": "^6.5.3", "inherits": "=2.0.1", "lodash": "^4.17.20" }, "devDependencies": { "bitcore-build": "^10.0.5", "brfs": "^2.0.1", "chai": "^4.2.0", "gulp": "^4.0.0", "mocha": "^10.2.0", "sinon": "^7.1.1" }, "license": "MIT", "gitHead": "012cc0216a9bc6b195035855bd17149bad41acd1" } When I tried to run the whole test, I got the error: D:\Qtum\bitcore-master\bitcore-master\packages\bitcore-lib>npm run test

bitcore-lib@10.0.5 test gulp test

[17:24:27] No gulpfile found

D:\Qtum\bitcore-master\bitcore-master\packages\bitcore-lib> I need a gulpfile to run test, but since this package is a very big one, I want to run small tests first, so I tried to run test using mocha for only one file: transaction/input/input.js: 'use strict';

var should = require('chai').should(); var expect = require('chai').expect; var _ = require('lodash');

// var bitcore = require('../../..'); var bitcore = require('../../../'); var errors = bitcore.errors; var PrivateKey = bitcore.PrivateKey; var Address = bitcore.Address; var Script = bitcore.Script; var Networks = bitcore.Networks; var Input = bitcore.Transaction.Input;

describe('Transaction.Input', function() {

var privateKey = new PrivateKey('KwF9LjRraetZuEjR8VqEq539z137LW5anYDUnVK11vM3mNMHTWb4'); var publicKey = privateKey.publicKey; var address = new Address(publicKey, Networks.livenet); var output = { address: '33zbk2aSZYdNbRsMPPt6jgy6Kq1kQreqeb', prevTxId: '66e64ef8a3b384164b78453fa8c8194de9a473ba14f89485a0e433699daec140', outputIndex: 0, script: new Script(address), satoshis: 1000000 }; var coinbase = { prevTxId: '0000000000000000000000000000000000000000000000000000000000000000', outputIndex: 0xFFFFFFFF, script: new Script(), satoshis: 1000000 };

var coinbaseJSON = JSON.stringify({ prevTxId: '0000000000000000000000000000000000000000000000000000000000000000', outputIndex: 4294967295, script:'' });

var otherJSON = JSON.stringify({ txidbuf: 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458', txoutnum: 0, seqnum:4294967295, script: '71 0x3044022006553276ec5b885ddf5cc1d79e1e3dadbb404b60ad4cc00318e21565' + '4f13242102200757c17b36e3d0492fb9cf597032e5afbea67a59274e64af5a05d12e5ea2303901 ' + '33 0x0223078d2942df62c45621d209fab84ea9a7a23346201b7727b9b45a29c4e76f5e', output: { 'satoshis':100000, 'script':'OP_DUP OP_HASH160 20 0x88d9931ea73d60eaf7e5671efc0552b912911f2a ' + 'OP_EQUALVERIFY OP_CHECKSIG' } });

it('has abstract methods: "getSignatures", "isFullySigned", "addSignature", "clearSignatures"', function() { var input = new Input(output); _.each(['getSignatures', 'isFullySigned', 'addSignature', 'clearSignatures'], function(method) { expect(function() { return input[method](); }).to.throw(errors.AbstractMethodInvoked); }); }); it('detects coinbase transactions', function() { new Input(output).isNull().should.equal(false); var ci = new Input(coinbase); ci.isNull().should.equal(true); });

describe('instantiation', function() { it('works without new', function() { var input = Input(); should.exist(input); }); it('fails with no script info', function() { expect(function() { var input = new Input({}); input.toString(); }).to.throw('Need a script to create an input'); }); it('fromObject should work', function() { var jsonData = JSON.parse(coinbaseJSON); var input = Input.fromObject(jsonData); should.exist(input); input.prevTxId.toString('hex').should.equal(jsonData.prevTxId); input.outputIndex.should.equal(jsonData.outputIndex); }); it('fromObject should work', function() { var input = Input.fromObject(JSON.parse(coinbaseJSON)); var obj = input.toObject(); Input.fromObject(obj).should.deep.equal(input); obj.script = 42; Input.fromObject.bind(null, obj).should.throw('Invalid argument type: script'); }); });

it('_estimateSize returns correct size', function() { var input = new Input(output); input._estimateSize().should.equal(66); });

describe('handling the BIP68 (sequenceNumber locktime)', function() { var blockHeight = 3434; it('handles a null locktime', function() { var input = new Input(output); expect(input.getLockTime()).to.equal(null); }); it('handles a simple seconds example', function() { var input = new Input() .lockForSeconds(1e5);

  var expected = (parseInt(1e5 / 512) * 512) ;
  input.getLockTime().should.deep.equal(expected);

  expected = (Math.floor(expected/512) ) | Input.SEQUENCE_LOCKTIME_TYPE_FLAG;
  input.sequenceNumber.should.equal(expected | Input.SEQUENCE_LOCKTIME_TYPE_FLAG);
});
it('accepts a block height', function() {
  var input = new Input()
    .lockUntilBlockHeight(blockHeight);

  input.sequenceNumber.should.equal(blockHeight);
  input.getLockTime().should.deep.equal(blockHeight);
});
it('fails if the block height is too high', function() {
  expect(function() {
    return new Input().lockUntilBlockHeight(5e8);
  }).to.throw(errors.Transaction.Input.BlockHeightOutOfRange);
});
it('fails if the block height is negative', function() {
  expect(function() {
    return new Input().lockUntilBlockHeight(-1);
  }).to.throw(errors.Transaction.Input.BlockHeightOutOfRange);
});

}); }); But when I run the following command for testing, I got error: D:\Qtum\bitcore-master\bitcore-master\packages\bitcore-lib\test\transaction\input>npx mocha input.js

Error: Cannot find module 'D:\Qtum\bitcore-master\bitcore-master\packages\bitcore-lib\index.js'. Please verify that the package.json has a valid "main" entry at tryPackage (node:internal/modules/cjs/loader:440:19) at Module._findPath (node:internal/modules/cjs/loader:689:18) at Module._resolveFilename (node:internal/modules/cjs/loader:1058:27) at Module._load (node:internal/modules/cjs/loader:925:27) at Module.require (node:internal/modules/cjs/loader:1139:19) at require (node:internal/modules/helpers:121:18) at Object. (D:\Qtum\bitcore-master\bitcore-master\packages\bitcore-lib\test\transaction\input\input.js:8:15) at Module._compile (node:internal/modules/cjs/loader:1257:14) at Module._extensions..js (node:internal/modules/cjs/loader:1311:10) at Module.load (node:internal/modules/cjs/loader:1115:32) at Module._load (node:internal/modules/cjs/loader:962:12) at ModuleWrap. (node:internal/modules/esm/translators:165:29) at ModuleJob.run (node:internal/modules/esm/module_job:192:25) at async DefaultModuleLoader.import (node:internal/modules/esm/loader:246:24) at async importModuleDynamicallyWrapper (node:internal/vm/module:428:15) at async formattedImport (D:\Qtum\bitcore-master\bitcore-master\packages\bitcore-lib\node_modules\mocha\lib\nodejs\esm-utils.js:9:14) at async exports.requireOrImport (D:\Qtum\bitcore-master\bitcore-master\packages\bitcore-lib\node_modules\mocha\lib\nodejs\esm-utils.js:42:28) at async exports.loadFilesAsync (D:\Qtum\bitcore-master\bitcore-master\packages\bitcore-lib\node_modules\mocha\lib\nodejs\esm-utils.js:100:20) at async singleRun (D:\Qtum\bitcore-master\bitcore-master\packages\bitcore-lib\node_modules\mocha\lib\cli\run-helpers.js:125:3) at async exports.handler (D:\Qtum\bitcore-master\bitcore-master\packages\bitcore-lib\node_modules\mocha\lib\cli\run.js:370:5) => Any suggestion how can I run a single test code like the above, not the whole package, since it will show numerious errors.