Support Deno 2.0
Issue
Setup:
- Deno Version: 2.0.0 (stable, release, aarch64-apple-darwin)
- v8 Version: 12.9.202.13-rusty
- Typescript Version: 5.6.2
- SuperOak Version: 4.8.1
Details
Deno test started breaking after upgrading to Deno 2.0:
Test code:
Deno.test({
name: 'init test',
fn: async () => {
const initReq = await superoak(app);
const initRes = await initReq
.post('/init')
.set('Content-Type', 'application/json')
.send(initPayloadDemo)
.expect(Status.OK)
sanitizeOps: false,
sanitizeResources: false,
});
ERRORS
./src/tests/play.test.ts (uncaught error)
error: (in promise) ReferenceError: window is not defined
(window as any)[SHAM_SYMBOL].promises,
^
at completeXhrPromises (https://deno.land/x/[email protected]/src/test.ts:192:7)
at https://deno.land/x/[email protected]/src/test.ts:558:21
at close (https://deno.land/x/[email protected]/src/close.ts:47:52)
This error was not caught from a test and caused the test runner to fail on the referenced module.
It most likely originated from a dangling promise, event/timeout handler or top-level code.
Superoak still works fine on Deno 1.46.3, the latest Deno 1.x version before 2.0.
This is a problem with superdeno module. I am not quite familiar with the code that handles the xhr sham, but you can try this workaround to make your tests work with Deno v2 and superoak.
Before your tests, run this line:
(globalThis as any).window = globalThis;
Hi all 👋
As @petruki points out, it seems there has been a long lived bug in https://github.com/cmorten/superdeno/blob/main/src/test.ts#L192 which makes Deno v2 fall over (this line should read (globalThis as any)[SHAM_SYMBOL].promises!)
I could patch this line however both superdeno and superoak were only ever meant to be drop-in replacements for the supertest package in a world where NPM wasn't supported by Deno.
Deno is now very much claiming Node and NPM compatability (especially with v2) so I would encourage as you do the major upgrades to consider migrating off of superdeno and superoak.
I might consider continuing to support superoak as a thin wrapper over supertest, but will most likely look to sunset superdeno now that it's purpose is over.
Thank you both for responding so quickly. If you don't have anything to add, I can close this issue.
With @petruki's work-around superoak is running on Deno 2.3.1, thanks.
However, that work-around causes VS Code to show this warning:
anytype is not allowed⏎Use a specific type other thanany
Use the following code to make the VS Code warning issue to also go away:
(globalThis as {window: unknown}).window = globalThis;
@doga glad the workaround worked for you.
As for the warning, if it's not an issue, you can bypass deno lint and vscode warnings by adding // deno-lint-ignore no-explicit-any on top of that line.
you can bypass deno lint and vscode warnings [...]
Good to know.
BTW I think the Deno 2+ fix should be mentioned on the project's README. Oak is an essential project for the Deno ecosystem, and this makes superoak important to get right. The key here is sponsoring the project.
I am sticking with superoak for now, even if there is another issue which causes my Deno tests to fail if I use superoak, even though all assertions pass, but I won't file a report just yet:
error: Leaks detected:
- "fetchCancelHandle" was created during the test, but not cleaned up during the test. Close the resource before the end of the test.
- An async operation to send a HTTP request was started in this test, but never completed. This is often caused by not awaiting the result of a `fetch` call.
To get more details where leaks occurred, run again with the --trace-leaks flag.
/ returns html => ./test/app_test.ts:117:6
error: Leaks detected:
- "fetchCancelHandle" was created before the test started, but was cleaned up during the test. Do not close resources in a test that were not created during that test.
- An async operation to send a HTTP request was started before the test, but completed during the test. Async operations should not complete in a test if they were not started in that test. This is often caused by not awaiting the result of a `fetch` call.
To get more details where leaks occurred, run again with the --trace-leaks flag.
FAILURES
user account can be created => ./test/app_test.ts:79:6
/ returns html => ./test/app_test.ts:117:6
FAILED | 0 passed | 2 failed (137ms)
error: Test failed