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

chore(test): Migrate test suites from mocha to node test runner #160

Merged
merged 7 commits into from
Sep 12, 2024

Conversation

Zen-cronic
Copy link
Collaborator

Resolves #157 & #158

Changes

  • Replaced mocha with node --test script
  • Replaces expect from chai with node:assert

Signed-off-by: Kaung Zin Hein <kaungzinhein113@gmail.com>
Signed-off-by: Kaung Zin Hein <kaungzinhein113@gmail.com>
@Zen-cronic
Copy link
Collaborator Author

The CI tests are ran on node-version: [14.x, 16.x, 18.x, 20.x, 22.x]. But node test runner was added in v16.17.0. So would support for node 14 be dropped?

@evert
Copy link
Collaborator

evert commented Sep 5, 2024

Nice! This is awesome. I think it's fine to finally drop Node 14. It's quite old at this point.

@Zen-cronic
Copy link
Collaborator Author

Zen-cronic commented Sep 5, 2024

Interestingly, the tests are not run or only run partially, depending on the node version.

  • v16.20.2: no tests run and hanging.
  • v18.20.4: only the first (alphabetically) test file is run authorization-code.ts and left hanging.
  • v20.17.0: same as v18
  • v22.8.0: same as v18

*Tested locally and as observed in CI

The test runner doesn't support running .ts files right out of the box - even more workarounds depending on cjs or esm. Check this issue.

@evert
Copy link
Collaborator

evert commented Sep 5, 2024

Good catch! I recently migrated a different library to npm --test and this is how I've solved that:

https://github.com/badgateway/structured-headers/pull/58/files

Basically I:

  • Installed tsx (and ditched ts-node, which doesn't work well with ESM anyway).
  • Instead of running node --test, run tsx --test test/**/*.ts

This mostly solved all my problems. tsx is effectively a wrapper around node that sets up parsing Typescript transparently.

@Zen-cronic
Copy link
Collaborator Author

Oh I see. Will try it!

Signed-off-by: Kaung Zin Hein <kaungzinhein113@gmail.com>
@Zen-cronic
Copy link
Collaborator Author

Zen-cronic commented Sep 7, 2024

Interestingly, the tests are not run or only run partially, depending on the node version.

The tests were passing but the process was hanging. It seems that node:test isn't as dev-ready as other mature test frameworks. All connections/promises had to be closed explicitly like this. Whereas with mocha, the process ended as expected after the tests pass/fail.

Currently, the tests in fetch-wrapper.ts will all pass but the process will hang, so the next test file isn't continued. More specifically, the first two tests in that file are causing the hang - verified by attaching only and running locally. Will investigate further.

(So ts-node wasn't the issue, but switched to tsx for esm like you mentioned)

package.json Outdated Show resolved Hide resolved
@evert
Copy link
Collaborator

evert commented Sep 10, 2024

The tests were passing but the process was hanging. It seems that node:test isn't as dev-ready as other mature test frameworks. All connections/promises had to be closed explicitly like this. Whereas with mocha, the process ended as expected after the tests pass/fail.

I'd imagine that this is a deliberate choice by Node, to keep things somewhat simple and explicit, but yeah i bet it was annoying to deal with.

Currently, the tests in fetch-wrapper.ts will all pass but the process will hang, so the next test file isn't continued. More specifically, the first two tests in that file are causing the hang - verified by attaching only and running locally. Will investigate further.

I'm almost certain this is because fetch-wrapper calls setTimeout which keeps an event open.

One idea is to explicitly disable this, or call clearTimeout in the cleanup function for this. (you may need to cast that object to any first to ignore the fact that it's a private function).

There's definitely more hoops here than I expected, so I understand if you're out of patience. If so I'll definitely build on your work to complete this. Either way I really appreciate the work so far!

Signed-off-by: Kaung Zin Hein <kaungzinhein113@gmail.com>
@Zen-cronic
Copy link
Collaborator Author

I'd imagine that this is a deliberate choice by Node, to keep things somewhat simple and explicit, but yeah i bet it was annoying to deal with.

I'm almost certain this is because fetch-wrapper calls setTimeout which keeps an event open.

One idea is to explicitly disable this, or call clearTimeout in the cleanup function for this. (you may need to cast that object to any first to ignore the fact that it's a private function).

Gotcha, I added a fix. Feel free to adjust for any improvement.

@evert
Copy link
Collaborator

evert commented Sep 12, 2024

Very frustrating! If I'm reading the errors right, we need tsx 3 for Node =< 16, and tsx 4 for Node > 20

@evert
Copy link
Collaborator

evert commented Sep 12, 2024

Given that dropping older Node versions is already a major change, maybe it's time to also drop Node 16, and make this PR of a future 3.0 release.

@evert evert merged commit a90ec80 into badgateway:main Sep 12, 2024
4 checks passed
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.

Switch to node --test from mocha.
2 participants