Skip to content

Commit

Permalink
[Tests] add test for import.meta
Browse files Browse the repository at this point in the history
See #119
  • Loading branch information
ljharb committed Dec 22, 2022
1 parent 26cc3c4 commit 1d39f58
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion __tests__/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const defaultPlugins = [
// 'nullishCoalescing', // TODO: update to babel 7
];
let plugins = [...defaultPlugins];
let isESM = false;

export function setParserName(name) {
parserName = name;
Expand All @@ -33,8 +34,13 @@ export function changePlugins(pluginOrFn) {
}
}

export function setIsESM() {
isESM = true;
}

beforeEach(() => {
plugins = [...defaultPlugins];
isESM = false;
});

function parse(code) {
Expand All @@ -43,7 +49,7 @@ function parse(code) {
}
if (parserName === 'babel') {
try {
return babelParser.parse(code, { plugins, sourceFilename: 'test.js' });
return babelParser.parse(code, { plugins, sourceFilename: 'test.js', ...(isESM && { sourceType: 'module' }) });
} catch (_) {
// eslint-disable-next-line no-console
console.warn(`Failed to parse with ${fallbackToBabylon ? 'babylon' : 'Babel'} parser.`);
Expand Down
16 changes: 16 additions & 0 deletions __tests__/src/getPropLiteralValue-babelparser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
describeIfNotBabylon,
changePlugins,
setParserName,
setIsESM,
} from '../helper';
import { getLiteralPropValue } from '../../src/getPropValue';

Expand Down Expand Up @@ -497,6 +498,21 @@ describe('getLiteralPropValue', () => {
});
});

describeIfNotBabylon('import.meta', () => {
beforeEach(() => {
setIsESM();
});

it('should return null', () => {
const prop = extractProp('<div foo={import.meta.env.whyIsThisNotOnProcess} />');

const expected = null;
const actual = getLiteralPropValue(prop);

assert.deepEqual(actual, expected);
});
});

describeIfNotBabylon('Typescript', () => {
beforeEach(() => {
changePlugins((pls) => [...pls, 'typescript']);
Expand Down

0 comments on commit 1d39f58

Please sign in to comment.