Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to get the current sub/segment when writing unit tests #110

Open
manchuck opened this issue Apr 4, 2019 · 3 comments

Comments

Projects
None yet
3 participants
@manchuck
Copy link

commented Apr 4, 2019

I'm trying to write a unit tests for a function which is traced. I've been stuck trying to bootstrap the test.

src/mysql.js:

module.exports.executeMysqlQuery = async (pool, sql, params = []) => {
    return new Promise(async (resolve, reject) => {
        xray.captureAsyncFunc('mysql-query', async (subsegment) => {
            subsegment.addAnnotation('query', sql);
            try {
                const results = await pool.execute(sql, params);
                subsegment.close();
                resolve(results);
            } catch (error) {
                reject(error);
                subsegment.close(error);
            }
        });
    });
};

tests/mysqlTest.js

const {expect} = require('chai');
const mysql = require('mysql2');
const {executeMysqlQuery} = require('../src/mysql');
const xray = require('aws-xray-sdk');


describe('mysql', () => {
    const sandbox = require('sinon').createSandbox();
    const namespace = xray.getNamespace();
    const segment = new xray.Segment('root');

    beforeEach(async () => {
        return await namespace.run(() => {
            xray.setSegment(segment);
        });
    });

    afterEach(() => {
        sandbox.verify();
        sandbox.restore();
        segment.close();
    });

    context('executeMysqlQuery', () => {
        it('Should Execute Query', async () => {
            const name = 'manchuck';
            const fields = ['id', 'name'];
            const expected = [{id: 0, name: name}];
            const results = [expected, fields];
            const pool = mysql.createPool({host: 'localhost'});

            const poolMock = sandbox.mock(pool);
            poolMock.
                expects('execute').
                once().
                withArgs('select * from users where name = ?', [name]).
                resolves(results);

            const actualResults = await executeMysqlQuery(
                pool,
                'select * from users where name = ?',
                [name],
            );

            expect(actualResults).deep.eq(expected);
        });
    });
});

When executing the test, I'm getting Error: Failed to get the current sub/segment from the context..

@bluehawk27

This comment has been minimized.

Copy link

commented Apr 4, 2019

I'm running into this as well when running jest in an automated unit test run with codebuild.

@chrisradek

This comment has been minimized.

Copy link
Contributor

commented Apr 23, 2019

@manchuck
Are you using the experimental version of the SDK? That version supports async/await.

You can install it using: npm install --save aws-xray-sdk@experimental

@chrisradek chrisradek added the question label Apr 23, 2019

@manchuck

This comment has been minimized.

Copy link
Author

commented May 29, 2019

@chrisradek Yes I am using 2.1.0-experimental.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.