cmorten / superoak

HTTP assertions for Oak made easy via SuperDeno. 🐿 🦕
https://cmorten.github.io/superoak/
MIT License
121 stars 8 forks source link

Support Deno 2.0 #32

Open andrija-tosic-rs opened 1 month ago

andrija-tosic-rs commented 1 month ago

Issue

Setup:

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/superdeno@4.9.0/src/test.ts:192:7)
    at https://deno.land/x/superdeno@4.9.0/src/test.ts:558:21
    at close (https://deno.land/x/superdeno@4.9.0/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.

petruki commented 1 month ago

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;
jlp-craigmorten commented 1 month ago

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.

andrija-tosic-rs commented 1 month ago

Thank you both for responding so quickly. If you don't have anything to add, I can close this issue.