Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

javascript - Why does a unit test with sinon createStubInstance is successful but hangs?

I'm unit testing a method in fileA.js that requires a new class instance from fileB.js with success.

The problem is that I get success, as expected but this specific test hangs and I don't get results from istanbul. Yes, I have already added --exit flag. This only happens if I run this test... Why does it hang?

This is the method in fileA.js:

global.mapperObj = {};
const opts = config.opts
(...)

async function startServices() {
    const data = await getData();

    Object.keys(data).forEach(function(key) {
        mapperObj[key] = new ClassInFileB(key, data[key], opts);
        mapperArray.push(key);
    });
}

The class in fileB.js:

const npmPool = require('npmPackage');

class ClassInFileB{
    
    constructor(ip, port, opts) {
        this.classPool = npmPool.createPool(ip, port, opts);
    }
    
    (...)
    // other class methods

}

And the test for that method:

const rewire = require('rewire');
const fileA = rewire(`path/to/fileA`);
const ClassInFileB = require(`path/to/fileB`);

describe('Testing startServices() function', function () {
    it('It should not throw errors', async function () {
        let result;
        let error = false;
        global.mapperArray = [];
        
        try {
            function getDataMock() {
                return { '0.0.0.1': 'thisIsSomething'};
            }
            fileA.__set__('getData', getDataMock);

            // Create a stub instance to ClassInFileB class avoiding the constructor
            sinon.createStubInstance(ClassInFileB);

            result = await fileA.startServices();
        } catch (err) {
            error = err;
        }
        expect(error).to.be.false;
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...