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

Examples with Testing #4

Closed
theodesp opened this issue May 3, 2019 · 7 comments
Closed

Examples with Testing #4

theodesp opened this issue May 3, 2019 · 7 comments
Labels
enhancement New feature or request

Comments

@theodesp
Copy link

theodesp commented May 3, 2019

We need examples for how to test the endpoints.

For example here is what I did:

// src/server.ts
export const app = createServer({
    port: 3000,
    hostname: '127.0.0.1',
    httpListener,
});

// tests/e2e/health.test.ts
import request from 'supertest';
import {app} from '../../src/server';

describe('/health', () => {
    it('returns 200 OK', () => {
        return request(app.server).get('/health')
            .expect(200);
    });
});

@JozefFlakus JozefFlakus added the enhancement New feature or request label May 21, 2019
@JozefFlakus
Copy link
Member

Yup, that's right. I will try to update the docs with a new topic inside Advanced group. It will reflect the integration tests topic. 💪

@JozefFlakus
Copy link
Member

Solved with introduction of @marblejs/testing
https://docs.marblejs.com/testing/http-testing

@viglioni
Copy link
Contributor

viglioni commented Dec 4, 2021

Sorry to comment on a closed issue or if I am in the wrong place, but I'm having a lot of trouble trying to test both ways

I'm using "@marblejs/core": "^3.5.0" also "@marblejs/testing": "^3.5.0"

I tried to upgrade to 4, but I had a lot of problems with rxjs 7, then I decided to go back to 3.5.0

I'm trying to do a very simple test:

The endpoint:

import { r } from '@marblejs/core'
import { mapTo } from 'rxjs/operators'

export const helloThere = r.pipe(
  r.matchPath('/'),
  r.matchType('GET'),
  r.useEffect(req => req.pipe(mapTo({ body: 'Hello there!' }))),
)

The test using jest:

const listener = httpListener({ effects: [helloThere] })

const httpTestBed = createHttpTestBed({
  listener,
})

 it('should return OK', async () => {
        const { request, finish } = await httpTestBed()

        const response = await pipe(
          request('GET'),
          request.withPath('/'),
          request.send,
        )
        
	expect(response.statusCode).toEqual(200)
				
        await finish()
      })

But I've got the error:

TypeError: Cannot read property 'path' of undefined

      at node_modules/@marblejs/core/dist/http/router/http.router.factory.js:13:59
          at Array.forEach (<anonymous>)
      at Object.<anonymous>.exports.factorizeRouting (node_modules/@marblejs/core/dist/http/router/http.router.factory.js:12:12)
      at Object.<anonymous>.exports.factorizeRoutingWithDefaults (node_modules/@marblejs/core/dist/http/router/http.router.factory.js:46:78)
      at node_modules/@marblejs/core/dist/http/server/http.server.listener.js:15:43
      at node_modules/fp-ts/lib/Reader.js:85:78
      at node_modules/@marblejs/core/dist/http/server/http.server.js:46:26
      at fulfilled (node_modules/@marblejs/core/dist/http/server/http.server.js:5:58)

Am I doing anything wrong?


The example with supertest doesn't work either.

node:internal/process/promises:246
         triggerUncaughtException(err, true /* fromPromise */);
         ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "TypeError: Cannot read property 'path' of undefined".] {
 code: 'ERR_UNHANDLED_REJECTION'
}

Thanks for your attention.

@JozefFlakus
Copy link
Member

Hi @viglioni,

Do you have a small reproducible repository that shows this problem?
I'm not sure if it will help you but the main repository shows an example usage of @marblejs/testing package.

Thanks!

@viglioni
Copy link
Contributor

viglioni commented Dec 7, 2021

That example helped a lot! Thanks!!

I had seen only this example that I was not able to reproduce, probably because it was v2

This usually means that there are asynchronous operations that weren't stopped in your tests.

But I'm getting a strange promise error on Jest. Do you have any idea the reason?

https://github.com/Viglioni/marblejs-test-example

@JozefFlakus
Copy link
Member

JozefFlakus commented Dec 9, 2021

@viglioni probably because you import the listener from main.ts which when evaluated (imported) starts listening to the server (it produces side effect):
https://github.com/Viglioni/marblejs-test-example/blob/main/src/main.ts#L23

I highly recommend to separate server and listener definitions and bootstrap them in a separate file.

@viglioni
Copy link
Contributor

viglioni commented Dec 9, 2021

Thanks a lot!

Also congratulations for the lib :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants