Skip to content

Commit

Permalink
feat: add typescript file extension support (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
infctr authored and Kent C. Dodds committed Aug 5, 2018
1 parent 4b512e8 commit c8e49be
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/__tests__/fixtures/fixtures/typescript/code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'use strict';
5 changes: 3 additions & 2 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,12 @@ test('can pass tests in fixtures relative to the filename', async () => {
}),
)
expect(describeSpy).toHaveBeenCalledTimes(2)
expect(itSpy).toHaveBeenCalledTimes(5)
expect(itSpy).toHaveBeenCalledTimes(6)
expect(itSpy.mock.calls).toEqual([
[`changed`, expect.any(Function)],
[`nested a`, expect.any(Function)],
[`nested b`, expect.any(Function)],
[`typescript`, expect.any(Function)],
[`unchanged`, expect.any(Function)],
[`without output file`, expect.any(Function)],
])
Expand Down Expand Up @@ -291,7 +292,7 @@ test('creates output file for new tests', async () => {
)

expect(writeFileSyncSpy.mock.calls[0]).toEqual([
expect.stringMatching(/\/output\.js$/),
expect.stringMatching(/\/output\.(j|t)s$/),
"'use strict';",
])
})
Expand Down
13 changes: 9 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,21 @@ function pluginTester({
const createFixtureTests = (fixturesDir, options) => {
fs.readdirSync(fixturesDir).forEach(caseName => {
const fixtureDir = path.join(fixturesDir, caseName)
const codePath = path.join(fixtureDir, 'code.js')
const jsCodePath = path.join(fixtureDir, 'code.js')
const tsCodePath = path.join(fixtureDir, 'code.ts')
const blockTitle = caseName.split('-').join(' ')
const codePath =
(pathExists.sync(jsCodePath) && jsCodePath) ||
(pathExists.sync(tsCodePath) && tsCodePath)

if (!pathExists.sync(codePath)) {
if (!codePath) {
describe(blockTitle, () => {
createFixtureTests(fixtureDir, options)
})
return
}

const ext = /\.ts$/.test(codePath) ? '.ts' : '.js'
it(blockTitle, () => {
const {plugin, pluginOptions, fixtureOutputName, babel, ...rest} = options

Expand All @@ -239,7 +244,7 @@ const createFixtureTests = (fixturesDir, options) => {
)
const actual = babel.transformFileSync(codePath, babelOptions).code.trim()

const outputPath = path.join(fixtureDir, `${fixtureOutputName}.js`)
const outputPath = path.join(fixtureDir, `${fixtureOutputName}${ext}`)

if (!fs.existsSync(outputPath)) {
fs.writeFileSync(outputPath, actual)
Expand All @@ -251,7 +256,7 @@ const createFixtureTests = (fixturesDir, options) => {
assert.equal(
actual,
output,
`actual output does not match ${fixtureOutputName}.js`,
`actual output does not match ${fixtureOutputName}${ext}`,
)
})
})
Expand Down

0 comments on commit c8e49be

Please sign in to comment.