Hyperledger Composer中npm test调用query函数失败问题
Alright, let's break down why you're hitting this error when running npm test—even though your transaction function calls the named query without issues. The problem comes down to how ESLint and the Composer test environment handle the query object, and there are two straightforward fixes depending on your needs.
1. Quick Fix: Tell ESLint query is a Global Variable
If you want to keep using the query shortcut in your test code (just like you do in transaction functions), you can explicitly let ESLint know that query is a global variable. Add this comment at the very top of your test file (usually located in the test/ directory):
/* global query */ // Rest of your test code below... const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection; // Example test case describe('My Business Network', () => { it('should retrieve a participant', async () => { const result = await query('getParticipant', { firstName: 'Alice' }); // Add your assertions here }); });
This tells ESLint not to flag query as an undefined variable, since it's provided by the Composer test runtime context.
2. Proper Test Flow: Use Query Manager (Recommended)
For more robust, maintainable tests, it's better to use the official Composer client API instead of relying on the global query shortcut. This aligns with how you'd interact with the network in a real application and avoids any ESLint confusion entirely:
Step 1: Establish a connection to the business network
const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection; describe('My Business Network', () => { let businessNetworkConnection; let queryManager; beforeAll(async () => { businessNetworkConnection = new BusinessNetworkConnection(); // Connect using your admin identity and network name await businessNetworkConnection.connect('admin@your-business-network'); // Get the query manager from the active connection queryManager = businessNetworkConnection.getQueryManager(); }); afterAll(async () => { await businessNetworkConnection.disconnect(); });
Step 2: Execute the named query via the query manager
it('should retrieve a participant by first name', async () => { // Execute the named query with your target parameters const participantResults = await queryManager.executeNamedQuery('getParticipant', { firstName: 'Alice' }); // Add your assertions here (e.g., validate results exist) expect(participantResults.length).toBeGreaterThan(0); }); });
This approach avoids implicit global dependencies, making your tests clearer and more reliable long-term.
Quick Sanity Check: Verify Your Query Definition
Just to cover all bases, double-check that your queries.qry file has a complete, valid query definition. For example:
query getParticipant { description: "Get participants with given first name" statement: SELECT org.yournamespace.Participant WHERE (firstName == _$firstName) }
Ensure the namespace matches your participant definition, and the parameter (_$firstName) matches what you're passing in your test.
内容的提问来源于stack exchange,提问作者jdias

