/* eslint-disable no-console */
jest.setTimeout(25000);
const fs = require("./fs");
const path = require("path");
const babel = require("babel-core");
const parseArgs = require("./argParser");

function testRunner(dir) {
  const pkgDir = path.join(dir, "../");
  const packageJson = JSON.parse(
    fs.readFileSync(path.join(pkgDir, "package.json"))
  );
  const pkgName = packageJson.name;

  const fixturesDir = path.join(pkgDir, "__tests__/fixtures");

  const fixtures = fs
    .readdirSync(fixturesDir)
    .filter(dir => fs.isDirectorySync(path.join(fixturesDir, dir)));

  const flags = parseArgs(process.argv);
  const updateFixtures = Boolean(flags["update-fixtures"]);

  describe(pkgName, () => {
    for (const fixture of fixtures) {
      const actualFile = path.join(fixturesDir, fixture, "actual.js");
      const expectedFile = path.join(fixturesDir, fixture, "expected.js");
      const skipFile = path.join(fixturesDir, fixture, "skip");
      const optionsFile = path.join(fixturesDir, fixture, "options.json");

      if (fs.isFileSync(skipFile)) {
        test.skip(fixture, () => {});
        continue;
      }

      test(fixture, async () => {
        const actual = await fs.readFile(actualFile);

        let options = {};
        if (await fs.isFile(optionsFile)) {
          options = JSON.parse(await fs.readFile(optionsFile));
        }

        const pluginPath = path.join(pkgDir, "src/index.js");

        const actualTransformed = babel.transform(actual, {
          plugins: [[pluginPath, options]]
        }).code;

        if (!await fs.isFile(expectedFile)) {
          await fs.writeFile(expectedFile, actualTransformed);
          console.warn("Created fixture's expected file - " + expectedFile);
        } else if (updateFixtures) {
          const expected = await fs.readFile(expectedFile);
          if (expected !== actualTransformed) {
            await fs.writeFile(expectedFile, actualTransformed);
            console.warn("Updated fixture's expected file - " + expectedFile);
          }
        } else {
          const expected = await fs.readFile(expectedFile);
          expect(actualTransformed).toBe(expected);
        }
      });
    }
  });
}

module.exports = testRunner;

@@ -187,16 +187,430 @@
 ser%22);%0A%0A
+/**%0A * Jest changes __dirname to relative path and the require of relative path%0A * that doesn't start with %22.%22 will be a module require -%0A *%0A * require(%22./packages/babel-plugin...%22) vs require(%22packages/babel-plugin..%22);%0A *%0A * So we start the path with a %22./%22%0A */%0Afunction pathJoin(...parts) %7B%0A  if (parts%5B0%5D.startsWith(%22/%22)) %7B%0A    return path.join(...parts);%0A  %7D%0A  return %22.%22 + path.sep + path.join(...parts);%0A%7D%0A%0A
 function
@@ -645,26 +645,25 @@
 kgDir = path
-.j
+J
 oin(dir, %22..
@@ -667,16 +667,17 @@
 %22../%22);%0A
+%0A
   const 
@@ -722,26 +722,25 @@
 ileSync(path
-.j
+J
 oin(pkgDir, 
@@ -820,26 +820,25 @@
 esDir = path
-.j
+J
 oin(pkgDir, 
@@ -951,26 +951,25 @@
 orySync(path
-.j
+J
 oin(fixtures
@@ -1171,34 +1171,33 @@
 ctualFile = path
-.j
+J
 oin(fixturesDir,
@@ -1243,34 +1243,33 @@
 ectedFile = path
-.j
+J
 oin(fixturesDir,
@@ -1313,34 +1313,33 @@
  skipFile = path
-.j
+J
 oin(fixturesDir,
@@ -1383,26 +1383,25 @@
 sFile = path
-.j
+J
 oin(fixtures
@@ -1430,16 +1430,93 @@
 .json%22);
+%0A      const babelOptionsFile = pathJoin(fixturesDir, fixture, %22babel.json%22);
 %0A%0A      
@@ -1857,33 +1857,194 @@
     
-const p
+let babelOpts = %7B%7D;%0A        if (await fs.isFile(babelOptionsFile)) %7B%0A          babelOpts = JSON.parse(await fs.readFile(babelOptionsFile));%0A        %7D%0A%0A        const currentP
 lugin
-Path
  = path
-.j
+J
 oin(
@@ -2081,57 +2081,142 @@
     
-const actualTransformed = babel.transform(actual,
+if (Array.isArray(babelOpts.plugins)) %7B%0A          babelOpts.plugins = %5B%5BcurrentPlugin, options%5D, ...babelOpts.plugins%5D;%0A        %7D else
  %7B%0A 
@@ -2228,29 +2228,43 @@
     
+babelOpts.
 plugins
-: %5B%5Bp
+ = %5B%5BcurrentP
 lugin
-Path
 , op
@@ -2270,16 +2270,17 @@
 ptions%5D%5D
+;
 %0A       
@@ -2281,16 +2281,85 @@
        %7D
+%0A%0A        const actualTransformed = babel.transform(actual, babelOpts
 ).code;%0A

add babel.json for fixture test-runner to override babel opts