Skip to content

Commit

Permalink
feat: Create automatically fixtures output.js files for new tests (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored and Kent C. Dodds committed Jun 4, 2018
1 parent f3e1ad2 commit d48a8fc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"validate": "kcd-scripts validate",
"precommit": "kcd-scripts precommit"
},
"files": ["dist"],
"files": [
"dist"
],
"keywords": [],
"author": "Kent C. Dodds <kent@doddsfamily.us> (http://kentcdodds.com/)",
"license": "MIT",
Expand Down Expand Up @@ -45,7 +47,12 @@
"max-lines": 0
}
},
"eslintIgnore": ["node_modules", "coverage", "dist", "fixtures"],
"eslintIgnore": [
"node_modules",
"coverage",
"dist",
"fixtures"
],
"babel": {
"presets": [
[
Expand All @@ -54,7 +61,9 @@
"targets": {
"node": "4.5"
},
"exclude": ["transform-regenerator"]
"exclude": [
"transform-regenerator"
]
}
]
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'use strict';
31 changes: 29 additions & 2 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(() => {
Expand All @@ -33,6 +43,7 @@ afterEach(() => {
itSpy.mockRestore()
itSkipSpy.mockRestore()
transformSpy.mockRestore()
writeFileSyncSpy.mockRestore()
})

test('plugin is required', () => {
Expand Down Expand Up @@ -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)],
])
})

Expand All @@ -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 = [
Expand Down
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit d48a8fc

Please sign in to comment.