nestjs / serve-static

Serve static websites (SPA's) using Nest framework (node.js) šŸ„¦

Home Page:https://nestjs.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

E2e tests fail with version 2.1.1

vdechef opened this issue Ā· comments

I'm submitting a...


[x] Regression 
[ ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

When running e2e tests, @nestjs/serve-static uses noop loader.
Consequently, my tests for routing are failing because serve-static returns a 404 error on non-existing routes, instead of serving the index.html of the root folder.

With version 2.1.0, when running e2e tests serve-static was using express loader instead of noop loader. So when requesting http://myserver/non-existing-path serve-static was returning a response 200 with /index.html as content.

The culprit is this commit : db6e86d#diff-b38ccc22d6bb4ce4c12a2c61b89a974b

Expected behavior

Testing the routing of @nestjs/serve-static should be possible.

Minimal reproduction of the problem with instructions

Doing the same with @nestjs/serve-static 2.1.0 will succeed

Environment


Nest version: 7.1.3
 
For Tooling issues:
- Node version: 12.16.2
- Platform:  Linux

Wouldn't it be easier to simply use the NestFactory.create() instead? TestModule bootstraps providers before knowing whether the simulated Nest environment will act as an HTTP application, Microservice, etc.

Sorry, I'm not sure to understand what you mean.

In my tests code, I do something like this to initialise the NestJS server and run a simple GET request againt it :

    const moduleFixture: TestingModule = await Test.createTestingModule({
        imports: [AppModule],
    }).compile()
    const app = moduleFixture.createNestApplication()
    await app.init()
    const serv = app.getHttpServer()

    let response = await request(serv).get("/myendpoint")
    expect(response.body).toEqual("My endpoint was called")

You mean that it could be done with NestFactory.create() instead ?

You can use the following code instead:

    const app = NestFactory.create(AppModule);
    await app.init();
    const serv = app.getHttpServer()

    let response = await request(serv).get("/myendpoint")
    expect(response.body).toEqual("My endpoint was called")

Indeed, it work. Thanks for the support!

So if I understand correctly, for end-to-end tests I should instanciate the whole NestJS application. And I should stick to TestingModule for unit-testing of individual modules.