Coder Social home page Coder Social logo

Comments (6)

remcohaszing avatar remcohaszing commented on May 18, 2024 1

Seems we'd have to do ready and close ourselves regardless, which somewhat defeats the point... 🙁

That part has already been abstracted away. I want this library to make the test code as simple as possible. 😉


The issue is that fastify instances can’t be reused. I.e. the following code won’t work if multiple tests are present.

// app.js
import * as Fastify from 'fastify'

const app = Fastify();
app.get('/', async (request, reply) => {
  reply.send({ hello: 'world' });
});
export default app;

// app.test.js
import { request, setTestApp } from 'axios-test-instance'
import app from './app'

beforeEach(() => setTestApp(app))

The following examples do work with the current PR. This is the same pattern as is used in their supertest example.

// createApp.js
import * as Fastify from 'fastify'

export default function createApp() {
  const app = Fastify();
  app.get('/', async (request, reply) => {
    reply.send({ hello: 'world' });
  });
  return app;
}

// createApp.test.js
import { request, setTestApp } from 'axios-test-instance'
import createApp from './createApp'

beforeEach(() => setTestApp(createApp()))
// createApp.js
import * as Fastify from 'fastify'

export default function createApp() {
  const app = Fastify();
  app.get('/', async (request, reply) => {
    reply.send({ hello: 'world' });
  });
  return app;
}

// createApp.test.js
import { request, setTestApp } from 'axios-test-instance'
import createApp from './createApp'

beforeAll(() => setTestApp(createApp()))

Because jest creates new instances for each test file when module isolation isn’t disabled, the following should also just work with jest.

// app.js
import * as Fastify from 'fastify'

const app = Fastify();
app.get('/', async (request, reply) => {
  reply.send({ hello: 'world' });
});
export default app;

// app.test.js
import { request, setTestApp } from 'axios-test-instance'
import app from './app'

beforeAll(() => setTestApp(app))

If the scenario above meets your requirements, I’ll gladly finalize my changes and release them with a footnote about the quirks of using this library with fastify.

from axios-test-instance.

remcohaszing avatar remcohaszing commented on May 18, 2024

Thank you for your interest!

I gave it a try, but as you can see in the failing tests of the pull request, starting a fastify instance a second time, even after closing it properly, fails.

Also I’m not too happy about the framework detection mechanism I came up with.

I don’t think I’m going to dive much deeper into support for Fastify, but if someone else manages to fix it, I’ll gladly merge it.

from axios-test-instance.

SimenB avatar SimenB commented on May 18, 2024

In their docs, they have a supertest example which gets passed the server. Would that work, somehow?

https://www.fastify.io/docs/v3.0.x/Testing/#example-1

Seems we'd have to do ready and close ourselves regardless, which somewhat defeats the point... 🙁

from axios-test-instance.

SimenB avatar SimenB commented on May 18, 2024

Yeah, that should cover my use cases, thanks! I think documenting that behavior should be enough

from axios-test-instance.

remcohaszing avatar remcohaszing commented on May 18, 2024

This has been released as 3.1.0.

from axios-test-instance.

SimenB avatar SimenB commented on May 18, 2024

Thanks! It works great, except for https://github.com/remcohaszing/axios-test-instance/pull/3/files#r439827863

from axios-test-instance.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.