From d48a8fc2ea941dddf50dbcedaf136ab7d2618522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Mon, 4 Jun 2018 16:57:03 +0200 Subject: [PATCH] feat: Create automatically fixtures output.js files for new tests (#27) --- package.json | 15 +++++++-- .../fixtures/without-output-file/code.js | 1 + src/__tests__/index.js | 31 +++++++++++++++++-- src/index.js | 9 +++++- 4 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 src/__tests__/fixtures/fixtures/without-output-file/code.js diff --git a/package.json b/package.json index b15cbc8..5ff793d 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,9 @@ "validate": "kcd-scripts validate", "precommit": "kcd-scripts precommit" }, - "files": ["dist"], + "files": [ + "dist" + ], "keywords": [], "author": "Kent C. Dodds (http://kentcdodds.com/)", "license": "MIT", @@ -45,7 +47,12 @@ "max-lines": 0 } }, - "eslintIgnore": ["node_modules", "coverage", "dist", "fixtures"], + "eslintIgnore": [ + "node_modules", + "coverage", + "dist", + "fixtures" + ], "babel": { "presets": [ [ @@ -54,7 +61,9 @@ "targets": { "node": "4.5" }, - "exclude": ["transform-regenerator"] + "exclude": [ + "transform-regenerator" + ] } ] ], diff --git a/src/__tests__/fixtures/fixtures/without-output-file/code.js b/src/__tests__/fixtures/fixtures/without-output-file/code.js new file mode 100644 index 0000000..ad9a93a --- /dev/null +++ b/src/__tests__/fixtures/fixtures/without-output-file/code.js @@ -0,0 +1 @@ +'use strict'; diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 567ed74..100cbb2 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -6,7 +6,14 @@ import * as babel from 'babel-core' import pluginTester from '../' import identifierReversePlugin from './helpers/identifier-reverse-plugin' -let errorSpy, describeSpy, itSpy, itOnlySpy, itSkipSpy, equalSpy, transformSpy +let errorSpy, + describeSpy, + itSpy, + itOnlySpy, + itSkipSpy, + equalSpy, + transformSpy, + writeFileSyncSpy const noop = () => {} const titleTesterMock = (title, testFn) => testFn() @@ -24,6 +31,9 @@ beforeEach(() => { itOnlySpy = global.it.only itSkipSpy = global.it.skip transformSpy = jest.spyOn(babel, 'transform') + writeFileSyncSpy = jest + .spyOn(fs, 'writeFileSync') + .mockImplementation(() => {}) }) afterEach(() => { @@ -33,6 +43,7 @@ afterEach(() => { itSpy.mockRestore() itSkipSpy.mockRestore() transformSpy.mockRestore() + writeFileSyncSpy.mockRestore() }) test('plugin is required', () => { @@ -246,10 +257,11 @@ test('can pass tests in fixtures relative to the filename', async () => { }), ) expect(describeSpy).toHaveBeenCalledTimes(1) - expect(itSpy).toHaveBeenCalledTimes(2) + expect(itSpy).toHaveBeenCalledTimes(3) expect(itSpy.mock.calls).toEqual([ [`changed`, expect.any(Function)], [`unchanged`, expect.any(Function)], + [`without output file`, expect.any(Function)], ]) }) @@ -267,6 +279,21 @@ test('can fail tests in fixtures at an absolute path', async () => { } }) +test('creates output file for new tests', async () => { + await pluginTester( + getOptions({ + filename: __filename, + fixtures: 'fixtures/fixtures', + tests: null, + }), + ) + + expect(writeFileSyncSpy.mock.calls[0]).toEqual([ + expect.stringMatching(/\/output\.js$/), + "'use strict';", + ]) +}) + test('uses the fixture filename in babelOptions', async () => { const fixture = getFixturePath('fixture1.js') const tests = [ diff --git a/src/index.js b/src/index.js index a44a5e2..615f6c2 100644 --- a/src/index.js +++ b/src/index.js @@ -243,8 +243,15 @@ function testFixtures({ .transformFileSync(codePath, babelOptions) .code.trim() + const outputPath = path.join(fixtureDir, `${fixtureOutputName}.js`) + + if (!fs.existsSync(outputPath)) { + fs.writeFileSync(outputPath, actual) + return + } + const output = fs - .readFileSync(path.join(fixtureDir, `${fixtureOutputName}.js`), 'utf8') + .readFileSync(outputPath, 'utf8') .trim() assert.equal(actual, output, 'actual output does not match output.js')