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

Ts-node and rewiremock #147

Closed
darcyrush opened this issue Jan 4, 2024 · 2 comments
Closed

Ts-node and rewiremock #147

darcyrush opened this issue Jan 4, 2024 · 2 comments

Comments

@darcyrush
Copy link

darcyrush commented Jan 4, 2024

Hello @theKashey, thank you for creating a library which tries to solve the issues of the existing module mocking libraries.

I have read the documentation, both of your articles, as well as a bunch of different issues, but I am really struggling to understand how this library works.

I have read the documentation multiple times, but it is very vague in places and in fact many code examples do not work (often missing closing brackets or braces). I understand there are four different ways to do things given that this library is based off of the four existing legacy libraries, but this makes examples very confusing to understand.

I'm not even sure if the require path within rewiremock() is meant to be the SUT module I am testing or a dependency module of the SUT module. Some examples show the require path as a dependency library, so it implies this should be a dependency, but other examples seem to point to the SUT path itself? I cannot tell if this is a typo in the documentation or if this is one of the four ways to mock.

I have a minimum reproducible repository here, I would be grateful if you can explain what I am doing wrong. I would also prefer to keep the arrange, act, assert test format.

describe('How', async () => {
  it('to make this work', async () => {
    // Arrange
    rewiremock(() => require('./main')).with({
      './helper': { helper: () => 'mock' }
    } as any)

    // Act
    const result = main()

    // Assert
    equal(result, 'mock')
  })
})
@theKashey
Copy link
Owner

As you've mentioned above - there are a few ways of using rewiremock, and you've mixed a few
There is a correction for your tests to work

 import { equal } from "node:assert/strict"
 import { rewiremock } from "./rewiremock"
 import { describe, it } from "node:test"
-import { main } from "./main"
 
 describe('How', async () => {
   it('to make this work', async () => {
-    rewiremock.proxy(() => require('./main'), {
+    const {main} = rewiremock.proxy(() => require('./main'), {
       './helper': { helper: () => 'mock' }
     })

if you want to still have import { main } then you need to use babel plugin AND have rewiremock.proxy or any other rewiremock command at the top level. Basically like jest.mock

If you want to import amended version of main in a test - you need to use a variable rewiremock will give you. Only it will contain amended version.

In short - overriding modules affects structure or your code, the very way files and variables are connected, a different branch, totally new world. You need to get a pointer to that new world - and that is variable rewiremock will give you. You have to store and use it somehow.

@darcyrush
Copy link
Author

Thank you, I am able to get the library working now

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