mercurius-js / auth

Mercurius Auth Plugin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tests fail on the latest main for Node.js 20.x

jonnydgreen opened this issue · comments

When running the tests against the latest commit on main with Node.js 20.x, I get the following error for the following test:

Test:

test('the single filter preExecution lets the app crash', async (t) => {
  const app = Fastify()
  t.teardown(app.close.bind(app))

  app.register(mercurius, {
    schema,
    resolvers
  })

  app.register(mercuriusAuth, {
    authContext,
    applyPolicy: authPolicy,
    filterSchema: true,
    authDirective: 'auth'
  })

  app.register(async function plugin () {
    throw new Error('boom')
  })

  try {
    await app.ready()
  } catch (error) {
    t.equal(error.message, 'boom')
  }
})

Error:

test/introspection-filter.js 2>
test/introspection-filter.js 2>   #  /Users/jonny/.asdf/installs/nodejs/20.11.1/bin/node[96991]: void node::InternalCallbackScope::Close() at ../src/api/callback.cc:145
test/introspection-filter.js 2>   #  Assertion failed: (env_->execution_async_id()) == (0)
test/introspection-filter.js 2>
test/introspection-filter.js 2> ----- Native stack trace -----
test/introspection-filter.js 2>
test/introspection-filter.js 2>  1: 0x10cd5a6c5 node::Abort() [/Users/jonny/.asdf/installs/nodejs/20.11.1/bin/node]
test/introspection-filter.js 2>  2: 0x10cd5a413 node::Assert(node::AssertionInfo const&) [/Users/jonny/.asdf/installs/nodejs/20.11.1/bin/node]
test/introspection-filter.js 2>  3: 0x10cc6fd39 node::InternalCallbackScope::Close() [/Users/jonny/.asdf/installs/nodejs/20.11.1/bin/node]
test/introspection-filter.js 2>  4: 0x10cc6f79e node::InternalCallbackScope::~InternalCallbackScope() [/Users/jonny/.asdf/installs/nodejs/20.11.1/bin/node]
test/introspection-filter.js 2>  5: 0x10cdcaa46 node::PerIsolatePlatformData::RunForegroundTask(std::__1::unique_ptr<v8::Task, std::__1::default_delete<v8::Task> >) [/Users/jonny/.asdf/installs/nodejs/20.11.1/bin/node]
test/introspection-filter.js 2>  6: 0x10cdc9477 node::PerIsolatePlatformData::FlushForegroundTasksInternal() [/Users/jonny/.asdf/installs/nodejs/20.11.1/bin/node]
test/introspection-filter.js 2>  7: 0x10d872931 uv__async_io [/Users/jonny/.asdf/installs/nodejs/20.11.1/bin/node]
test/introspection-filter.js 2>  8: 0x10d8862cc uv__io_poll [/Users/jonny/.asdf/installs/nodejs/20.11.1/bin/node]
test/introspection-filter.js 2>  9: 0x10d872f36 uv_run [/Users/jonny/.asdf/installs/nodejs/20.11.1/bin/node]
test/introspection-filter.js 2> 10: 0x10cc70700 node::SpinEventLoopInternal(node::Environment*) [/Users/jonny/.asdf/installs/nodejs/20.11.1/bin/node]
test/introspection-filter.js 2> 11: 0x10cda293b node::NodeMainInstance::Run(node::ExitCode*, node::Environment*) [/Users/jonny/.asdf/installs/nodejs/20.11.1/bin/node]
test/introspection-filter.js 2> 12: 0x10cda26ac node::NodeMainInstance::Run() [/Users/jonny/.asdf/installs/nodejs/20.11.1/bin/node]
test/introspection-filter.js 2> 13: 0x10cd1eb93 node::Start(int, char**) [/Users/jonny/.asdf/installs/nodejs/20.11.1/bin/node]
test/introspection-filter.js 2> 14: 0x11dee052e
 FAIL  test/introspection-filter.js 19 OK 5s
  command: /Users/jonny/.asdf/installs/nodejs/20.11.1/bin/node
  args:
    - test/introspection-filter.js
  exitCode: null
  signal: SIGABRT

After some debugging and a quick look at the code, it seems like the issue is with the following line: https://github.com/mercurius-js/auth/blob/main/lib/filter-schema.js#L16

Gracefully handling the error by logging out instead seems to solve the issue whilst still preserving the integrity of the test:

// the filter hook must be the last one to be executed (after all the authContextHook ones)
app.ready(err => {
  if (err) {
    app.log.warn('Error occurred during the app.ready hook, failing gracefully: ', err)
  } else {
    app.graphql.addHook('preExecution', filterGraphQLSchemaHook.bind(app))
  }
})

wdyt?

I'll get a PR up for this tomorrow, let me know if you think of any other/better suggestions on the solution :)

No clue tbh, this seems a bug between tap and Node.js. Updating this to latest tap (or to node:test`) would fix it

No clue tbh, this seems a bug between tap and Node.js. Updating this to latest tap (or to node:test`) would fix it

Okay, I'll have a play! I did upgrade tap to the latest but the test still failed - I'll open a PR to investigate further :)