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

[Test Recorder] Fix browser custom console logging when recording #10018

Merged
merged 3 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion sdk/test-utils/recorder/src/customConsoleLog.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

import { isBrowser } from "./utils";

// Converting content corresponding to all the console statements
// into (JSON.stringify)-ed content in record mode for browser tests.
//
Expand All @@ -24,12 +26,21 @@
// - Example - console.warn("hello"); -> console.log({ warn: "hello" });
// - Example - console.log("hello"); -> console.log({ log: "hello" });

export let consoleLog: (msg: any, ...args: any[]) => void;

if (isBrowser()) {
consoleLog = window.console.log;
}

export function setConsoleLogForTesting(func: (msg: any, ...args: any[]) => void) {
consoleLog = func;
}

/**
* Converts content corresponding to all the console statements into (JSON.stringify)-ed content in record mode for browser tests.
* This allows filtering certain console.logs to generate the recordings for browser tests.
*/
export function customConsoleLog() {
const consoleLog = window.console.log;
for (const method in window.console) {
if (
window.console.hasOwnProperty(method) &&
Expand Down
35 changes: 24 additions & 11 deletions sdk/test-utils/recorder/test/browser/recorder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { record, TestContextInterface, TestContext, TestContextTest } from "../.
import xhrMock from "xhr-mock";
import MD5 from "md5";
import chai from "chai";
import { consoleLog, setConsoleLogForTesting } from "../../src/customConsoleLog";
const { expect } = chai;

const expectedHttpResponse = "Hello World!";
Expand Down Expand Up @@ -69,11 +70,17 @@ describe("The recorder's public API, on a browser", () => {

// The recorder outputs files into the console,
// so we need to mock the console.log function to capture and test the recorder output.
const originalConsoleLog = console.log;
const originalConsoleLog = consoleLog;
const savedConsoleLogParams: any[] = [];
console.log = (...params: any[]) => {
savedConsoleLogParams.push(params);
};
setConsoleLogForTesting((...params: any[]) => {
if (params && params.length > 0) {
try {
if (JSON.parse(params[0]).writeFile) {
savedConsoleLogParams.push(params);
}
} catch (err) {}
}
});

// The recorder should start in the beforeEach call.
// We have to do this to emulate that.
Expand All @@ -97,7 +104,7 @@ describe("The recorder's public API, on a browser", () => {
// Cleaning everything before we continue verifying the results.
xhrMock.teardown();
recorder.stop();
console.log = originalConsoleLog;
setConsoleLogForTesting(originalConsoleLog);

// Here we confirm that the recorder generated an expected output on the console.logs.
// This output is used to generate the recording files in the filesystem, though here we're only
Expand Down Expand Up @@ -203,12 +210,18 @@ describe("The recorder's public API, on a browser", () => {
const originalXHR = XMLHttpRequest;

// The recorder outputs files into the console,
// so we need to mock the console.log function to capture and test the recorder output.
const originalConsoleLog = console.log;
// so we need to override the consoleLog function to capture and test the recorder output.
const originalConsoleLog = consoleLog;
const savedConsoleLogParams: any[] = [];
console.log = (...params: any) => {
savedConsoleLogParams.push(params);
};
setConsoleLogForTesting((...params: any[]) => {
if (params && params.length > 0) {
try {
if (JSON.parse(params[0]).writeFile) {
savedConsoleLogParams.push(params);
}
} catch (err) {}
}
});

// The recorder should start in the beforeEach call.
// To emulate that behavior while keeping the test code as contained as possible,
Expand All @@ -234,7 +247,7 @@ describe("The recorder's public API, on a browser", () => {
// Cleaning everything before we continue verifying the results.
xhrMock.teardown();
recorder.stop();
console.log = originalConsoleLog;
setConsoleLogForTesting(originalConsoleLog);

// Now we check the hash has changed in the recorded console.log output.

Expand Down