Skip to content

Commit

Permalink
fix(createMacro): force wrapping macros in createMacro
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Sep 8, 2017
1 parent 92dcb96 commit f7c9881
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
6 changes: 5 additions & 1 deletion __mocks__/fake/macro.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
// this is used to make sure that you can require macro from node_modules
module.exports = jest.fn()
const {createMacro} = require('../../src')

const innerFn = jest.fn()
module.exports = createMacro(innerFn)
module.exports.innerFn = innerFn
11 changes: 11 additions & 0 deletions src/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ fakeMacro('hi');
`;
exports[`throws an error if the macro is not properly wrapped 1`] = `
import unwrapped from './fixtures/non-wrapped.macro'
unwrapped('hey')
↓ ↓ ↓ ↓ ↓ ↓
Error: <PROJECT_ROOT>/src/__tests__/index.js: The macro imported from "./fixtures/non-wrapped.macro" must be wrapped in "createMacro" which you can get from "babel-macros". Please refer to the documentation to see how to do this properly: https://github.com/kentcdodds/babel-macros/blob/master/other/docs/author.md#writing-a-macro
`;
exports[`throws error if it is not transpiled 1`] = `The macro you imported from "untranspiled.macro" is being executed outside the context of compilation with babel-macros. This indicates that you don't have the babel plugin "babel-macros" configured correctly. Please see the documentation for how to configure babel-macros properly: https://github.com/kentcdodds/babel-macros/blob/master/other/docs/user.md`;
exports[`works with function calls 1`] = `
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/fixtures/non-wrapped.macro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => {}
16 changes: 12 additions & 4 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import pluginTester from 'babel-plugin-tester'
import plugin from '../'

afterEach(() => {
fakeMacro.mockClear()
fakeMacro.innerFn.mockClear()
})

const projectRoot = path.join(__dirname, '../../')
Expand Down Expand Up @@ -93,16 +93,24 @@ pluginTester({
teardown() {
// kinda abusing the babel-plugin-tester API here
// to make an extra assertion
expect(fakeMacro).toHaveBeenCalledTimes(1)
expect(fakeMacro).toHaveBeenCalledWith({
expect(fakeMacro.innerFn).toHaveBeenCalledTimes(1)
expect(fakeMacro.innerFn).toHaveBeenCalledWith({
references: expect.any(Object),
state: expect.any(Object),
babel: expect.any(Object),
isBabelMacrosCall: true,
})
expect(fakeMacro.mock.calls[0].babel).toBe(babel)
expect(fakeMacro.innerFn.mock.calls[0].babel).toBe(babel)
},
},
{
title: 'throws an error if the macro is not properly wrapped',
error: true,
code: `
import unwrapped from './fixtures/non-wrapped.macro'
unwrapped('hey')
`,
},
{
title: 'forwards MacroErrors thrown by the macro',
error: true,
Expand Down
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ function applyMacros({path, imports, source, state, babel}) {
}
// eslint-disable-next-line import/no-dynamic-require
const macro = require(requirePath)
if (macro.name !== 'macroWrapper') {
throw new Error(
// eslint-disable-next-line prefer-template
`The macro imported from "${source}" must be wrapped in "createMacro" ` +
`which you can get from "babel-macros". ` +
`Please refer to the documentation to see how to do this properly: https://github.com/kentcdodds/babel-macros/blob/master/other/docs/author.md#writing-a-macro`,
)
}
try {
macro({
references: referencePathsByImportName,
Expand Down

0 comments on commit f7c9881

Please sign in to comment.