|
1 | 1 | import * as babel from '@babel/core'
|
2 | 2 | import { describe, expect, it } from 'vitest'
|
3 |
| -import { restoreJSX } from './restore-jsx' |
| 3 | +import { parseReactAlias, restoreJSX } from './restore-jsx' |
| 4 | + |
| 5 | +describe('parseReactAlias', () => { |
| 6 | + it('handles cjs require', () => { |
| 7 | + expect(parseReactAlias(`const React = require("react")`)) |
| 8 | + .toMatchInlineSnapshot(` |
| 9 | + [ |
| 10 | + "React", |
| 11 | + true, |
| 12 | + ] |
| 13 | + `) |
| 14 | + }) |
| 15 | + |
| 16 | + it('handles cjs require (minified)', () => { |
| 17 | + expect(parseReactAlias(`var F=require('foo');var R=require('react')`)) |
| 18 | + .toMatchInlineSnapshot(` |
| 19 | + [ |
| 20 | + "R", |
| 21 | + true, |
| 22 | + ] |
| 23 | + `) |
| 24 | + }) |
| 25 | + |
| 26 | + it('does not handle destructured cjs require', () => { |
| 27 | + expect(parseReactAlias(`var {createElement} = require("react")`)) |
| 28 | + .toMatchInlineSnapshot(` |
| 29 | + [ |
| 30 | + undefined, |
| 31 | + false, |
| 32 | + ] |
| 33 | + `) |
| 34 | + }) |
| 35 | + |
| 36 | + it('handles esm import', () => { |
| 37 | + expect(parseReactAlias(`import React from 'react'`)).toMatchInlineSnapshot(` |
| 38 | + [ |
| 39 | + "React", |
| 40 | + false, |
| 41 | + ] |
| 42 | + `) |
| 43 | + }) |
| 44 | + |
| 45 | + it('handles esm import namespace', () => { |
| 46 | + expect(parseReactAlias(`import * as React from "react"`)) |
| 47 | + .toMatchInlineSnapshot(` |
| 48 | + [ |
| 49 | + "React", |
| 50 | + false, |
| 51 | + ] |
| 52 | + `) |
| 53 | + }) |
| 54 | + |
| 55 | + it('does not handle destructured esm import', () => { |
| 56 | + expect(parseReactAlias(`import {createElement} from "react"`)) |
| 57 | + .toMatchInlineSnapshot(` |
| 58 | + [ |
| 59 | + undefined, |
| 60 | + false, |
| 61 | + ] |
| 62 | + `) |
| 63 | + }) |
| 64 | +}) |
4 | 65 |
|
5 | 66 | async function jsx(sourceCode: string) {
|
6 | 67 | const [ast] = await restoreJSX(babel, sourceCode, 'test.js')
|
|
0 commit comments