Skip to content

Commit 1d39f58

Browse files
committed
[Tests] add test for import.meta
See #119
1 parent 26cc3c4 commit 1d39f58

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

__tests__/helper.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const defaultPlugins = [
1818
// 'nullishCoalescing', // TODO: update to babel 7
1919
];
2020
let plugins = [...defaultPlugins];
21+
let isESM = false;
2122

2223
export function setParserName(name) {
2324
parserName = name;
@@ -33,8 +34,13 @@ export function changePlugins(pluginOrFn) {
3334
}
3435
}
3536

37+
export function setIsESM() {
38+
isESM = true;
39+
}
40+
3641
beforeEach(() => {
3742
plugins = [...defaultPlugins];
43+
isESM = false;
3844
});
3945

4046
function parse(code) {
@@ -43,7 +49,7 @@ function parse(code) {
4349
}
4450
if (parserName === 'babel') {
4551
try {
46-
return babelParser.parse(code, { plugins, sourceFilename: 'test.js' });
52+
return babelParser.parse(code, { plugins, sourceFilename: 'test.js', ...(isESM && { sourceType: 'module' }) });
4753
} catch (_) {
4854
// eslint-disable-next-line no-console
4955
console.warn(`Failed to parse with ${fallbackToBabylon ? 'babylon' : 'Babel'} parser.`);

__tests__/src/getPropLiteralValue-babelparser-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
describeIfNotBabylon,
77
changePlugins,
88
setParserName,
9+
setIsESM,
910
} from '../helper';
1011
import { getLiteralPropValue } from '../../src/getPropValue';
1112

@@ -497,6 +498,21 @@ describe('getLiteralPropValue', () => {
497498
});
498499
});
499500

501+
describeIfNotBabylon('import.meta', () => {
502+
beforeEach(() => {
503+
setIsESM();
504+
});
505+
506+
it('should return null', () => {
507+
const prop = extractProp('<div foo={import.meta.env.whyIsThisNotOnProcess} />');
508+
509+
const expected = null;
510+
const actual = getLiteralPropValue(prop);
511+
512+
assert.deepEqual(actual, expected);
513+
});
514+
});
515+
500516
describeIfNotBabylon('Typescript', () => {
501517
beforeEach(() => {
502518
changePlugins((pls) => [...pls, 'typescript']);

0 commit comments

Comments
 (0)