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

Add basic server smoke test #172

Closed
wants to merge 4 commits into from
Closed

Conversation

alexrudd2
Copy link
Collaborator

@alexrudd2 alexrudd2 commented Jul 12, 2023

Requires #170 since transient superagent silently dropped Node 12. (DefinitelyTyped/DefinitelyTyped#58445)

This is a basic smoke test to make sure the Express server starts (sometimes dependency updates break it). It currently works, but has a warning:

A worker process has failed to exit gracefully and has been force exited.
This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks. Active timers can also cause this, ensure that .unref() was called on them.

I'm nearly certain this is caused by server.ts trying to connect to the EBB, but haven't quite figured out how to mock it out. Help would be appreciated.

src/__tests__/server-smoke.test.ts Outdated Show resolved Hide resolved
Comment on lines 5 to 11
jest.mock("../server", () => {
const original = jest.requireActual("../server");
return {
...original,
ebbs: jest.fn()
};
});
Copy link
Owner

Choose a reason for hiding this comment

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

I assume this doesn't work because the server.ts code has an internal reference to ebbs, which doesn't get replaced by this mock. I'd recommend refactoring startServer to be more test-friendly by adding an optional argument which is a function to call to get the list of attached EBBs, defaulting to the internal ebbs function when not specified.

Copy link
Collaborator Author

@alexrudd2 alexrudd2 Jul 15, 2023

Choose a reason for hiding this comment

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

More or less - the solution I came up with was exporting a function and then having the module import itself, which allows jest to mock that exported function. (In this case waitForEbb)

After various iterations, I think 3388136 mocks the least amount of code / runs the most "real" code. What do you think?

@alexrudd2
Copy link
Collaborator Author

> [email protected] test
> jest

 PASS  src/__tests__/planning.test.ts
 PASS  src/__tests__/util.test.ts
 PASS  src/__tests__/ebb.test.ts
 PASS  src/__tests__/server-smoke.test.ts
  ● Console

    console.log
      Server listening on http://[::]:61226

      at Server.<anonymous> (src/server.ts:233:15)

    console.log
      Found EBB at fake-ebb-path

      at ebbs_1 (src/server.ts:273:15)
          at ebbs_1.next (<anonymous>)


Test Suites: 4 passed, 4 total
Tests:       1 skipped, 13 passed, 14 total
Snapshots:   0 total
Time:        1.752 s, estimated 2 s
Ran all test suites.

@alexrudd2 alexrudd2 marked this pull request as ready for review July 21, 2023 04:11
@alexrudd2
Copy link
Collaborator Author

Closing in favor of #187

@alexrudd2 alexrudd2 closed this Oct 3, 2023
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.

2 participants