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

Issue with partially mocking module that is also used within same file by another module #118

Closed
vueme opened this issue Aug 16, 2022 · 4 comments

Comments

@vueme
Copy link

vueme commented Aug 16, 2022

Hello.

I think I've found an issue. I'm having trouble with partially mocking deeply nested module that is also used in same file. Example below. I'm on Mocha and Node 17.4.0.

const supertestModule = await esmock.px('../modules/supertest.js', null, {
  '../../../../server/troublesomeFile.js': {
    funcOne: async () => {
      console.log('Ran')
    }
  }
})

troublesomeFile.js

export async function funcOne() {
  console.log('Should not be called')
}

export async function funcTwoSameFile() {
   const test = await funcOne()
  
  // some more code
  
  return test
}

supertest.js (might not be relevant)

// imports app.js...
export default async () => {
     // More code...

     return supertest(app)
      .post(`/v1/${endpoint}`)
      .set({ ...thingsToSet })
      .send({ ...requestBody })
  }
}

When funcTwoSameFile is called somewhere deeply nested in app.js, original version of funcOne is called and not the mocked one. Is this some kind of limitation or am i missing something? My other (full-module, not .px mocks) are working fine in other tests.

Thanks for your help!

@vueme vueme changed the title Issue with partial mocking module used within mocked file Issue with partially mocking module that is also used within same file by another function Aug 16, 2022
@vueme vueme changed the title Issue with partially mocking module that is also used within same file by another function Issue with partially mocking module that is also used within same file by another module Aug 16, 2022
@iambumblehead
Copy link
Owner

iambumblehead commented Aug 16, 2022

https://github.com/iambumblehead/esmock/blob/master/src/esmockModule.js#L53-L55

@vueme esmock currently does something like this paraphrased example,

const originalFile = import( pathtooriginalfile );
const mockedFile = Object.assign({}, originalFile, mockDefinitions);

return mockedFile

and so the partial mock won't work for your use-case (sorry)

is it OK with you if this ticket is closed?

@vueme
Copy link
Author

vueme commented Aug 16, 2022

Thanks for quick answer!

You do not happen to have any workaround recommendation?

Thanks anyways!

@iambumblehead
Copy link
Owner

@vueme these may not be great ideas but here they are :) One idea is to define both mock functions at the same time eg,

const mock = {};
mock.funcTwo = () => 'mocked func two';
mock.funcOne = () => mock.funcTwo();

const supertestModule = await esmock.px('../modules/supertest.js', null, {
  '../../../../server/troublesomeFile.js': mock
})

another idea is to use 'this' or some other namespace creatively like this
originalfile.js

export const obj = {};

obj.funcOne = async function () {
  return 'this.funcOne'
};
obj.funcTwo = async function () {
  return await this.funcOne();
};

originalfile-test.js

const supertestModule = await esmock.px('../modules/supertest.js', null, {
  '../../../../server/troublesomeFile.js': {
     obj: {
       ...troublesomefile.obj,
       funcOne: () => 'mocked value'
     }
   }
})

I didn't test these but hope their intention is expressed

@vueme
Copy link
Author

vueme commented Aug 17, 2022

Thanks for the ideas and this great js package! I'll look into this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants