From 1d39f58c7bc6a89aa936e666b76e355c9436e854 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 22 Dec 2022 10:46:51 -0800 Subject: [PATCH] [Tests] add test for `import.meta` See #119 --- __tests__/helper.js | 8 +++++++- .../src/getPropLiteralValue-babelparser-test.js | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/__tests__/helper.js b/__tests__/helper.js index e48a149..3a53df2 100644 --- a/__tests__/helper.js +++ b/__tests__/helper.js @@ -18,6 +18,7 @@ const defaultPlugins = [ // 'nullishCoalescing', // TODO: update to babel 7 ]; let plugins = [...defaultPlugins]; +let isESM = false; export function setParserName(name) { parserName = name; @@ -33,8 +34,13 @@ export function changePlugins(pluginOrFn) { } } +export function setIsESM() { + isESM = true; +} + beforeEach(() => { plugins = [...defaultPlugins]; + isESM = false; }); function parse(code) { @@ -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.`); diff --git a/__tests__/src/getPropLiteralValue-babelparser-test.js b/__tests__/src/getPropLiteralValue-babelparser-test.js index 3981cd9..e932d56 100644 --- a/__tests__/src/getPropLiteralValue-babelparser-test.js +++ b/__tests__/src/getPropLiteralValue-babelparser-test.js @@ -6,6 +6,7 @@ import { describeIfNotBabylon, changePlugins, setParserName, + setIsESM, } from '../helper'; import { getLiteralPropValue } from '../../src/getPropValue'; @@ -497,6 +498,21 @@ describe('getLiteralPropValue', () => { }); }); + describeIfNotBabylon('import.meta', () => { + beforeEach(() => { + setIsESM(); + }); + + it('should return null', () => { + const prop = extractProp('
'); + + const expected = null; + const actual = getLiteralPropValue(prop); + + assert.deepEqual(actual, expected); + }); + }); + describeIfNotBabylon('Typescript', () => { beforeEach(() => { changePlugins((pls) => [...pls, 'typescript']);