Skip to content

Commit 969be11

Browse files
authored
feat: use babel.transformAsync when it's available (#84)
1 parent c3f8ef7 commit 969be11

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

Diff for: src/__tests__/plugin-tester.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ beforeEach(() => {
5252
global.it.skip = jest.fn(titleTesterMock)
5353
itOnlySpy = global.it.only
5454
itSkipSpy = global.it.skip
55-
transformSpy = jest.spyOn(babel, 'transform')
55+
transformSpy = jest.spyOn(babel, 'transformAsync')
5656
writeFileSyncSpy = jest
5757
.spyOn(fs, 'writeFileSync')
5858
.mockImplementation(() => {})
@@ -369,6 +369,24 @@ test('can provide a test filename for code strings', async () => {
369369
)
370370
})
371371

372+
test('works with versions of babel without `.transformSync` method', async () => {
373+
const tests = [simpleTest]
374+
const oldBabel = {
375+
transform: babel.transform,
376+
}
377+
const transformSyncSpy = jest.spyOn(oldBabel, 'transform')
378+
await runPluginTester(
379+
getOptions({
380+
babel: oldBabel,
381+
filename: __filename,
382+
fixtures: 'fixtures/fixtures',
383+
tests,
384+
}),
385+
)
386+
expect(transformSpy).not.toHaveBeenCalled()
387+
expect(transformSyncSpy).toHaveBeenCalledTimes(15)
388+
})
389+
372390
test('can provide plugin options', async () => {
373391
const tests = [simpleTest]
374392
const pluginOptions = {

Diff for: src/plugin-tester.js

+23-16
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function pluginTester({
118118
teardowns.push(returnedTeardown)
119119
}
120120
try {
121-
tester()
121+
await tester()
122122
} finally {
123123
try {
124124
await Promise.all(teardowns.map(t => t()))
@@ -132,7 +132,7 @@ function pluginTester({
132132
}
133133

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

149-
let result
149+
let result, transformed
150150
let errored = false
151151

152152
try {
153+
if (babel.transformAsync) {
154+
transformed = await babel.transformAsync(code, babelOptions)
155+
} else {
156+
transformed = babel.transform(code, babelOptions)
157+
}
153158
result = formatResult(
154-
fixLineEndings(
155-
babel.transform(code, babelOptions).code,
156-
endOfLine,
157-
code,
158-
),
159+
fixLineEndings(transformed.code, endOfLine, code),
159160
{filename: testFilename},
160161
)
161162
} catch (err) {
@@ -293,7 +294,7 @@ const createFixtureTests = (fixturesDir, options) => {
293294
return
294295
}
295296

296-
it(blockTitle, () => {
297+
it(blockTitle, async () => {
297298
const {
298299
plugin,
299300
pluginOptions,
@@ -335,17 +336,23 @@ const createFixtureTests = (fixturesDir, options) => {
335336
)
336337

337338
const input = fs.readFileSync(codePath).toString()
339+
let transformed, ext
340+
if (babel.transformAsync) {
341+
transformed = await babel.transformAsync(input, {
342+
...babelOptions,
343+
filename: codePath,
344+
})
345+
} else {
346+
transformed = babel.transform(input, {
347+
...babelOptions,
348+
filename: codePath,
349+
})
350+
}
338351
const actual = formatResult(
339-
fixLineEndings(
340-
babel.transformSync(input, {...babelOptions, filename: codePath})
341-
.code,
342-
endOfLine,
343-
input,
344-
),
352+
fixLineEndings(transformed.code, endOfLine, input),
345353
)
346354

347355
const {fixtureOutputExt} = fixturePluginOptions
348-
let ext
349356
if (fixtureOutputExt) {
350357
ext = fixtureOutputExt
351358
} else {

0 commit comments

Comments
 (0)