You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hyperledger Composer中npm test调用query函数失败问题

Fixing the 'query is not defined' no-undef Error in Hyperledger Composer Tests

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.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:07:07