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

Import from unified instead of relative index.js #217

Closed
wants to merge 1 commit into from

Conversation

remcohaszing
Copy link
Member

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and couldn’t find anything (or linked relevant results below)
  • If applicable, I’ve added docs and tests

Description of changes

In test code, imports now import from unified instead of a relative path to index.js. This way package exports are also tested, both runtime and for type checking. This truly tests the public interface.

I’m not saying we should go over all packages to apply this immediately, but when working on a package, I think it’s a good idea to apply this pattern. This PR is mostly to share and discuss the idea.

Sorry, something went wrong.

@remcohaszing remcohaszing added 🕸️ area/tests This affects tests 💬 type/discussion This is a request for comments 🤞 phase/open Post is being triaged manually labels Mar 26, 2023
@remcohaszing remcohaszing requested a review from a team March 26, 2023 14:50
@wooorm
Copy link
Member

wooorm commented Mar 26, 2023

This way package exports are also tested, both runtime and for type checking. This truly tests the public interface.

I don’t think this is really done this way?
I think something like this checks it better.
Or at least, together. I’d propose:

  const mod = await import('unified')
  assert.deepEqual(
    Object.keys(mod).sort(),
    ['unified'],
    'should expose the public api'
  )

If there are other exports, we can have such blocks for those too.


Furthermore, doesn’t this actually checks the nested unified, not the code we’re working on here?
At least, this used to be something, as far as I was aware, that was only supported if an exports field was available? 🤔 And most of our packages don’t have that yet (which I’m for, just that, it’s a breaking change for not much benefit)?

@remcohaszing
Copy link
Member Author

Furthermore, doesn’t this actually checks the nested unified, not the code we’re working on here? At least, this used to be something, as far as I was aware, that was only supported if an exports field was available? thinking And most of our packages don’t have that yet (which I’m for, just that, it’s a breaking change for not much benefit)?

You’re right, I was under the impression we already used export maps. I think it’s nice to use them, because it marks internals as private. This prevents accidental use, I think also of internal types. But indeed, it’s a breaking change.


I don’t think this is really done this way? I think something like this checks it better. Or at least, together. I’d propose:

  const mod = await import('unified')
  assert.deepEqual(
    Object.keys(mod).sort(),
    ['unified'],
    'should expose the public api'
  )

If there are other exports, we can have such blocks for those too.

Given the above statement that we don’t use export maps, I think you mean this:

  const mod = await import('../index.js')
  assert.deepEqual(
    Object.keys(mod).sort(),
    ['unified'],
    'should expose the public api'
  )

I don’t really see the value of this assertion TBH. We already test against the main index.js file, which is currently as close to the public interface as we can get.


Another benefit of my proposal (for packages that do use export maps) is that we can test against multiple export conditions. This is already done in https://github.com/syntax-tree/hast-util-from-html-isomorphic/blob/main/package.json#L64-L66 for example.

node --test
node --test --conditions browser
node --test --conditions worker

@wooorm
Copy link
Member

wooorm commented Mar 27, 2023

I don’t really see the value of this assertion TBH. We already test against the main index.js file, which is currently as close to the public interface as we can get.

We don’t test what’s part of the API. It is good to test what is part of the API.
Your last phrase, “which is currently as close to the public interface as we can get.” answers it: we can get closer.

@wooorm wooorm closed this in 932c140 Aug 9, 2023
@wooorm wooorm added the 💪 phase/solved Post is done label Aug 9, 2023
@wooorm wooorm deleted the import-unified branch August 9, 2023 13:59
@github-actions

This comment has been minimized.

@github-actions github-actions bot removed the 🤞 phase/open Post is being triaged manually label Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🕸️ area/tests This affects tests 💪 phase/solved Post is done 💬 type/discussion This is a request for comments
Development

Successfully merging this pull request may close these issues.

None yet

2 participants