Skip to content

Commit

Permalink
fix: update all deps and fix cosmiconfig usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Nov 15, 2019
1 parent e7ea8eb commit 1fcdc4f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ dist
.opt-in
.opt-out
package-lock.json
.eslintcache
yarn.lock
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@
"author": "Kent C. Dodds <kent@doddsfamily.us> (http://kentcdodds.com/)",
"license": "MIT",
"dependencies": {
"@babel/runtime": "^7.4.2",
"cosmiconfig": "^5.2.0",
"resolve": "^1.10.0"
"@babel/runtime": "^7.7.2",
"cosmiconfig": "^6.0.0",
"resolve": "^1.12.0"
},
"devDependencies": {
"@babel/core": "^7.4.0",
"@babel/parser": "^7.4.2",
"@babel/types": "^7.4.0",
"@babel/core": "^7.7.2",
"@babel/parser": "^7.7.3",
"@babel/types": "^7.7.2",
"ast-pretty-print": "^2.0.1",
"babel-plugin-tester": "^6.0.1",
"babel-plugin-tester": "^7.0.4",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
"cpy": "^7.1.0",
"kcd-scripts": "^1.1.3"
"cpy": "^7.3.0",
"kcd-scripts": "^1.11.0"
},
"eslintConfig": {
"extends": "./node_modules/kcd-scripts/eslint.js"
Expand Down
28 changes: 19 additions & 9 deletions src/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const Div = STYLED.div\`
↓ ↓ ↓ ↓ ↓ ↓
const red = "background-color: red;";
const Div = STYLED.div\`undefined\`;
const Div = STYLED.div\`composes: background-color: red;
color: blue;\`;
`;

Expand All @@ -49,7 +50,7 @@ errorThrower('hi')
↓ ↓ ↓ ↓ ↓ ↓
Error: not helpful Learn more: https://www.npmjs.com/package/babel-plugin-macros-test-error-thrower
Error: babel-plugin-macros-test-error-thrower/macro: not helpful Learn more: https://www.npmjs.com/package/babel-plugin-macros-test-error-thrower
`;

Expand All @@ -60,7 +61,7 @@ errorThrower('hi')
↓ ↓ ↓ ↓ ↓ ↓
Error: not helpful Learn more: https://www.npmjs.com/package/babel-plugin-macros-test-error-thrower.macro
Error: babel-plugin-macros-test-error-thrower.macro: not helpful Learn more: https://www.npmjs.com/package/babel-plugin-macros-test-error-thrower.macro
`;

Expand Down Expand Up @@ -133,8 +134,15 @@ const red = macro('noop')
↓ ↓ ↓ ↓ ↓ ↓
import macro from './fixtures/keep-imports.macro';
const red = macro('noop');
"use strict";
var _keepImports = require("./fixtures/keep-imports.macro");
var _keepImports2 = _interopRequireDefault(_keepImports);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const red = (0, _keepImports2.default)('noop');
`;

Expand All @@ -145,7 +153,7 @@ errorThrower('hey')
↓ ↓ ↓ ↓ ↓ ↓
Error: very unhelpful
Error: ./fixtures/error-thrower.macro: very unhelpful
`;

Expand Down Expand Up @@ -202,7 +210,7 @@ unwrapped('hey')
↓ ↓ ↓ ↓ ↓ ↓
Error: https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/author.md#writing-a-macro
Error: The macro imported from "./fixtures/non-wrapped.macro" must be wrapped in "createMacro" which you can get from "babel-plugin-macros". Please refer to the documentation to see how to do this properly: https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/author.md#writing-a-macro
`;
Expand Down Expand Up @@ -346,7 +354,8 @@ const Div = STYLED.div\`
↓ ↓ ↓ ↓ ↓ ↓
const red = "background-color: red;";
const Div = STYLED.div\`undefined\`;
const Div = STYLED.div\`composes: background-color: red;
color: blue;\`;
`;
Expand All @@ -365,7 +374,8 @@ const Div = styled.div\`
↓ ↓ ↓ ↓ ↓ ↓
const red = "background-color: red;";
const Div = styled.div\`undefined\`;
const Div = styled.div\`composes: background-color: red;
color: blue;\`;
`;
Expand Down
5 changes: 4 additions & 1 deletion src/__tests__/fixtures/emotion.macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ function emotionMacro({references, babel}) {
if (styledRef.parentPath.parentPath.type === 'TaggedTemplateExpression') {
const quasi = styledRef.parentPath.parentPath.get('quasi')
const val = quasi.evaluate().value.trim()
const replacement = t.templateLiteral([t.templateElement(val)], [])
const replacement = t.templateLiteral(
[t.templateElement({raw: val, cooked: val})],
[],
)
quasi.replaceWith(replacement)
}
})
Expand Down
63 changes: 29 additions & 34 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint no-console:0 */
import path from 'path'
import cosmiconfigMock from 'cosmiconfig'
import {cosmiconfigSync as cosmiconfigSyncMock} from 'cosmiconfig'
import cpy from 'cpy'
import babel from '@babel/core'
import pluginTester from 'babel-plugin-tester'
Expand All @@ -9,28 +8,13 @@ import plugin from '../'
const projectRoot = path.join(__dirname, '../../')

