Skip to content

Commit

Permalink
Print Debug Messages for Babel Plugin Test Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
yungsters committed Oct 4, 2015
1 parent 3aca989 commit df00f40
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,48 @@

'use strict';

var path = require('path');
var readFixtures = require('../tools/readFixtures');
var transformGraphQL = require('../tools/transformGraphQL');
const path = require('path');
const readFixtures = require('../tools/readFixtures');
const transformGraphQL = require('../tools/transformGraphQL');

var FIXTURE_PATH = path.resolve(__dirname, '..', '__fixtures__');
var SCHEMA_PATH = path.resolve(__dirname, 'testschema.rfc.json');
const FIXTURE_PATTERN = process.env.FIXTURE;
const FIXTURE_PATH = path.resolve(__dirname, '..', '__fixtures__');
const SCHEMA_PATH = path.resolve(__dirname, 'testschema.rfc.json');

var transform = transformGraphQL.bind(null, SCHEMA_PATH);
const transform = transformGraphQL.bind(null, SCHEMA_PATH);

describe('getBabelRelayPlugin', function() {
var fixtures = readFixtures(FIXTURE_PATH);
const ConsoleErrorQueue = {
print: console.error.bind(console),
queue: [],
clear() {
ConsoleErrorQueue.queue = [];
},
enqueue(...args) {
ConsoleErrorQueue.queue.push(args);
},
flush() {
ConsoleErrorQueue.queue.forEach(args => {
ConsoleErrorQueue.print(...args);
});
ConsoleErrorQueue.clear();
},
};

Object.keys(fixtures).forEach(function(testName) {
var fixture = fixtures[testName];
describe('getBabelRelayPlugin', () => {
const fixtures = readFixtures(FIXTURE_PATH);

// Only print debug errors if test fails.
console.error = ConsoleErrorQueue.enqueue;

Object.keys(fixtures).forEach(testName => {
if (FIXTURE_PATTERN && testName.indexOf(FIXTURE_PATTERN) < 0) {
return;
}
ConsoleErrorQueue.clear();

const fixture = fixtures[testName];
if (fixture.output !== undefined) {
var expected;
let expected;
try {
expected = trimCode(transform(fixture.output, testName));
} catch (e) {
Expand All @@ -34,15 +60,29 @@ describe('getBabelRelayPlugin', function() {
);
}

it('transforms GraphQL RFC for `' + testName + '`', function() {
var actual = trimCode(transform(fixture.input, testName));
expect('\n' + actual + '\n').toBe('\n' + expected + '\n');
it('transforms GraphQL RFC for `' + testName + '`', () => {
const actual = trimCode(transform(fixture.input, testName));
if (actual !== expected) {
ConsoleErrorQueue.flush();
expect('\n' + actual + '\n').toBe('\n' + expected + '\n');
}
});
} else {
it('throws for GraphQL fixture: ' + testName, function() {
expect(function() {
it('throws for GraphQL fixture: ' + testName, () => {
let expected;
try {
transform(fixture.input, testName);
}).toThrow(fixtures.error);
} catch (e) {
expected = e;
}
if (!expected || expected.message !== fixtures.error.message) {
ConsoleErrorQueue.flush();
expect(() => {
if (expected) {
throw expected;
}
}).toThrow(fixtures.error);
}
});
}
});
Expand Down
4 changes: 2 additions & 2 deletions scripts/babel-relay-plugin/src/getBabelRelayPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ function getBabelRelayPlugin(
);

if (options.debug) {
console.log(error.message);
console.log(error.stack);
console.error(error.message);
console.error(error.stack);
}
if (options.abortOnError) {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion scripts/babel-relay-plugin/src/tools/transformGraphQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function transformGraphQL(schemaPath, source, filename) {
var plugin = getBabelRelayPlugin(getSchema(schemaPath), {
abortOnError: false,
suppressWarnings: true,
debug: true
});
return babel.transform(source, {
compact: false,
Expand All @@ -47,7 +48,6 @@ function transformGraphQL(schemaPath, source, filename) {
blacklist: ['strict'],
extra: {
providesModule: 'Fixture',
debug: false,
},
}).code;
}
Expand Down

0 comments on commit df00f40

Please sign in to comment.