Skip to content

Cannot convert object to primitive value #36

Description

@mike-hanson

I've been using the library for about a week as I started to re-factor a VS Code Extension I created. For the most part Substitute is working as expected, but I have started to get an error "Cannot convert object to primitive value" when trying to verify a method call that returns a promise. The promise may not be the issue, but I am getting the error in two places in one test suite while other similar calls in the same suite do not cause the error and the difference is what the target methods return.

I have pushed the current state of my code base to https://github.com/testpossessed/auto-barrel, in the hope someone can help me resolve the issue.

I am using Mocha with expect and the code base includes a Run Config that allows me to debug via the tests. Using this I have traced the point where the issue occurs to the util.inspect call in the following function in Utilities.ts at line 7. However I am struggling to see how to fix it or whether I am doing something wrong.

export function stringifyArguments(args: any[]) {
    args = args.map(x => util.inspect(x));
    return args && args.length > 0 ? 'arguments [' + args.join(', ') + ']' : 'no arguments';
};

The test file that this is happening in is https://github.com/testpossessed/auto-barrel/blob/master/src/test/unit/create-barrel-command.test.ts and the target of the test is https://github.com/testpossessed/auto-barrel/blob/master/src/create-barrel-command.ts

This is the setup part of the test

describe('CreateBarrelCommand', () => {
    const rootFolder = '\/c:\/barrel';
    const files: Array<string> = [
        '\/c:\/barrel\/test1.ts',
        '\/c:\/barrel\/test2.ts',
        '\/c:\/barrel\/test3.ts'
    ];
    const contentLines: Array<string> = [
        'export * from \'./test1\';',
        'export * from \'./test2\';',
        'export * from \'./test3\';'
    ];
    const barrelDetails = {barrelFilePath: `${rootFolder}/index.ts`, contentLines};

    let vsCodeApi: ObjectSubstitute<OmitProxyMethods<IVsCodeApi>, IVsCodeApi> & IVsCodeApi;
    let barrelBuilder: any;
    let target: CreateBarrelCommand;

    beforeEach(() => {
        vsCodeApi = Substitute.for<IVsCodeApi>();
        barrelBuilder = Substitute.for<IBarrelBuilder>();
        target = new CreateBarrelCommand(vsCodeApi, barrelBuilder);        
    });

There are a couple of helper functions that configure the substitutes that look like this

    function assumeVsCodeApiFindsFiles() {        
        vsCodeApi.findFiles(rootFolder).returns(Promise.resolve(files));
    }

    function assumeBarrelBuildReturnsResult(){
        barrelBuilder.build(Arg.all()).return(Promise.resolve(barrelDetails));
    }

I have focused on one test for now, which looks like this

     it.only('should fetch files via vs code api', async () => {
        assumeVsCodeApiFindsFiles();
        assumeBarrelBuildReturnsResult();

        await target.execute(rootFolder);

        // expect(
            vsCodeApi.received(1).findFiles(rootFolder); // this is where the error occurs
            // );
    });

My Mrs (she is also a very good developer) and I spent some time yesterday trying to resolve it but once we found where it happened we got stuck on what to do to solve it.

Any help in identifying either a fix or what I might be doing wrong would be greatly appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions