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

Nested dependencies? #80

Open
Wizek opened this issue Mar 26, 2021 · 1 comment
Open

Nested dependencies? #80

Wizek opened this issue Mar 26, 2021 · 1 comment

Comments

@Wizek
Copy link

Wizek commented Mar 26, 2021

To quote #23:

Hello!
In my scenario I have a file a.js requiring b.js which requires c.js
I want to mock c.js when I require a.js

Is it possible? So far I was only able to mock c.js if I mock b.js directly...

Thank you!

Has this become possible in the last 4.5 years?

If not, what is advised here? Can this be worked around? Or does a different package have this feature?

@Wizek
Copy link
Author

Wizek commented Mar 26, 2021

Imagine a scenario like this:

// stuff.js

export const ourFetch = fetch

export const postJson = async (url, payload) => {
  const res = await ourFetch(url, {
    method: 'POST',
    body: JSON.stringify(payload),
    headers: {
      'Content-Type': 'application/json',
    },
  })
  const obj = await res.json()
  return obj
}

export const putJson = async (url, payload) => {
  const res = await ourFetch(url, {
    method: 'PUT',
    body: JSON.stringify(payload),
    headers: {
      'Content-Type': 'application/json',
    },
  })
  const obj = await res.json()
  return obj
}

export const getJson = async (url) => {
  const res = await ourFetch(url)
  const obj = await res.json()
  return obj
}

export const deleteReq = async (url) => {
  const res = await ourFetch(url, {
    method: 'DELETE',
  })
  return res
}

export const apiBase = "/api"

export const apiReadStuffs  = () => getJson(`${apiBase}/stuff`)
export const apiReadStuff   = id => getJson(`${apiBase}/stuff/${id}`)
export const apiDeleteStuff = id => deleteReq(`${apiBase}/stuff/${id}`)
export const apiCreateStuff = pl => postJson(`${apiBase}/stuff/`, pl)
export const apiUpdateStuff = (id, pl) => putJson(`${apiBase}/stuff/${id}`, pl)

If we want to run tests that involve these api* functions, it would be nice if we could mock ourFetch directly like this:

const injector = require('inject-loader!somethingThatUsesApiCalls.js')
const sth = injector({
  './stuff.js': {ourFetch: () => {json: () => ({ok: 1})}}
})

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

1 participant