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

Add runFixtureTestsWithoutExactASTMatch in parser test runner #13227

Merged
merged 2 commits into from Apr 29, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/babel-parser/test/estree-throws.js
@@ -1,9 +1,13 @@
import path from "path";
import { runThrowTestsWithEstree } from "./helpers/runFixtureTests";
import { runFixtureTestsWithoutExactASTMatch } from "./helpers/runFixtureTests";
import { parse } from "../lib";
import { fileURLToPath } from "url";

runThrowTestsWithEstree(
runFixtureTestsWithoutExactASTMatch(
path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures"),
parse,
(input, options = {}) => {
options.plugins = options.plugins || [];
options.plugins.push("estree");
return parse(input, options);
},
);
34 changes: 26 additions & 8 deletions packages/babel-parser/test/helpers/runFixtureTests.js
Expand Up @@ -81,15 +81,26 @@ export function runFixtureTests(fixturesPath, parseFunction) {
});
}

export function runThrowTestsWithEstree(fixturesPath, parseFunction) {
/**
* Run Fixture test without an exact AST match. If the output.json does not contain
* "errors", it asserts the actual output does not have "errors". If the output.json
* have "errors", it asserts the actual output have the same "errors".
*
* This routine is used to test parser options that have impact on the AST shape but
* does not change the syntax
* @param {*} fixturesPath The path to the fixture root
* @param {*} parseFunction The customized parseFunction, different global test options
* should be implemented here
*/
export function runFixtureTestsWithoutExactASTMatch(
fixturesPath,
parseFunction,
) {
const fixtures = getFixtures(fixturesPath);

Object.keys(fixtures).forEach(function (name) {
fixtures[name].forEach(function (testSuite) {
testSuite.tests.forEach(function (task) {
task.options.plugins = task.options.plugins || [];
task.options.plugins.push("estree");

const testFn = task.disabled ? it.skip : it;

testFn(name + "/" + testSuite.title + "/" + task.title, function () {
Expand Down Expand Up @@ -135,7 +146,7 @@ function save(test, ast) {
generated from `getFixtures`
* @param {*} parseFunction A parser with the same interface of `@babel/parser#parse`
* @param {boolean} [compareErrorsOnly=false] Whether we should only compare the "errors"
* of generated ast against the expected AST. Used for `runThrowTestsWithEstree` where an
* of generated ast against the expected AST. Used for `runFixtureTestsWithoutExactASTMatch` where an
* ESTree AST is generated but we want to make sure `@babel/parser` still throws expected
* recoverable errors on given code locations.
* @returns {void}
Expand Down Expand Up @@ -180,13 +191,20 @@ function runTest(test, parseFunction, compareErrorsOnly = false) {
if (ast.comments && !ast.comments.length) delete ast.comments;
if (ast.errors && !ast.errors.length) delete ast.errors;

if (!test.expect.code && !opts.throws && !process.env.CI) {
if (
!test.expect.code &&
!opts.throws &&
!process.env.CI &&
!compareErrorsOnly
) {
test.expect.loc += "on";
return save(test, ast);
}

const shouldOverWrite = process.env.OVERWRITE && !compareErrorsOnly;

if (opts.throws) {
if (process.env.OVERWRITE) {
if (shouldOverWrite) {
const fn = path.dirname(test.expect.loc) + "/options.json";
test.options = test.options || {};
delete test.options.throws;
Expand All @@ -212,7 +230,7 @@ function runTest(test, parseFunction, compareErrorsOnly = false) {
const mis = misMatch(JSON.parse(test.expect.code), ast);

if (mis) {
if (process.env.OVERWRITE) {
if (shouldOverWrite) {
return save(test, ast);
}
throw new Error(mis);
Expand Down