Add runFixtureTestsWithoutExactASTMatch in parser test runner (#13227)
This commit is contained in:
parent
a2ac2f39b5
commit
0f2a1a8153
@ -1,9 +1,13 @@
|
|||||||
import path from "path";
|
import path from "path";
|
||||||
import { runThrowTestsWithEstree } from "./helpers/runFixtureTests";
|
import { runFixtureTestsWithoutExactASTMatch } from "./helpers/runFixtureTests";
|
||||||
import { parse } from "../lib";
|
import { parse } from "../lib";
|
||||||
import { fileURLToPath } from "url";
|
import { fileURLToPath } from "url";
|
||||||
|
|
||||||
runThrowTestsWithEstree(
|
runFixtureTestsWithoutExactASTMatch(
|
||||||
path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures"),
|
path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures"),
|
||||||
parse,
|
(input, options = {}) => {
|
||||||
|
options.plugins = options.plugins || [];
|
||||||
|
options.plugins.push("estree");
|
||||||
|
return parse(input, options);
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@ -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);
|
const fixtures = getFixtures(fixturesPath);
|
||||||
|
|
||||||
Object.keys(fixtures).forEach(function (name) {
|
Object.keys(fixtures).forEach(function (name) {
|
||||||
fixtures[name].forEach(function (testSuite) {
|
fixtures[name].forEach(function (testSuite) {
|
||||||
testSuite.tests.forEach(function (task) {
|
testSuite.tests.forEach(function (task) {
|
||||||
task.options.plugins = task.options.plugins || [];
|
|
||||||
task.options.plugins.push("estree");
|
|
||||||
|
|
||||||
const testFn = task.disabled ? it.skip : it;
|
const testFn = task.disabled ? it.skip : it;
|
||||||
|
|
||||||
testFn(name + "/" + testSuite.title + "/" + task.title, function () {
|
testFn(name + "/" + testSuite.title + "/" + task.title, function () {
|
||||||
@ -135,7 +146,7 @@ function save(test, ast) {
|
|||||||
generated from `getFixtures`
|
generated from `getFixtures`
|
||||||
* @param {*} parseFunction A parser with the same interface of `@babel/parser#parse`
|
* @param {*} parseFunction A parser with the same interface of `@babel/parser#parse`
|
||||||
* @param {boolean} [compareErrorsOnly=false] Whether we should only compare the "errors"
|
* @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
|
* ESTree AST is generated but we want to make sure `@babel/parser` still throws expected
|
||||||
* recoverable errors on given code locations.
|
* recoverable errors on given code locations.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
@ -180,13 +191,20 @@ function runTest(test, parseFunction, compareErrorsOnly = false) {
|
|||||||
if (ast.comments && !ast.comments.length) delete ast.comments;
|
if (ast.comments && !ast.comments.length) delete ast.comments;
|
||||||
if (ast.errors && !ast.errors.length) delete ast.errors;
|
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";
|
test.expect.loc += "on";
|
||||||
return save(test, ast);
|
return save(test, ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const shouldOverWrite = process.env.OVERWRITE && !compareErrorsOnly;
|
||||||
|
|
||||||
if (opts.throws) {
|
if (opts.throws) {
|
||||||
if (process.env.OVERWRITE) {
|
if (shouldOverWrite) {
|
||||||
const fn = path.dirname(test.expect.loc) + "/options.json";
|
const fn = path.dirname(test.expect.loc) + "/options.json";
|
||||||
test.options = test.options || {};
|
test.options = test.options || {};
|
||||||
delete test.options.throws;
|
delete test.options.throws;
|
||||||
@ -212,7 +230,7 @@ function runTest(test, parseFunction, compareErrorsOnly = false) {
|
|||||||
const mis = misMatch(JSON.parse(test.expect.code), ast);
|
const mis = misMatch(JSON.parse(test.expect.code), ast);
|
||||||
|
|
||||||
if (mis) {
|
if (mis) {
|
||||||
if (process.env.OVERWRITE) {
|
if (shouldOverWrite) {
|
||||||
return save(test, ast);
|
return save(test, ast);
|
||||||
}
|
}
|
||||||
throw new Error(mis);
|
throw new Error(mis);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user