Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use inline snapshots #117

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3,939 changes: 0 additions & 3,939 deletions __snapshots__/test.ts.snap

This file was deleted.

108 changes: 108 additions & 0 deletions __tests__/modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import {test, expect} from '@jest/globals'

import transform from '../helpers/transform'

test('static imports', () => {
const code = `
import defaultExport, {namedExport} from 'foo'
import * as namespace from 'bar'
console.log(defaultExport, namedExport, namespace)
`

expect(transform({code, env: 'development'})).toMatchInlineSnapshot(`
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)
`)
billyjanitsch marked this conversation as resolved.
Show resolved Hide resolved

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(`
'use strict'
var _interopRequireWildcard = require('@babel/runtime/helpers/interopRequireWildcard').default
var _foo = _interopRequireWildcard(require('foo'))
var namespace = _interopRequireWildcard(require('bar'))
console.log(_foo.default, _foo.namedExport, namespace)
`)
})

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(`
'use strict'
var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault').default
var _interopRequireWildcard2 = _interopRequireDefault(
require('@babel/runtime/helpers/interopRequireWildcard')
)
Promise.resolve().then(() => (0, _interopRequireWildcard2.default)(require('foo')))
`)
})

test('static exports', () => {
const code = `
const foo = 1
export default foo
export {foo}
export {bar} from 'other'
export * as baz from 'other'
`

expect(transform({code, env: 'development'})).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: '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(`
const foo = 1
export default foo
export {foo}
export {bar} from 'other'
export * as baz from 'other'
`)

expect(transform({code, env: 'cjs'})).toMatchInlineSnapshot(`
'use strict'
var _interopRequireWildcard = require('@babel/runtime/helpers/interopRequireWildcard').default
Object.defineProperty(exports, '__esModule', {value: true})
Object.defineProperty(exports, 'bar', {
enumerable: true,
get: function () {
return _baz.bar
},
})
exports.baz = exports.foo = exports.default = void 0
var _baz = _interopRequireWildcard(require('other'))
exports.baz = _baz
const foo = 1
exports.foo = foo
var _default = foo
exports.default = _default
`)
})
59 changes: 59 additions & 0 deletions __tests__/polyfills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {test, expect} from '@jest/globals'

import transform from '../helpers/transform'

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

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

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

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'`)
})

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

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/es.symbol.description.js'
import 'core-js/modules/es.symbol.async-iterator.js'
import 'core-js/modules/es.array.flat.js'
import 'core-js/modules/es.array.flat-map.js'
import 'core-js/modules/es.array.iterator.js'
import 'core-js/modules/es.array.reduce.js'
import 'core-js/modules/es.array.reduce-right.js'
import 'core-js/modules/es.array.sort.js'
import 'core-js/modules/es.array.unscopables.flat.js'
import 'core-js/modules/es.array.unscopables.flat-map.js'
import 'core-js/modules/es.math.hypot.js'
import 'core-js/modules/es.object.define-getter.js'
import 'core-js/modules/es.object.define-setter.js'
import 'core-js/modules/es.object.from-entries.js'
import 'core-js/modules/es.object.lookup-getter.js'
import 'core-js/modules/es.object.lookup-setter.js'
import 'core-js/modules/es.promise.js'
import 'core-js/modules/es.promise.finally.js'
import 'core-js/modules/es.string.replace.js'
import 'core-js/modules/es.string.trim-end.js'
import 'core-js/modules/es.string.trim-start.js'
import 'core-js/modules/web.dom-collections.iterator.js'
import 'core-js/modules/web.immediate.js'
import 'core-js/modules/web.queue-microtask.js'
import 'core-js/modules/web.url.js'
import 'core-js/modules/web.url.to-json.js'
import 'core-js/modules/web.url-search-params.js'
`)
})
90 changes: 90 additions & 0 deletions __tests__/react-classic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {test, expect} from '@jest/globals'

import transform from '../helpers/transform'

test('transpiles JSX using classic runtime', () => {
const options = {react: {runtime: 'classic'}}
const code = `
import * as React from 'react'
function MyComponent() {
React.useEffect(() => {}, [])
return (
<button height={2} {...rest}>
<div />
{'foo'} bar
</button>
)
}
`

expect(transform({code, options, env: 'development'})).toMatchInlineSnapshot(`
var _jsxFileName = '/file.js',
_s = $RefreshSig$()
import * as React from 'react'
function MyComponent() {
_s()
React.useEffect(function () {}, [])
return /*#__PURE__*/ React.createElement(
'button',
Object.assign({height: 2}, rest, {
__self: this,
__source: {fileName: _jsxFileName, lineNumber: 6, columnNumber: 9},
}),
/*#__PURE__*/ React.createElement('div', {
__self: this,
__source: {fileName: _jsxFileName, lineNumber: 7, columnNumber: 11},
}),
'foo',
' bar'
)
}
_s(MyComponent, 'OD7bBpZva5O2jO+Puf00hKivP7c=')
_c = MyComponent
var _c
$RefreshReg$(_c, 'MyComponent')
`)

expect(transform({code, options, env: 'production'})).toMatchInlineSnapshot(`
import * as React from 'react'
function MyComponent() {
React.useEffect(function () {}, [])
return /*#__PURE__*/ React.createElement(
'button',
Object.assign({height: 2}, rest),
/*#__PURE__*/ React.createElement('div', null),
'foo',
' bar'
)
}
`)

expect(transform({code, options, env: 'esm'})).toMatchInlineSnapshot(`
import * as React from 'react'
function MyComponent() {
React.useEffect(() => {}, [])
return /*#__PURE__*/ React.createElement(
'button',
{height: 2, ...rest},
/*#__PURE__*/ React.createElement('div', null),
'foo',
' bar'
)
}
`)

expect(transform({code, options, env: 'cjs'})).toMatchInlineSnapshot(`
'use strict'
var _interopRequireWildcard = require('@babel/runtime/helpers/interopRequireWildcard').default
var React = _interopRequireWildcard(require('react'))
function MyComponent() {
React.useEffect(() => {}, [])
return /*#__PURE__*/ React.createElement(
'button',
{height: 2, ...rest},
/*#__PURE__*/ React.createElement('div', null),
'foo',
' bar'
)
}
`)
})
95 changes: 95 additions & 0 deletions __tests__/react.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {test, expect} from '@jest/globals'

import transform from '../helpers/transform'

test('transpiles JSX', () => {
const code = `
import {useState} from 'react'
function MyComponent() {
useEffect(() => {}, [])
return (
<button height={2} {...rest}>
<div />
{'foo'} bar
</button>
)
}
`

expect(transform({code, env: 'development'})).toMatchInlineSnapshot(`
var _jsxFileName = '/file.js',
_s = $RefreshSig$()
import {useState} from 'react'
import {jsxDEV as _jsxDEV} from 'react/jsx-dev-runtime'
function MyComponent() {
_s()
useEffect(function () {}, [])
return /*#__PURE__*/ _jsxDEV(
'button',
Object.assign({height: 2}, rest, {
children: [
/*#__PURE__*/ _jsxDEV(
'div',
{},
void 0,
false,
{fileName: _jsxFileName, lineNumber: 7, columnNumber: 11},
this
),
'foo',
' bar',
],
}),
void 0,
true,
{fileName: _jsxFileName, lineNumber: 6, columnNumber: 9},
this
)
}
_s(MyComponent, 'OD7bBpZva5O2jO+Puf00hKivP7c=')
_c = MyComponent
var _c
$RefreshReg$(_c, 'MyComponent')
`)

expect(transform({code, env: 'production'})).toMatchInlineSnapshot(`
import {useState} from 'react'
import {jsx as _jsx} from 'react/jsx-runtime'
import {jsxs as _jsxs} from 'react/jsx-runtime'
function MyComponent() {
useEffect(function () {}, [])
return /*#__PURE__*/ _jsxs(
'button',
Object.assign({height: 2}, rest, {children: [/*#__PURE__*/ _jsx('div', {}), 'foo', ' bar']})
)
}
`)

expect(transform({code, env: 'esm'})).toMatchInlineSnapshot(`
import {useState} from 'react'
import {jsx as _jsx} from 'react/jsx-runtime'
import {jsxs as _jsxs} from 'react/jsx-runtime'
function MyComponent() {
useEffect(() => {}, [])
return /*#__PURE__*/ _jsxs('button', {
height: 2,
...rest,
children: [/*#__PURE__*/ _jsx('div', {}), 'foo', ' bar'],
})
}
`)

expect(transform({code, env: 'cjs'})).toMatchInlineSnapshot(`
'use strict'
var _react = require('react')
var _jsxRuntime = require('react/jsx-runtime')
function MyComponent() {
useEffect(() => {}, [])
return /*#__PURE__*/ (0, _jsxRuntime.jsxs)('button', {
height: 2,
...rest,
children: [/*#__PURE__*/ (0, _jsxRuntime.jsx)('div', {}), 'foo', ' bar'],
})
}
`)
})