From 481be191651b3b5d4a0fd550146a76a6e1e819c4 Mon Sep 17 00:00:00 2001 From: Bernard Date: Sun, 9 Oct 2022 07:48:47 -0500 Subject: [PATCH] fix(src): fix fixtureOutputExt being ignored in root options.json (#89) --- .../__snapshots__/plugin-tester.js.snap | 2 +- .../fixtures/fixtureOutputExt/fixture/code.ts | 1 + .../fixtureOutputExt/fixture/options.json | 1 + .../fixtureOutputExt/fixture/output.js | 1 + .../root/fixture/code.ts | 1 + .../root/fixture/output.js | 1 + .../root-fixtureOutputExt/root/options.json | 1 + src/__tests__/plugin-tester.js | 36 ++++++++++++++++++- src/plugin-tester.js | 30 +++++++--------- 9 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 src/__tests__/fixtures/fixtureOutputExt/fixture/code.ts create mode 100644 src/__tests__/fixtures/fixtureOutputExt/fixture/options.json create mode 100644 src/__tests__/fixtures/fixtureOutputExt/fixture/output.js create mode 100644 src/__tests__/fixtures/root-fixtureOutputExt/root/fixture/code.ts create mode 100644 src/__tests__/fixtures/root-fixtureOutputExt/root/fixture/output.js create mode 100644 src/__tests__/fixtures/root-fixtureOutputExt/root/options.json diff --git a/src/__tests__/__snapshots__/plugin-tester.js.snap b/src/__tests__/__snapshots__/plugin-tester.js.snap index 419da15..ed46df8 100644 --- a/src/__tests__/__snapshots__/plugin-tester.js.snap +++ b/src/__tests__/__snapshots__/plugin-tester.js.snap @@ -32,6 +32,6 @@ exports[`throws error if fixture provided and code changes 1`] = `Expected outpu exports[`throws error when error expected but no error thrown 1`] = `Expected to throw error, but it did not.`; -exports[`throws error when function doesn't return true 1`] = `[BABEL] unknown: test message (While processing: "base$0")`; +exports[`throws error when function doesn't return true 1`] = `[BABEL] unknown file: test message (While processing: "base$0")`; exports[`throws if output is incorrect 1`] = `Output is incorrect.`; diff --git a/src/__tests__/fixtures/fixtureOutputExt/fixture/code.ts b/src/__tests__/fixtures/fixtureOutputExt/fixture/code.ts new file mode 100644 index 0000000..ad9a93a --- /dev/null +++ b/src/__tests__/fixtures/fixtureOutputExt/fixture/code.ts @@ -0,0 +1 @@ +'use strict'; diff --git a/src/__tests__/fixtures/fixtureOutputExt/fixture/options.json b/src/__tests__/fixtures/fixtureOutputExt/fixture/options.json new file mode 100644 index 0000000..795c9ee --- /dev/null +++ b/src/__tests__/fixtures/fixtureOutputExt/fixture/options.json @@ -0,0 +1 @@ +{"fixtureOutputExt": ".js"} \ No newline at end of file diff --git a/src/__tests__/fixtures/fixtureOutputExt/fixture/output.js b/src/__tests__/fixtures/fixtureOutputExt/fixture/output.js new file mode 100644 index 0000000..ad9a93a --- /dev/null +++ b/src/__tests__/fixtures/fixtureOutputExt/fixture/output.js @@ -0,0 +1 @@ +'use strict'; diff --git a/src/__tests__/fixtures/root-fixtureOutputExt/root/fixture/code.ts b/src/__tests__/fixtures/root-fixtureOutputExt/root/fixture/code.ts new file mode 100644 index 0000000..ad9a93a --- /dev/null +++ b/src/__tests__/fixtures/root-fixtureOutputExt/root/fixture/code.ts @@ -0,0 +1 @@ +'use strict'; diff --git a/src/__tests__/fixtures/root-fixtureOutputExt/root/fixture/output.js b/src/__tests__/fixtures/root-fixtureOutputExt/root/fixture/output.js new file mode 100644 index 0000000..ad9a93a --- /dev/null +++ b/src/__tests__/fixtures/root-fixtureOutputExt/root/fixture/output.js @@ -0,0 +1 @@ +'use strict'; diff --git a/src/__tests__/fixtures/root-fixtureOutputExt/root/options.json b/src/__tests__/fixtures/root-fixtureOutputExt/root/options.json new file mode 100644 index 0000000..795c9ee --- /dev/null +++ b/src/__tests__/fixtures/root-fixtureOutputExt/root/options.json @@ -0,0 +1 @@ +{"fixtureOutputExt": ".js"} \ No newline at end of file diff --git a/src/__tests__/plugin-tester.js b/src/__tests__/plugin-tester.js index 1b058ac..60cc6d5 100644 --- a/src/__tests__/plugin-tester.js +++ b/src/__tests__/plugin-tester.js @@ -639,7 +639,7 @@ test('prettier formatter supported', async () => { ) }) -test('gets options from options.json files when using fixtures', async () => { +test('gets options from local and root options.json files when using fixtures', async () => { const optionRootFoo = jest.fn() const optionFoo = jest.fn() const optionBar = jest.fn() @@ -673,6 +673,40 @@ test('gets options from options.json files when using fixtures', async () => { expect(optionBar).toHaveBeenCalledTimes(1) }) +test('respects fixtureOutputExt from root options.json file when using fixtures', async () => { + await runPluginTester( + getOptions({ + fixtures: getFixturePath('root-fixtureOutputExt'), + tests: null, + }), + ) + + expect(writeFileSyncSpy).toHaveBeenCalledTimes(0) + + expect(equalSpy).toHaveBeenCalledWith( + getFixtureContents('root-fixtureOutputExt/root/fixture/code.ts'), + getFixtureContents('root-fixtureOutputExt/root/fixture/output.js'), + expect.any(String), + ) +}) + +test('respects fixtureOutputExt from local options.json file when using fixtures', async () => { + await runPluginTester( + getOptions({ + fixtures: getFixturePath('fixtureOutputExt'), + tests: null, + }), + ) + + expect(writeFileSyncSpy).toHaveBeenCalledTimes(0) + + expect(equalSpy).toHaveBeenCalledWith( + getFixtureContents('fixtureOutputExt/fixture/code.ts'), + getFixtureContents('fixtureOutputExt/fixture/output.js'), + expect.any(String), + ) +}) + test('appends to root plugins array', async () => { const optionRootFoo = jest.fn() const optionFoo = jest.fn() diff --git a/src/plugin-tester.js b/src/plugin-tester.js index c384116..c1d2a55 100644 --- a/src/plugin-tester.js +++ b/src/plugin-tester.js @@ -275,20 +275,22 @@ const createFixtureTests = (fixturesDir, options) => { (fs.existsSync(tsCodePath) && tsCodePath) || (fs.existsSync(jsxCodePath) && jsxCodePath) || (fs.existsSync(tsxCodePath) && tsxCodePath) - let fixturePluginOptions = {} + let localFixtureOptions = {} if (fs.existsSync(optionsPath)) { - fixturePluginOptions = require(optionsPath) + localFixtureOptions = require(optionsPath) + } + + const mergedFixtureAndPluginOptions = { + ...rootFixtureOptions, + ...options.pluginOptions, + ...localFixtureOptions, } if (!codePath) { describe(blockTitle, () => { createFixtureTests(fixtureDir, { ...options, - pluginOptions: { - ...rootFixtureOptions, - ...options.pluginOptions, - ...fixturePluginOptions, - }, + pluginOptions: mergedFixtureAndPluginOptions, }) }) return @@ -316,16 +318,7 @@ const createFixtureTests = (fixturesDir, options) => { fullDefaultConfig, { babelOptions: { - plugins: [ - [ - plugin, - { - ...rootFixtureOptions, - ...pluginOptions, - ...fixturePluginOptions, - }, - ], - ], + plugins: [[plugin, mergedFixtureAndPluginOptions]], // if they have a babelrc, then we'll let them use that // otherwise, we'll just use our simple config babelrc: hasBabelrc, @@ -352,7 +345,8 @@ const createFixtureTests = (fixturesDir, options) => { fixLineEndings(transformed.code, endOfLine, input), ) - const {fixtureOutputExt} = fixturePluginOptions + const {fixtureOutputExt} = mergedFixtureAndPluginOptions + if (fixtureOutputExt) { ext = fixtureOutputExt } else {