Skip to content

Commit

Permalink
feat: use babel.transformAsync when it's available (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
moon-stripe authored May 29, 2021
1 parent c3f8ef7 commit 969be11
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
20 changes: 19 additions & 1 deletion src/__tests__/plugin-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ beforeEach(() => {
global.it.skip = jest.fn(titleTesterMock)
itOnlySpy = global.it.only
itSkipSpy = global.it.skip
transformSpy = jest.spyOn(babel, 'transform')
transformSpy = jest.spyOn(babel, 'transformAsync')
writeFileSyncSpy = jest
.spyOn(fs, 'writeFileSync')
.mockImplementation(() => {})
Expand Down Expand Up @@ -369,6 +369,24 @@ test('can provide a test filename for code strings', async () => {
)
})

test('works with versions of babel without `.transformSync` method', async () => {
const tests = [simpleTest]
const oldBabel = {
transform: babel.transform,
}
const transformSyncSpy = jest.spyOn(oldBabel, 'transform')
await runPluginTester(
getOptions({
babel: oldBabel,
filename: __filename,
fixtures: 'fixtures/fixtures',
tests,
}),
)
expect(transformSpy).not.toHaveBeenCalled()
expect(transformSyncSpy).toHaveBeenCalledTimes(15)
})

test('can provide plugin options', async () => {
const tests = [simpleTest]
const pluginOptions = {
Expand Down
39 changes: 23 additions & 16 deletions src/plugin-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function pluginTester({
teardowns.push(returnedTeardown)
}
try {
tester()
await tester()
} finally {
try {
await Promise.all(teardowns.map(t => t()))
Expand All @@ -132,7 +132,7 @@ function pluginTester({
}

// eslint-disable-next-line complexity
function tester() {
async function tester() {
assert(
code,
'A string or object with a `code` or `fixture` property must be provided',
Expand All @@ -146,16 +146,17 @@ function pluginTester({
'`output` cannot be provided with `snapshot: true`',
)

let result
let result, transformed
let errored = false

try {
if (babel.transformAsync) {
transformed = await babel.transformAsync(code, babelOptions)
} else {
transformed = babel.transform(code, babelOptions)
}
result = formatResult(
fixLineEndings(
babel.transform(code, babelOptions).code,
endOfLine,
code,
),
fixLineEndings(transformed.code, endOfLine, code),
{filename: testFilename},
)
} catch (err) {
Expand Down Expand Up @@ -293,7 +294,7 @@ const createFixtureTests = (fixturesDir, options) => {
return
}

it(blockTitle, () => {
it(blockTitle, async () => {
const {
plugin,
pluginOptions,
Expand Down Expand Up @@ -335,17 +336,23 @@ const createFixtureTests = (fixturesDir, options) => {
)

const input = fs.readFileSync(codePath).toString()
let transformed, ext
if (babel.transformAsync) {
transformed = await babel.transformAsync(input, {
...babelOptions,
filename: codePath,
})
} else {
transformed = babel.transform(input, {
...babelOptions,
filename: codePath,
})
}
const actual = formatResult(
fixLineEndings(
babel.transformSync(input, {...babelOptions, filename: codePath})
.code,
endOfLine,
input,
),
fixLineEndings(transformed.code, endOfLine, input),
)

const {fixtureOutputExt} = fixturePluginOptions
let ext
if (fixtureOutputExt) {
ext = fixtureOutputExt
} else {
Expand Down

0 comments on commit 969be11

Please sign in to comment.