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

Enrich the docs when it comes to types #269

Merged
merged 2 commits into from
Nov 10, 2023
Merged

Conversation

ahmed-hritani
Copy link
Contributor

@ahmed-hritani ahmed-hritani commented Nov 10, 2023

I noticed that after the addition of #267, there is nothing in the docs to show that esmock is now type generic.

@ahmed-hritani
Copy link
Contributor Author

Great work on this repo @iambumblehead, it made our lives way easier 🙏

@uwinkelvos
Copy link
Contributor

👍

@koshic koshic merged commit ac2af97 into iambumblehead:main Nov 10, 2023
@iambumblehead
Copy link
Owner

iambumblehead commented Nov 10, 2023

test('allows you to specify the type of returned exports', async () => {
  const { multiplyNumbers } = await esmock<typeof import('../src/utils.js')>(
    '../src/utils.js', {
    multiplyNumbers: (numbers: number[]): number => numbers.reduce((acc, current) => acc *= current, 1),
  })

  assert.equal(multiplyNumbers([1, 2, 3]), 6)
})

The example only works if strange things happen,

  1. there is a package named "multiplyNumbers" exporting only a default function reducer,
  2. "../src/utils.js" imports the "multiplyNumbers" package, then exports it to a named-export of the same name; import multiplyNumbers from 'multiplyNumbers'; export { multipyNumbers } (strange)
test('allows you to specify the type of returned exports', async () => {
  const { multiplyNumbers } = await esmock('../src/utils.js', {
    multiplyNumbers: numbers => numbers.reduce((acc, current) => acc *= current, 1)
  })

  assert.equal(multiplyNumbers([1, 2, 3]), 6)
})

The example would be easier to follow if utils used multiply in a more meaningful way and if it imported multiply from an obvious moduleId or path such as "../src/numbers.js", eg something like this,

test('allows you to specify the type of returned exports', async () => {
  const { makebabies } = await esmock('../src/rabbits.js', {
    '../src/numbers.js': {
      multiply: numbers => numbers.reduce((acc, current) => acc *= current, 1)
    }
  })

  assert.equal(makebabies({ pairs: 3 }).length, 28)
})

@iambumblehead
Copy link
Owner

@ahmed-hritani would you remake the README example to follow these points?

  • The example should work. It should be possible to write a real unit-test that passes with the example,
  • Each line less than 70 characters wide. This is a special optimization for the README so the examples fit inside most laptop screens without scrollbars,
  • The example should be slightly interesting, if possible, and should use a unicode emoji

@iambumblehead
Copy link
Owner

iambumblehead commented Nov 10, 2023

I like this example and I like if this is added to a separated block from the javascript one,


esmock examples in typescript

import test from 'node:test'
import assert from 'node:assert'
import esmock from 'esmock'
import type Rabbit from '../rabbit.js';

test('specify the type of export returned', async () => {
  const rabbit = await esmock<Rabbit>('../rabbit.js', {
    '../util.js': {
      multiply: (numbers: number[]): number => (
        numbers.reduce((acc, n) => acc *= n, 1))
    }
  })
  
  assert.equal(rabbit.makebabies({ pairs: 3 }), '🐇'
    + '🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇'
    + '🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇'
    + '🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇')
})

@ahmed-hritani
Copy link
Contributor Author

ahmed-hritani commented Nov 13, 2023

Thank you for your comments @iambumblehead, I created a new PR with an example that (hopefully) follow the constraints and added the constraints to the CONTRIBUTING.md file through a different PR.

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

Successfully merging this pull request may close these issues.

4 participants