jest.mock('cosmiconfig', () => {
const mockSearchSync = jest.fn()
Object.assign(mockSearchSync, {
mockReset() {
return mockSearchSync.mockImplementation(
(filename, configuredCosmiconfig) =>
configuredCosmiconfig.searchSync(filename),
)
},
})

mockSearchSync.mockReset()

const _cosmiconfigMock = (...args) => ({
searchSync(filename) {
return mockSearchSync(
filename,
require.requireActual('cosmiconfig')(...args),
)
},
})

return Object.assign(_cosmiconfigMock, {mockSearchSync})
const cosmiconfigExports = jest.requireActual('cosmiconfig')
const actualCosmiconfigSync = cosmiconfigExports.cosmiconfigSync
function fakeCosmiconfigSync(...args) {
fakeCosmiconfigSync.explorer = actualCosmiconfigSync(...args)
return fakeCosmiconfigSync.explorer
}
return {...cosmiconfigExports, cosmiconfigSync: fakeCosmiconfigSync}
})

beforeAll(() => {
Expand All @@ -43,10 +27,13 @@ beforeAll(() => {
})
})

beforeEach(() => {
jest.spyOn(console, 'error').mockImplementation(() => {})
})

afterEach(() => {
// eslint-disable-next-line
require('babel-plugin-macros-test-fake/macro').innerFn.mockClear()
cosmiconfigMock.mockSearchSync.mockReset()
console.error.mockRestore()
jest.clearAllMocks()
})

expect.addSnapshotSerializer({
Expand Down Expand Up @@ -308,18 +295,20 @@ pluginTester({
error: true,
fixture: path.join(__dirname, 'fixtures/config/code.js'),
setup() {
cosmiconfigMock.mockSearchSync.mockImplementationOnce(() => {
throw new Error('this is a cosmiconfig error')
})
const originalError = console.error
console.error = jest.fn()
jest
.spyOn(cosmiconfigSyncMock.explorer, 'search')
.mockImplementationOnce(() => {
throw new Error('this is a cosmiconfig error')
})
jest.spyOn(console, 'error').mockImplementationOnce(() => {})
return function teardown() {
try {
expect(console.error).toHaveBeenCalledTimes(1)
expect(console.error.mock.calls[0]).toMatchSnapshot()
console.error = originalError
console.error.mockClear()
} catch (e) {
console.error(e)
console.error.mockClear()
throw e
}
}
Expand All @@ -329,7 +318,11 @@ pluginTester({
title: 'when there is no config to load, then no config is passed',
fixture: path.join(__dirname, 'fixtures/config/code.js'),
setup() {
cosmiconfigMock.mockSearchSync.mockImplementationOnce(() => null)
jest
.spyOn(cosmiconfigSyncMock.explorer, 'search')
.mockImplementationOnce(() => {
return null
})
return function teardown() {
try {
const configurableMacro = require('./fixtures/config/configurable.macro')
Expand Down Expand Up @@ -450,3 +443,5 @@ pluginTester({
},
],
})

/* eslint no-console:0 */
5 changes: 2 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function getConfigExporer() {
return (_configExplorer =
_configExplorer ||
// Lazy load cosmiconfig since it is a relatively large bundle
require('cosmiconfig')('babel-plugin-macros', {
require('cosmiconfig').cosmiconfigSync('babel-plugin-macros', {
searchPlaces: [
'package.json',
'.babel-plugin-macrosrc',
Expand All @@ -34,7 +34,6 @@ function getConfigExporer() {
'babel-plugin-macros.config.js',
],
packageProp: 'babelMacros',
sync: true,
}))
}

Expand Down Expand Up @@ -253,7 +252,7 @@ function applyMacros({

function getConfigFromFile(configName, filename) {
try {
const loaded = getConfigExporer().searchSync(filename)
const loaded = getConfigExporer().search(filename)

if (loaded) {
return {
Expand Down

0 comments on commit 1fcdc4f

Please sign in to comment.