cmorten / superdeno

Super-agent driven library for testing Deno HTTP servers.
https://cmorten.github.io/superdeno/
MIT License
124 stars 6 forks source link

Getting async leaking error when testing opine v2 #38

Closed twilson63 closed 2 years ago

twilson63 commented 2 years ago

Issue: Getting async leaking error when testing opine v2

Setup:

Details

When running a simple test with opine@2.0.0 Deno Test Assertions return AssertionError: Test case is leaking async ops. - there are no async's in the example code which is pasted below.

server.ts

import { opine } from 'https://deno.land/x/opine@2.0.0/mod.ts'

export function build() {

 const app = opine();

  app.get('/', function (req, res) {
    res.send('Hello World')
  })

  return app
}

test.ts

import {superdeno} from 'https://deno.land/x/superdeno@4.7.1/mod.ts'
import { build } from './server.ts'
const app = build()

Deno.test('hello world', () => {
  superdeno(app)
    .get('/')
    .expect(200)
})

Run

deno test -A --unstable test.ts

Output

running 1 test from file:///workspace/deno-playground/test.ts
test hello world ... FAILED (12ms)

failures:

hello world
AssertionError: Test case is leaking async ops.
Before:
  - dispatched: 0
  - completed: 0
After:
  - dispatched: 2
  - completed: 1
Ops:
  op_net_accept:
    Before:
      - dispatched: 0
      - completed: 0
    After:
      - dispatched: 1
      - completed: 0

Make sure to await all promises returned from Deno APIs before
finishing test case.
    at assert (deno:runtime/js/06_util.js:41:13)
    at asyncOpSanitizer (deno:runtime/js/40_testing.js:128:7)
    at async resourceSanitizer (deno:runtime/js/40_testing.js:144:7)
    at async Object.exitSanitizer [as fn] (deno:runtime/js/40_testing.js:176:9)
    at async runTest (deno:runtime/js/40_testing.js:381:7)
    at async Object.runTests (deno:runtime/js/40_testing.js:494:22)

failures:

        hello world

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out (147ms)

error: Test failed
asos-craigmorten commented 2 years ago

Hey @twilson63 👋

The constructed superdeno chain is a promise. Can you try...

import {superdeno} from 'https://deno.land/x/superdeno@4.7.1/mod.ts';
import { build } from './server.ts';
const app = build();

Deno.test('hello world', async () => {
  await superdeno(app)
    .get('/')
    .expect(200);
});

And see if that resolves the issue?

twilson63 commented 2 years ago

Hey @asos-craigmorten,

Adding await, creates some strange results, randomly it seems to either pass or fail with the leaking async issue.

cmorten commented 2 years ago

Hey @twilson63, have released https://deno.land/x/superdeno@4.7.2 - let me know if you're still experiencing the issue.