Skip to content

Commit

Permalink
add test to check printer idempotence
Browse files Browse the repository at this point in the history
  • Loading branch information
cometkim committed May 13, 2024
1 parent 84ccc5d commit cd480da
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions packages/babel-plugin-relay/__tests__/printGraphQL-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @flow
* @format
*/

'use strict';

const print = require('../printGraphQL');

const { parse } = require('graphql');
const path = require('path');
const fs = require('fs');

type OutputFixture = { name: string, input: string, output: string };
type ErrorFixture = { name: string, input: string, error: string };
type PrinterFixture = OutputFixture | ErrorFixture;

describe('printGraphQL', () => {
const outputFixtures = loadPrinterFixtures()
.filter(fixture => fixture.output)
// object key format doesn't work
.map(fixture => [fixture.name, fixture.input, fixture.output]);

it.each(outputFixtures)('tests printer idempotence: %s', (_name, input, expected) => {
expect(print(parse(input))).toEqual(expected);
});
});

function loadPrinterFixtures(): PrinterFixture[] {
const fixturesPath = path.join(
__dirname,
'../../../compiler/crates/graphql-text-printer/tests/print_ast/fixtures',
);
const fixtures = [];
for (const file of fs.readdirSync(fixturesPath)) {
if (!file.endsWith('.expected')) {
continue;
}
const content = fs.readFileSync(path.join(fixturesPath, file), 'utf8');
try {
const fixture = parsePrintFixture(file, content);
fixtures.push(fixture);
} catch (err) {
console.error(err);
}
}
return fixtures;
}

function parsePrintFixture(name: string, content: string): PrinterFixture {
const successPatttern = /^=+ INPUT =+\n(?<input>[\s\S]*)\n=+ OUTPUT =+\n(?<output>[\s\S]*)$/;
const failurePattern = /^=+ INPUT =+\n(?<input>[\s\S]*)\n=+ ERROR =+\n(?<error>[\s\S]*)$/;

const match = content.match(successPatttern) ?? content.match(failurePattern);
if (!match) {
throw new Error(`Failed to parse ${name}. Unknown fixture format from the graphql-text-printer crate!`);
}
return { name, ...match.groups } as PrinterFixture;
}

0 comments on commit cd480da

Please sign in to comment.