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

refactor: replace mocha with node:test #1697

Merged
merged 27 commits into from
Sep 21, 2023
Merged

refactor: replace mocha with node:test #1697

merged 27 commits into from
Sep 21, 2023

Conversation

robertsLando
Copy link
Member

@robertsLando robertsLando commented Sep 18, 2023

Fixes #1694

/cc @mcollina

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job! tests are failing

@codecov
Copy link

codecov bot commented Sep 19, 2023

Codecov Report

Patch coverage has no change and project coverage change: +0.03% 🎉

Comparison is base (d6fd3a8) 81.00% compared to head (35253d4) 81.03%.

❗ Current head 35253d4 differs from pull request most recent head cf882c3. Consider uploading reports for the commit cf882c3 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1697      +/-   ##
==========================================
+ Coverage   81.00%   81.03%   +0.03%     
==========================================
  Files          21       21              
  Lines        1369     1366       -3     
  Branches      324      323       -1     
==========================================
- Hits         1109     1107       -2     
+ Misses        183      182       -1     
  Partials       77       77              

see 18 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@robertsLando
Copy link
Member Author

robertsLando commented Sep 19, 2023

@mcollina Tests are working now BTW them are much slower then before

A possible way to improve this would be to enable parallel tests. Problem is that abstract_client tests is used in multiple test files and it setup some servers that listen on a port (so this will end up with port conflict). I would like to know if there is a way using process.pid or else to shift ports so there are no conflicts. Otherwise we should use a package to get the first free open port every time listen is called

@mcollina
Copy link
Member

A possible way to improve this would be to enable parallel tests. Problem is that abstract_client tests is used in multiple test files and it setup some servers that listen on a port (so this will end up with port conflict). I would like to know if there is a way using process.pid or else to shift ports so there are no conflicts. Otherwise we should use a package to get the first free open port every time listen is called

Indeed. Avoiding reusing those broker instances will fix a lot of the flakyness. It's expected that the tests will take longer. As you said, the solution is to enable parallel testing and use different ports.

@robertsLando
Copy link
Member Author

robertsLando commented Sep 19, 2023

@mcollina Curious thing, seems the internal types of beforeEach and afterEach are wrong. First argument should be the test context, instead they set the result callback as first but that results in an error.

Expected (based on types):

beforeEach((done) => {
		build((err, _store) => {
			store = _store
			done(err)
		})
	})

Correct:

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
	// @ts-expect-error
	beforeEach((_ctx, done) => {
		build((err, _store) => {
			store = _store
			done(err)
		})
	})

same for afterEach

@robertsLando
Copy link
Member Author

robertsLando commented Sep 19, 2023

@mcollina I tried multiple approaches when running tests. The first was by using test/runTests.ts:

import { readdirSync } from 'node:fs'
import { run } from 'node:test'
import process from 'node:process'
import { tap } from 'node:test/reporters'
import { basename } from 'node:path'

const files = readdirSync(__dirname)
	.filter((f) => f.endsWith('.ts') && f !== basename(__filename))
	.map((f) => `${__dirname}/${f}`)

run({ files, timeout: 60 * 1000, concurrency: true })
	.compose(tap)
	.pipe(process.stdout)

This runs the test concurrently but what I don't like is that if some test fail this is not reported at all, the test passes anyway as I think the exit code is 0 always, I'm not sure how to fix this, maybe by using a custom reporter?

I also tried to use spec reporter but it returns a class constructor and it cannot be used as tap, dot reporter is printing nothing at all. The only reporter I found to work is tap, the others doesn't work at all...

At the end I used another way to run test using node -r esbuild-register --test test/*.ts and that seems more reliable for now.

It takes 37 seconds on my PC but in actions it still takes more then 2 minutes, dunno if you have any suggestion, it's now working with parallel test without any problem on my end but seems not enabling concurrency on github actions.

Apart from this the PR is ready for review.

@robertsLando
Copy link
Member Author

robertsLando commented Sep 19, 2023

I have been able to cut down time to ~2min. Don't know if we can do better here.

@robertsLando robertsLando changed the title refactor: replace mocha with node:test refactor: replace mocha with node:test Sep 19, 2023
Comment on lines +28 to +41
testStream.on('test:fail', (data) => {
exitCode = 1
const error = data.details.error

summary.push(
`${data.file} - "${data.name}" (${Math.round(
data.details.duration_ms,
)}ms)\n${error.toString()} `,
)
})

testStream.on('test:stderr', (data) => {
summary.push(`${data.file} - Error:\n${data.message} `)
})
Copy link
Member Author

@robertsLando robertsLando Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dunno why but the summary is missed when running tests using run function. It is printed only when running test file directly with node or with --test. Maybe it's a bug or I'm missing something, anyway I created my custom summary in this way, this is needed also to correct the exit code in case there are errors

* @param i Index to shift the ports by
* @returns
*/
export default function getPorts(i = 0) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trick to be sure ports doesn't overlap, each file must use a different i in order to work

Comment on lines +11 to +13
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
beforeEach((_ctx, done) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the type error I mentioned above, use @ts-expect-error so in case this get fixed typescript will tell us

@robertsLando
Copy link
Member Author

Ping @mcollina

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@robertsLando robertsLando merged commit 069605b into main Sep 21, 2023
@robertsLando robertsLando deleted the refactor-tests branch September 21, 2023 08:03
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.

[refactor] Rewrite tests using tap/nodejs test runner
2 participants