Skip to content

Commit

Permalink
Add support for logs to transform-fixture-test-runner
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Aug 12, 2019
1 parent ea5191c commit e700f31
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
9 changes: 9 additions & 0 deletions packages/babel-helper-fixtures/src/index.js
Expand Up @@ -141,11 +141,14 @@ export default function get(entryLoc): Array<Suite> {
const taskOptsLoc = resolve(taskDir + "/options");
if (taskOptsLoc) extend(taskOpts, require(taskOptsLoc));

const taskLogLoc = resolve(taskDir + "/expected-log.txt");

const test = {
optionsDir: taskOptsLoc ? path.dirname(taskOptsLoc) : null,
title: humanize(taskName, true),
disabled: taskName[0] === ".",
options: taskOpts,
expectedLog: taskLogLoc ? readFile(taskLogLoc) : null,
exec: {
loc: execLoc,
code: readFile(execLoc),
Expand Down Expand Up @@ -222,6 +225,12 @@ export default function get(entryLoc): Array<Suite> {
);
}
}

if (test.exec.code && test.expectedLog) {
throw new Error(
"Test cannot have logs and also be executed: " + taskLogLoc,
);
}
}
}

Expand Down
26 changes: 21 additions & 5 deletions packages/babel-helper-transform-fixture-test-runner/src/index.js
Expand Up @@ -131,11 +131,14 @@ function wrapPackagesArray(type, names, optionsDir) {
}

function run(task) {
const actual = task.actual;
const expected = task.expect;
const exec = task.exec;
const opts = task.options;
const optionsDir = task.optionsDir;
const {
actual,
expect: expected,
exec,
options: opts,
optionsDir,
expectedLog,
} = task;

function getOpts(self) {
const newOpts = merge(
Expand Down Expand Up @@ -194,12 +197,25 @@ function run(task) {
let actualCode = actual.code;
const expectCode = expected.code;
if (!execCode || actualCode) {
let actualLogs = "";
if (expectedLog !== null) {
jest.spyOn(console, "log").mockImplementation(msg => {
actualLogs += `>>>>>> [console.log] <<<<<<\n${msg}\n\n`;
});
jest.spyOn(console, "warn").mockImplementation(msg => {
actualLogs += `>>>>>> [console.warn] <<<<<<\n${msg}\n\n`;
});
}

result = babel.transform(actualCode, getOpts(actual));

const expectedCode = result.code.replace(
escapeRegExp(path.resolve(__dirname, "../../../")),
"<CWD>",
);

if (expectedLog !== null) expect(actualLogs.trim()).toBe(expectedLog);

checkDuplicatedNodes(babel, result.ast);
if (
!expected.code &&
Expand Down

0 comments on commit e700f31

Please sign in to comment.