Skip to content

Commit

Permalink
Group inline snapshot transform results by env (#118)
Browse files Browse the repository at this point in the history
* Group inline snapshot transform results by env

* Prefix env list with BABEL_ENV
  • Loading branch information
billyjanitsch authored Jun 8, 2021
1 parent a401937 commit be57960
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 112 deletions.
45 changes: 12 additions & 33 deletions __tests__/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,13 @@ test('static imports', () => {
console.log(defaultExport, namedExport, namespace)
`

expect(transform({code, env: 'development'})).toMatchInlineSnapshot(`
expect(transform({code})).toMatchInlineSnapshot(`
// BABEL_ENV development, production, esm:
import defaultExport, {namedExport} from 'foo'
import * as namespace from 'bar'
console.log(defaultExport, namedExport, namespace)
`)

expect(transform({code, env: 'production'})).toMatchInlineSnapshot(`
import defaultExport, {namedExport} from 'foo'
import * as namespace from 'bar'
console.log(defaultExport, namedExport, namespace)
`)

expect(transform({code, env: 'esm'})).toMatchInlineSnapshot(`
import defaultExport, {namedExport} from 'foo'
import * as namespace from 'bar'
console.log(defaultExport, namedExport, namespace)
`)
expect(transform({code, env: 'cjs'})).toMatchInlineSnapshot(`
// BABEL_ENV cjs:
'use strict'
var _interopRequireWildcard = require('@babel/runtime/helpers/interopRequireWildcard').default
var _foo = _interopRequireWildcard(require('foo'))
Expand All @@ -39,10 +27,11 @@ test('static imports', () => {
test('dynamic imports', () => {
const code = `import('foo')`

expect(transform({code, env: 'development'})).toMatchInlineSnapshot(`import('foo')`)
expect(transform({code, env: 'production'})).toMatchInlineSnapshot(`import('foo')`)
expect(transform({code, env: 'esm'})).toMatchInlineSnapshot(`import('foo')`)
expect(transform({code, env: 'cjs'})).toMatchInlineSnapshot(`
expect(transform({code})).toMatchInlineSnapshot(`
// BABEL_ENV development, production, esm:
import('foo')
// BABEL_ENV cjs:
'use strict'
var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault').default
var _interopRequireWildcard2 = _interopRequireDefault(
Expand All @@ -61,33 +50,23 @@ test('static exports', () => {
export * as baz from 'other'
`

expect(transform({code, env: 'development'})).toMatchInlineSnapshot(`
expect(transform({code})).toMatchInlineSnapshot(`
// BABEL_ENV development, production:
var foo = 1
export default foo
export {foo}
export {bar} from 'other'
import * as _baz from 'other'
export {_baz as baz}
`)
expect(transform({code, env: 'production'})).toMatchInlineSnapshot(`
var foo = 1
export default foo
export {foo}
export {bar} from 'other'
import * as _baz from 'other'
export {_baz as baz}
`)

expect(transform({code, env: 'esm'})).toMatchInlineSnapshot(`
// BABEL_ENV esm:
const foo = 1
export default foo
export {foo}
export {bar} from 'other'
export * as baz from 'other'
`)
expect(transform({code, env: 'cjs'})).toMatchInlineSnapshot(`
// BABEL_ENV cjs:
'use strict'
var _interopRequireWildcard = require('@babel/runtime/helpers/interopRequireWildcard').default
Object.defineProperty(exports, '__esModule', {value: true})
Expand Down
26 changes: 15 additions & 11 deletions __tests__/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,34 @@ import transform from '../helpers/transform'

test('removes polyfill imports in libraries', () => {
const code = `import 'core-js/stable'`
const envs = ['esm', 'cjs']

expect(transform({code, env: 'esm'})).toMatchInlineSnapshot(``)
expect(transform({code, env: 'cjs'})).toMatchInlineSnapshot(`'use strict'`)
expect(transform({code, envs})).toMatchInlineSnapshot(`
// BABEL_ENV esm:
// BABEL_ENV cjs:
'use strict'
`)
})

test('narrows polyfill imports in apps with modern targets', () => {
const code = `import 'core-js/stable'`
const targets = 'Chrome 90'
const envs = ['development', 'production']

const development = transform({code, targets, env: 'development'})
const production = transform({code, targets, env: 'production'})
expect(development).toBe(production)
expect(development).toMatchInlineSnapshot(`import 'core-js/modules/web.immediate.js'`)
expect(transform({code, targets, envs})).toMatchInlineSnapshot(`
// BABEL_ENV development, production:
import 'core-js/modules/web.immediate.js'
`)
})

test('narrows polyfill imports in apps with legacy targets', () => {
const code = `import 'core-js/stable'`
const targets = 'Chrome 60'
const envs = ['development', 'production']

const development = transform({code, targets, env: 'development'})
const production = transform({code, targets, env: 'production'})
expect(development).toBe(production)

expect(development).toMatchInlineSnapshot(`
expect(transform({code, targets, envs})).toMatchInlineSnapshot(`
// BABEL_ENV development, production:
import 'core-js/modules/es.symbol.description.js'
import 'core-js/modules/es.symbol.async-iterator.js'
import 'core-js/modules/es.array.flat.js'
Expand Down
12 changes: 5 additions & 7 deletions __tests__/react-classic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ test('transpiles JSX using classic runtime', () => {
}
`

expect(transform({code, options, env: 'development'})).toMatchInlineSnapshot(`
expect(transform({code, options})).toMatchInlineSnapshot(`
// BABEL_ENV development:
var _jsxFileName = '/file.js',
_s = $RefreshSig$()
import * as React from 'react'
Expand All @@ -42,9 +43,8 @@ test('transpiles JSX using classic runtime', () => {
_c = MyComponent
var _c
$RefreshReg$(_c, 'MyComponent')
`)
expect(transform({code, options, env: 'production'})).toMatchInlineSnapshot(`
// BABEL_ENV production:
import * as React from 'react'
function MyComponent() {
React.useEffect(function () {}, [])
Expand All @@ -56,9 +56,8 @@ test('transpiles JSX using classic runtime', () => {
' bar'
)
}
`)
expect(transform({code, options, env: 'esm'})).toMatchInlineSnapshot(`
// BABEL_ENV esm:
import * as React from 'react'
function MyComponent() {
React.useEffect(() => {}, [])
Expand All @@ -70,9 +69,8 @@ test('transpiles JSX using classic runtime', () => {
' bar'
)
}
`)
expect(transform({code, options, env: 'cjs'})).toMatchInlineSnapshot(`
// BABEL_ENV cjs:
'use strict'
var _interopRequireWildcard = require('@babel/runtime/helpers/interopRequireWildcard').default
var React = _interopRequireWildcard(require('react'))
Expand Down
12 changes: 5 additions & 7 deletions __tests__/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ test('transpiles JSX', () => {
}
`

expect(transform({code, env: 'development'})).toMatchInlineSnapshot(`
expect(transform({code})).toMatchInlineSnapshot(`
// BABEL_ENV development:
var _jsxFileName = '/file.js',
_s = $RefreshSig$()
import {useState} from 'react'
Expand Down Expand Up @@ -50,9 +51,8 @@ test('transpiles JSX', () => {
_c = MyComponent
var _c
$RefreshReg$(_c, 'MyComponent')
`)
expect(transform({code, env: 'production'})).toMatchInlineSnapshot(`
// BABEL_ENV production:
import {useState} from 'react'
import {jsx as _jsx} from 'react/jsx-runtime'
import {jsxs as _jsxs} from 'react/jsx-runtime'
Expand All @@ -63,9 +63,8 @@ test('transpiles JSX', () => {
Object.assign({height: 2}, rest, {children: [/*#__PURE__*/ _jsx('div', {}), 'foo', ' bar']})
)
}
`)
expect(transform({code, env: 'esm'})).toMatchInlineSnapshot(`
// BABEL_ENV esm:
import {useState} from 'react'
import {jsx as _jsx} from 'react/jsx-runtime'
import {jsxs as _jsxs} from 'react/jsx-runtime'
Expand All @@ -77,9 +76,8 @@ test('transpiles JSX', () => {
children: [/*#__PURE__*/ _jsx('div', {}), 'foo', ' bar'],
})
}
`)
expect(transform({code, env: 'cjs'})).toMatchInlineSnapshot(`
// BABEL_ENV cjs:
'use strict'
var _react = require('react')
var _jsxRuntime = require('react/jsx-runtime')
Expand Down
26 changes: 6 additions & 20 deletions __tests__/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,15 @@ const targets = 'IE 11' // ensures that a helper is required to transpile the re

test('imports external runtime helpers by default', () => {
const options = {targets}
const development = transform({code, options, env: 'development'})
const production = transform({code, options, env: 'production'})
const esm = transform({code, options, env: 'esm'})
const cjs = transform({code, options, env: 'cjs'})

expect(development).toMatchInlineSnapshot(`
expect(transform({code, options})).toMatchInlineSnapshot(`
// BABEL_ENV development, production, esm:
import _objectWithoutPropertiesLoose from '@babel/runtime/helpers/objectWithoutPropertiesLoose'
var _obj = obj,
foo = _obj.foo,
rest = _objectWithoutPropertiesLoose(_obj, ['foo'])
`)

expect(production).toBe(development)
expect(esm).toBe(development)
expect(cjs).toMatchInlineSnapshot(`
// BABEL_ENV cjs:
'use strict'
var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault').default
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(
Expand All @@ -36,12 +29,9 @@ test('imports external runtime helpers by default', () => {

test('injects runtime helpers when `runtime` is disabled', () => {
const options = {targets, runtime: false}
const development = transform({code, options, env: 'development'})
const production = transform({code, options, env: 'production'})
const esm = transform({code, options, env: 'esm'})
const cjs = transform({code, options, env: 'cjs'})

expect(development).toMatchInlineSnapshot(`
expect(transform({code, options})).toMatchInlineSnapshot(`
// BABEL_ENV development, production, esm:
function _objectWithoutPropertiesLoose(source, excluded) {
if (source == null) return {}
var target = {}
Expand All @@ -57,12 +47,8 @@ test('injects runtime helpers when `runtime` is disabled', () => {
var _obj = obj,
foo = _obj.foo,
rest = _objectWithoutPropertiesLoose(_obj, ['foo'])
`)

expect(production).toBe(development)
expect(esm).toBe(development)
expect(cjs).toMatchInlineSnapshot(`
// BABEL_ENV cjs:
'use strict'
function _objectWithoutPropertiesLoose(source, excluded) {
if (source == null) return {}
Expand Down
9 changes: 4 additions & 5 deletions __tests__/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ test('class properties', () => {
}
`

expect(transform({code, env: 'development'})).toMatchInlineSnapshot(`
expect(transform({code})).toMatchInlineSnapshot(`
// BABEL_ENV development, production:
import _classPrivateFieldLooseKey from '@babel/runtime/helpers/classPrivateFieldLooseKey'
var _privateMethod = /*#__PURE__*/ _classPrivateFieldLooseKey('privateMethod')
var Foo = function Foo() {
Expand All @@ -26,19 +27,17 @@ test('class properties', () => {
return 1
}
Foo.bar = 'abc'
`)
expect(transform({code, env: 'esm'})).toMatchInlineSnapshot(`
// BABEL_ENV esm:
class Foo {
static bar = 'abc'
baz = (x, y) => x({...y})
#privateMethod() {
return 1
}
}
`)
expect(transform({code, env: 'cjs'})).toMatchInlineSnapshot(`
// BABEL_ENV cjs:
'use strict'
class Foo {
static bar = 'abc'
Expand Down
22 changes: 12 additions & 10 deletions __tests__/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@ import {test, expect} from '@jest/globals'
import transform from '../helpers/transform'

test('strips type annotations in .ts files', () => {
const filename = 'file.ts'
const code = `var one: number = 1`
const filename = 'file.ts'

expect(transform({code, filename})).toMatchInlineSnapshot(`
// BABEL_ENV development, production, esm:
var one = 1
expect(transform({code, filename, env: 'development'})).toMatchInlineSnapshot(`var one = 1`)
expect(transform({code, filename, env: 'production'})).toMatchInlineSnapshot(`var one = 1`)
expect(transform({code, filename, env: 'esm'})).toMatchInlineSnapshot(`var one = 1`)
expect(transform({code, filename, env: 'cjs'})).toMatchInlineSnapshot(`
// BABEL_ENV cjs:
'use strict'
var one = 1
`)
})

test('strips type annotations in .tsx files', () => {
const filename = 'file.tsx'
const code = `var one: number = 1`
const filename = 'file.tsx'

expect(transform({code, filename})).toMatchInlineSnapshot(`
// BABEL_ENV development, production, esm:
var one = 1
expect(transform({code, filename, env: 'development'})).toMatchInlineSnapshot(`var one = 1`)
expect(transform({code, filename, env: 'production'})).toMatchInlineSnapshot(`var one = 1`)
expect(transform({code, filename, env: 'esm'})).toMatchInlineSnapshot(`var one = 1`)
expect(transform({code, filename, env: 'cjs'})).toMatchInlineSnapshot(`
// BABEL_ENV cjs:
'use strict'
var one = 1
`)
Expand Down
12 changes: 12 additions & 0 deletions helpers/groupBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function groupBy<T, K>(items: T[], iteratee: (item: T) => K): Map<K, T[]> {
const groups = new Map<K, T[]>()

items.forEach((item) => {
const key = iteratee(item)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (groups.has(key)) groups.get(key)!.push(item)
else groups.set(key, [item])
})

return groups
}
10 changes: 3 additions & 7 deletions helpers/serializer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import {format, resolveConfig} from 'prettier'

const prettierConfig = resolveConfig.sync(__dirname)
if (!prettierConfig) throw new Error()

module.exports = {
test: (val: string) => typeof val === 'string',
serialize: (val: string) => format(val, {...prettierConfig, parser: 'babel'}).trim(),
serialize: (val: string) => val,
}

export {}
Loading

0 comments on commit be57960

Please sign in to comment.