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

Support for multiple server instances #8

Open
sazzer opened this issue Apr 21, 2022 · 0 comments
Open

Support for multiple server instances #8

sazzer opened this issue Apr 21, 2022 · 0 comments

Comments

@sazzer
Copy link

sazzer commented Apr 21, 2022

(Note - I'm not totally sure if this is a problem with Fastify itself or with this plugin, but it's only this plugin that appears to be affected by it)

I'm currently using Fastify and have tried to add this plugin in order to generate hypermedia links automatically from the configuration. That works great, until it comes to testing.

I'm testing with Jest, and the test process is:

  • Build my server
  • Inject requests into it
  • Assert the responses

This also works great, and keeps every test isolated. Except that adding this plugin in has broken it. It seems that somehow the routes added to one server instance are remembered in the next one, which means I can only ever run a single test at a time.

This is a minimal reproducing test case: (Using TypeScript but should be trivial to do in JS as well)

import fastify from 'fastify';
import { plugin as reverseRoutesPlugin } from 'fastify-reverse-routes';

async function doTest() {
  const server = fastify();
  server.register(reverseRoutesPlugin);
  server.register(async function (fastify) {
    fastify.route({
      method: 'GET',
      url: '/',
      name: 'get_home_document',
      handler: async () => {
        return 'Hello';
      }
    });
  });

  try {
    await server.inject({
      method: 'GET',
      url: '/'
    });
  } finally {
    await server.close();
  }
}

describe('Some tests', () => {
  test('This is test 1', async () => {
    await doTest();
  });
  test('This is test 2', async () => {
    await doTest();
  });
});

Running this will produce:

 FAIL  tests/broken.spec.ts
  Some tests
    ✓ This is test 1 (71 ms)
    ✕ This is test 2 (2 ms)

  ● Some tests › This is test 2

    Route with name get_home_document already registered

      at Object.<anonymous> (node_modules/fastify-reverse-routes/index.js:22:15)
      at Object.afterRouteAdded (node_modules/fastify/lib/route.js:223:18)
      at node_modules/fastify/lib/route.js:188:25
      at Object._encapsulateThreeParam (node_modules/avvio/boot.js:551:7)
      at Boot.timeoutCall (node_modules/avvio/boot.js:447:5)
      at Boot.callWithCbOrNextTick (node_modules/avvio/boot.js:428:19)
      at Boot._after (node_modules/avvio/boot.js:273:26)
      at Plugin.Object.<anonymous>.Plugin.exec (node_modules/avvio/plugin.js:132:19)
      at Boot.loadPlugin (node_modules/avvio/plugin.js:274:10)

So you can clearly see that the second test fails because the route name is remembered from the first test.

Cheers

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

No branches or pull requests

1 participant