Fix console warn issues in discourse mirror tests (#1444)

@wchargin identified issues with the way we setup and reset the warning
mocks in discourse/mirror.test.js. During testing, we found issues where
an unexpected warning might not cause test failures, or an unexpected
warning could break subsequent tests.

This commit fixes both issues.

Test plan: Besides the fact that `yarn test` passes, we've found that
adding a single unexpected console.warn to a test will cause that test
(and only that test) to fail.

Paired with @wchargin
This commit is contained in:
Dandelion Mané 2019-11-11 19:20:53 -08:00 committed by GitHub
parent aabeda2403
commit d34ef1cb42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 11 deletions

View File

@ -153,18 +153,18 @@ class MockFetcher implements Discourse {
} }
describe("plugins/discourse/mirror", () => { describe("plugins/discourse/mirror", () => {
beforeAll(() => {
jest.spyOn(console, "warn").mockImplementation(() => {});
});
function spyWarn(): JestMockFn<[string], void> { function spyWarn(): JestMockFn<[string], void> {
return ((console.warn: any): JestMockFn<any, void>); return ((console.warn: any): JestMockFn<any, void>);
} }
beforeEach(() => { beforeEach(() => {
spyWarn().mockReset(); jest.spyOn(console, "warn").mockImplementation(() => {});
}); });
afterAll(() => { afterEach(() => {
expect(console.warn).not.toHaveBeenCalled(); try {
spyWarn().mockRestore(); expect(console.warn).not.toHaveBeenCalled();
} finally {
spyWarn().mockRestore();
}
}); });
const example = () => { const example = () => {
const fetcher = new MockFetcher(); const fetcher = new MockFetcher();
@ -423,7 +423,7 @@ describe("plugins/discourse/mirror", () => {
"while adding post http://example.com/t/2/1." "while adding post http://example.com/t/2/1."
); );
expect(console.warn).toHaveBeenCalledTimes(1); expect(console.warn).toHaveBeenCalledTimes(1);
jest.spyOn(console, "warn").mockImplementation(() => {}); spyWarn().mockReset();
}); });
it("warns if it finds a (non-latest) post with no topic", async () => { it("warns if it finds a (non-latest) post with no topic", async () => {
@ -446,7 +446,7 @@ describe("plugins/discourse/mirror", () => {
"while adding post http://example.com/t/2/1." "while adding post http://example.com/t/2/1."
); );
expect(console.warn).toHaveBeenCalledTimes(1); expect(console.warn).toHaveBeenCalledTimes(1);
jest.spyOn(console, "warn").mockImplementation(() => {}); spyWarn().mockReset();
}); });
it("warns if it gets a like that doesn't correspond to any post", async () => { it("warns if it gets a like that doesn't correspond to any post", async () => {
@ -463,7 +463,7 @@ describe("plugins/discourse/mirror", () => {
"on a like by credbot on post id 37." "on a like by credbot on post id 37."
); );
expect(console.warn).toHaveBeenCalledTimes(1); expect(console.warn).toHaveBeenCalledTimes(1);
jest.spyOn(console, "warn").mockImplementation(() => {}); spyWarn().mockReset();
}); });
}); });
@ -482,7 +482,7 @@ describe("plugins/discourse/mirror", () => {
"while processing likes for credbot; skipping this user." "while processing likes for credbot; skipping this user."
); );
expect(console.warn).toHaveBeenCalledTimes(1); expect(console.warn).toHaveBeenCalledTimes(1);
jest.spyOn(console, "warn").mockImplementation(() => {}); spyWarn().mockReset();
}); });
it("sends the right tasks to the TaskReporter", async () => { it("sends the right tasks to the TaskReporter", async () => {