-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
feat(core): support route injection #6573
feat(core): support route injection #6573
Conversation
Pull Request Test Coverage Report for Build d3899006-e65d-4975-bdbf-3fc8f301473a
💛 - Coveralls |
jest.spyOn doesn't mention changing the original method so that you can do Otherwise, can you check this test instead in your repo: import { Controller, Get, INestApplication, Query } from "@nestjs/common";
import { Test, TestingModule } from "@nestjs/testing";
import supertest from "supertest";
@Controller("api")
class TestController {
@Get("hello")
getHello(@Query("userName") userName: string) {
return `Hello, ${userName}!`;
}
}
describe("mocking route", () => {
const userName = "Lutz";
let moduleRef: TestingModule;
let controller: TestController;
let app: INestApplication;
let agent: supertest.SuperTest<supertest.Test>;
beforeEach(async () => {
const testingModuleBuilder = Test.createTestingModule({
controllers: [TestController],
});
moduleRef = await testingModuleBuilder.compile();
controller = moduleRef.get(TestController);
app = moduleRef.createNestApplication();
agent = supertest(app.getHttpServer());
jest.clearAllMocks();
});
test("spyOn", async () => {
const controllerSpy = jest.spyOn(controller, "getHello");
await app.init();
const res = await agent.get("/api/hello").query({ userName });
expect(res.status).toBe(200);
expect(res.text).toBe(`Hello, ${userName}!`);
//
// fail from here.
// reason: new impl (spy function) not called.
expect(controllerSpy).toBeCalledTimes(1);
expect(controllerSpy).toHaveBeenNthCalledWith(1, userName);
});
}); |
It is not written in the official API document, but it seems to work in that way. I did the test in my repo. If there is another problem, please let me know. |
Sure enough it does seem an undocumented functionality. Seems a bit weird, to not use the returned spy object. Does this change affect other testing libraries, like sinon, and tap? It'd be better to make a change that doesn't enforce people to use one testing library. |
I agree with what you are worried about. But, There will be little impact on other testing libraries. To show this, I did same tests with a various combinations of mocking libaray and test runner. Mocking library :
Test runner :
At least for the famous testing library written above, it ensures to work. |
LGTM |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
summary :
Currently, even if the router implementation changed, the new impl is ignored.
Due to the above reasons, the following e2e test fail :
What is the new behavior?
I suggest that if the routing function is overwritten, use the injected one rather than the original one.
Does this PR introduce a breaking change?
Other information