From 64efbb2bb2c11f32836c9a7ad9dfbb9eb5e20233 Mon Sep 17 00:00:00 2001 From: Sebastian Werner Date: Tue, 4 Jun 2019 11:49:07 +0200 Subject: [PATCH] Added TS test + added non-working alias tests (WIP) --- __tests__/__fixtures__/alias.js | 10 +++++ __tests__/__fixtures__/alias.tsx | 10 +++++ __tests__/__fixtures__/typescript.tsx | 13 +++++++ .../__snapshots__/target-browser-dev.js.snap | 24 ++++++++++++ .../__snapshots__/target-browser-prod.js.snap | 6 +++ __tests__/__snapshots__/target-node.js.snap | 39 +++++++++++++++++++ .../target-universal-node.js.snap | 24 ++++++++++++ .../__snapshots__/target-universal.js.snap | 24 ++++++++++++ .../__snapshots__/transpile-es2015.js.snap | 24 ++++++++++++ __tests__/__snapshots__/transpile-es5.js.snap | 24 ++++++++++++ __tests__/__snapshots__/transpile-esm.js.snap | 24 ++++++++++++ .../__snapshots__/transpile-modern.js.snap | 24 ++++++++++++ .../__snapshots__/transpile-node10.js.snap | 39 +++++++++++++++++++ .../__snapshots__/transpile-node8.js.snap | 39 +++++++++++++++++++ src/testonly.js | 3 ++ 15 files changed, 327 insertions(+) create mode 100644 __tests__/__fixtures__/alias.js create mode 100644 __tests__/__fixtures__/alias.tsx create mode 100644 __tests__/__fixtures__/typescript.tsx create mode 100644 src/testonly.js diff --git a/__tests__/__fixtures__/alias.js b/__tests__/__fixtures__/alias.js new file mode 100644 index 0000000..66d5fff --- /dev/null +++ b/__tests__/__fixtures__/alias.js @@ -0,0 +1,10 @@ +// Project relative import to missing file +import { Button } from '-/components/button/Button' + +// Project relative import to existing file +import { logx } from '-/testonly' + +// This is probably shown as okay by TS validation, but is not supported by Babel +import 'src/testonly' + +logx(Button) diff --git a/__tests__/__fixtures__/alias.tsx b/__tests__/__fixtures__/alias.tsx new file mode 100644 index 0000000..66d5fff --- /dev/null +++ b/__tests__/__fixtures__/alias.tsx @@ -0,0 +1,10 @@ +// Project relative import to missing file +import { Button } from '-/components/button/Button' + +// Project relative import to existing file +import { logx } from '-/testonly' + +// This is probably shown as okay by TS validation, but is not supported by Babel +import 'src/testonly' + +logx(Button) diff --git a/__tests__/__fixtures__/typescript.tsx b/__tests__/__fixtures__/typescript.tsx new file mode 100644 index 0000000..49404f6 --- /dev/null +++ b/__tests__/__fixtures__/typescript.tsx @@ -0,0 +1,13 @@ +/** + * Checks if a configuration is defined in package.json + * + * @param name Key of the configuration + * @return whether the configuration exists + */ +export const hasPackageConfig = (name: string): boolean => { + try { + return !!getPackageConfig()[name] + } catch (_) { + return false + } +} diff --git a/__tests__/__snapshots__/target-browser-dev.js.snap b/__tests__/__snapshots__/target-browser-dev.js.snap index 5595678..d6bb726 100644 --- a/__tests__/__snapshots__/target-browser-dev.js.snap +++ b/__tests__/__snapshots__/target-browser-dev.js.snap @@ -1,5 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Target: Browser: Dev alias 1`] = ` +"import { Button } from '-/components/button/Button'; +import { logx } from '-/testonly'; +import 'src/testonly'; +logx(Button);" +`; + +exports[`Target: Browser: Dev alias.tsx 1`] = ` +"import { Button } from '-/components/button/Button'; +import { logx } from '-/testonly'; +import 'src/testonly'; +logx(Button);" +`; + exports[`Target: Browser: Dev comments 1`] = ` "import _camelCase from \\"lodash/camelCase\\"; @@ -591,3 +605,13 @@ exports[`Target: Browser: Dev require context 1`] = ` const loader = require.context(__dirname, false, /\\\\.js/);" `; + +exports[`Target: Browser: Dev typescript.tsx 1`] = ` +"export const hasPackageConfig = name => { + try { + return !!getPackageConfig()[name]; + } catch (_) { + return false; + } +};" +`; diff --git a/__tests__/__snapshots__/target-browser-prod.js.snap b/__tests__/__snapshots__/target-browser-prod.js.snap index 7c3e947..d4e11a1 100644 --- a/__tests__/__snapshots__/target-browser-prod.js.snap +++ b/__tests__/__snapshots__/target-browser-prod.js.snap @@ -1,5 +1,9 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Target: Browser: Prod alias 1`] = `"import{Button}from\\"-/components/button/Button\\";import{logx}from\\"-/testonly\\";import\\"src/testonly\\";logx(Button);"`; + +exports[`Target: Browser: Prod alias.tsx 1`] = `"import{Button}from\\"-/components/button/Button\\";import{logx}from\\"-/testonly\\";import\\"src/testonly\\";logx(Button);"`; + exports[`Target: Browser: Prod comments 1`] = `"import _camelCase from\\"lodash/camelCase\\";function translate(a){return _camelCase(a)}"`; exports[`Target: Browser: Prod deadcode 1`] = `"var STATIC=!1;!1,before(),!1,after();"`; @@ -51,3 +55,5 @@ exports[`Target: Browser: Prod react: jsx 1`] = `"import _jsx from\\"@babel/runt exports[`Target: Browser: Prod react: jsx spread 1`] = `"import\\"core-js/modules/es.object.assign\\";function render(a){return React.createElement(\\"h1\\",Object.assign({className:\\"large\\"},a),\\"Hello\\")}"`; exports[`Target: Browser: Prod require context 1`] = `"\\"test\\"===process.env.NODE_ENV&&require(\\"babel-plugin-require-context-hook/register\\")();var loader=require.context(__dirname,!1,/\\\\.js/);"`; + +exports[`Target: Browser: Prod typescript.tsx 1`] = `"export var hasPackageConfig=function(a){try{return!!getPackageConfig()[a]}catch(a){return!1}};"`; diff --git a/__tests__/__snapshots__/target-node.js.snap b/__tests__/__snapshots__/target-node.js.snap index 8d83346..c51e2bc 100644 --- a/__tests__/__snapshots__/target-node.js.snap +++ b/__tests__/__snapshots__/target-node.js.snap @@ -1,5 +1,27 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Target: NodeJS alias 1`] = ` +"\\"use strict\\"; + +require(\\"src/testonly\\"); + +var _Button = require(\\"-/components/button/Button\\"), + _testonly = require(\\"-/testonly\\"); + +(0, _testonly.logx)(_Button.Button);" +`; + +exports[`Target: NodeJS alias.tsx 1`] = ` +"\\"use strict\\"; + +require(\\"src/testonly\\"); + +var _Button = require(\\"-/components/button/Button\\"), + _testonly = require(\\"-/testonly\\"); + +(0, _testonly.logx)(_Button.Button);" +`; + exports[`Target: NodeJS comments 1`] = ` "\\"use strict\\"; @@ -759,3 +781,20 @@ if (process.env.NODE_ENV === \\"test\\") { const loader = __requireContext(__dirname, __dirname, false, /\\\\.js/);" `; + +exports[`Target: NodeJS typescript.tsx 1`] = ` +"\\"use strict\\"; + +exports.__esModule = true; +exports.hasPackageConfig = void 0; + +const hasPackageConfig = name => { + try { + return !!getPackageConfig()[name]; + } catch (_) { + return false; + } +}; + +exports.hasPackageConfig = hasPackageConfig;" +`; diff --git a/__tests__/__snapshots__/target-universal-node.js.snap b/__tests__/__snapshots__/target-universal-node.js.snap index 09dc08e..beff927 100644 --- a/__tests__/__snapshots__/target-universal-node.js.snap +++ b/__tests__/__snapshots__/target-universal-node.js.snap @@ -1,5 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Target: Universal: Node Imports alias 1`] = ` +"import { Button } from '-/components/button/Button'; +import { logx } from '-/testonly'; +import 'src/testonly'; +logx(Button);" +`; + +exports[`Target: Universal: Node Imports alias.tsx 1`] = ` +"import { Button } from '-/components/button/Button'; +import { logx } from '-/testonly'; +import 'src/testonly'; +logx(Button);" +`; + exports[`Target: Universal: Node Imports comments 1`] = ` "import _camelCase from \\"lodash/camelCase\\"; @@ -676,3 +690,13 @@ exports[`Target: Universal: Node Imports require context 1`] = ` var loader = require.context(__dirname, false, /\\\\.js/);" `; + +exports[`Target: Universal: Node Imports typescript.tsx 1`] = ` +"export var hasPackageConfig = function hasPackageConfig(name) { + try { + return !!getPackageConfig()[name]; + } catch (_) { + return false; + } +};" +`; diff --git a/__tests__/__snapshots__/target-universal.js.snap b/__tests__/__snapshots__/target-universal.js.snap index 88119d6..76bee22 100644 --- a/__tests__/__snapshots__/target-universal.js.snap +++ b/__tests__/__snapshots__/target-universal.js.snap @@ -1,5 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Target: Universal alias 1`] = ` +"import { Button } from '-/components/button/Button'; +import { logx } from '-/testonly'; +import 'src/testonly'; +logx(Button);" +`; + +exports[`Target: Universal alias.tsx 1`] = ` +"import { Button } from '-/components/button/Button'; +import { logx } from '-/testonly'; +import 'src/testonly'; +logx(Button);" +`; + exports[`Target: Universal comments 1`] = ` "import _camelCase from \\"lodash/camelCase\\"; @@ -672,3 +686,13 @@ exports[`Target: Universal require context 1`] = ` var loader = require.context(__dirname, false, /\\\\.js/);" `; + +exports[`Target: Universal typescript.tsx 1`] = ` +"export var hasPackageConfig = function hasPackageConfig(name) { + try { + return !!getPackageConfig()[name]; + } catch (_) { + return false; + } +};" +`; diff --git a/__tests__/__snapshots__/transpile-es2015.js.snap b/__tests__/__snapshots__/transpile-es2015.js.snap index 195c142..51590c7 100644 --- a/__tests__/__snapshots__/transpile-es2015.js.snap +++ b/__tests__/__snapshots__/transpile-es2015.js.snap @@ -1,5 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Transpile: ES2015 alias 1`] = ` +"import { Button } from '-/components/button/Button'; +import { logx } from '-/testonly'; +import 'src/testonly'; +logx(Button);" +`; + +exports[`Transpile: ES2015 alias.tsx 1`] = ` +"import { Button } from '-/components/button/Button'; +import { logx } from '-/testonly'; +import 'src/testonly'; +logx(Button);" +`; + exports[`Transpile: ES2015 comments 1`] = ` "import _camelCase from \\"lodash/camelCase\\"; @@ -670,3 +684,13 @@ exports[`Transpile: ES2015 require context 1`] = ` const loader = require.context(__dirname, false, /\\\\.js/);" `; + +exports[`Transpile: ES2015 typescript.tsx 1`] = ` +"export const hasPackageConfig = name => { + try { + return !!getPackageConfig()[name]; + } catch (_) { + return false; + } +};" +`; diff --git a/__tests__/__snapshots__/transpile-es5.js.snap b/__tests__/__snapshots__/transpile-es5.js.snap index 4edd302..a278801 100644 --- a/__tests__/__snapshots__/transpile-es5.js.snap +++ b/__tests__/__snapshots__/transpile-es5.js.snap @@ -1,5 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Transpile: ES5 alias 1`] = ` +"import { Button } from '-/components/button/Button'; +import { logx } from '-/testonly'; +import 'src/testonly'; +logx(Button);" +`; + +exports[`Transpile: ES5 alias.tsx 1`] = ` +"import { Button } from '-/components/button/Button'; +import { logx } from '-/testonly'; +import 'src/testonly'; +logx(Button);" +`; + exports[`Transpile: ES5 comments 1`] = ` "import _camelCase from \\"lodash/camelCase\\"; @@ -672,3 +686,13 @@ exports[`Transpile: ES5 require context 1`] = ` var loader = require.context(__dirname, false, /\\\\.js/);" `; + +exports[`Transpile: ES5 typescript.tsx 1`] = ` +"export var hasPackageConfig = function hasPackageConfig(name) { + try { + return !!getPackageConfig()[name]; + } catch (_) { + return false; + } +};" +`; diff --git a/__tests__/__snapshots__/transpile-esm.js.snap b/__tests__/__snapshots__/transpile-esm.js.snap index 6ebd7ce..8f02de6 100644 --- a/__tests__/__snapshots__/transpile-esm.js.snap +++ b/__tests__/__snapshots__/transpile-esm.js.snap @@ -1,5 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Transpile: ESM alias 1`] = ` +"import { Button } from '-/components/button/Button'; +import { logx } from '-/testonly'; +import 'src/testonly'; +logx(Button);" +`; + +exports[`Transpile: ESM alias.tsx 1`] = ` +"import { Button } from '-/components/button/Button'; +import { logx } from '-/testonly'; +import 'src/testonly'; +logx(Button);" +`; + exports[`Transpile: ESM comments 1`] = ` "import _camelCase from \\"lodash/camelCase\\"; @@ -654,3 +668,13 @@ exports[`Transpile: ESM require context 1`] = ` const loader = require.context(__dirname, false, /\\\\.js/);" `; + +exports[`Transpile: ESM typescript.tsx 1`] = ` +"export const hasPackageConfig = name => { + try { + return !!getPackageConfig()[name]; + } catch (_) { + return false; + } +};" +`; diff --git a/__tests__/__snapshots__/transpile-modern.js.snap b/__tests__/__snapshots__/transpile-modern.js.snap index a1594cb..92bc124 100644 --- a/__tests__/__snapshots__/transpile-modern.js.snap +++ b/__tests__/__snapshots__/transpile-modern.js.snap @@ -1,5 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Transpile: Modern alias 1`] = ` +"import { Button } from '-/components/button/Button'; +import { logx } from '-/testonly'; +import 'src/testonly'; +logx(Button);" +`; + +exports[`Transpile: Modern alias.tsx 1`] = ` +"import { Button } from '-/components/button/Button'; +import { logx } from '-/testonly'; +import 'src/testonly'; +logx(Button);" +`; + exports[`Transpile: Modern comments 1`] = ` "import _camelCase from \\"lodash/camelCase\\"; @@ -588,3 +602,13 @@ exports[`Transpile: Modern require context 1`] = ` const loader = require.context(__dirname, false, /\\\\.js/);" `; + +exports[`Transpile: Modern typescript.tsx 1`] = ` +"export const hasPackageConfig = name => { + try { + return !!getPackageConfig()[name]; + } catch (_) { + return false; + } +};" +`; diff --git a/__tests__/__snapshots__/transpile-node10.js.snap b/__tests__/__snapshots__/transpile-node10.js.snap index 866a669..b2da0fd 100644 --- a/__tests__/__snapshots__/transpile-node10.js.snap +++ b/__tests__/__snapshots__/transpile-node10.js.snap @@ -1,5 +1,27 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Transpile: NodeJS v8 alias 1`] = ` +"\\"use strict\\"; + +require(\\"src/testonly\\"); + +var _Button = require(\\"-/components/button/Button\\"), + _testonly = require(\\"-/testonly\\"); + +(0, _testonly.logx)(_Button.Button);" +`; + +exports[`Transpile: NodeJS v8 alias.tsx 1`] = ` +"\\"use strict\\"; + +require(\\"src/testonly\\"); + +var _Button = require(\\"-/components/button/Button\\"), + _testonly = require(\\"-/testonly\\"); + +(0, _testonly.logx)(_Button.Button);" +`; + exports[`Transpile: NodeJS v8 comments 1`] = ` "\\"use strict\\"; @@ -688,3 +710,20 @@ if (process.env.NODE_ENV === \\"test\\") { const loader = __requireContext(__dirname, __dirname, false, /\\\\.js/);" `; + +exports[`Transpile: NodeJS v8 typescript.tsx 1`] = ` +"\\"use strict\\"; + +exports.__esModule = true; +exports.hasPackageConfig = void 0; + +const hasPackageConfig = name => { + try { + return !!getPackageConfig()[name]; + } catch (_) { + return false; + } +}; + +exports.hasPackageConfig = hasPackageConfig;" +`; diff --git a/__tests__/__snapshots__/transpile-node8.js.snap b/__tests__/__snapshots__/transpile-node8.js.snap index 173fed1..e5c40f3 100644 --- a/__tests__/__snapshots__/transpile-node8.js.snap +++ b/__tests__/__snapshots__/transpile-node8.js.snap @@ -1,5 +1,27 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Transpile: NodeJS v8 alias 1`] = ` +"\\"use strict\\"; + +require(\\"src/testonly\\"); + +var _Button = require(\\"-/components/button/Button\\"), + _testonly = require(\\"-/testonly\\"); + +(0, _testonly.logx)(_Button.Button);" +`; + +exports[`Transpile: NodeJS v8 alias.tsx 1`] = ` +"\\"use strict\\"; + +require(\\"src/testonly\\"); + +var _Button = require(\\"-/components/button/Button\\"), + _testonly = require(\\"-/testonly\\"); + +(0, _testonly.logx)(_Button.Button);" +`; + exports[`Transpile: NodeJS v8 comments 1`] = ` "\\"use strict\\"; @@ -696,3 +718,20 @@ if (process.env.NODE_ENV === \\"test\\") { const loader = __requireContext(__dirname, __dirname, false, /\\\\.js/);" `; + +exports[`Transpile: NodeJS v8 typescript.tsx 1`] = ` +"\\"use strict\\"; + +exports.__esModule = true; +exports.hasPackageConfig = void 0; + +const hasPackageConfig = name => { + try { + return !!getPackageConfig()[name]; + } catch (_) { + return false; + } +}; + +exports.hasPackageConfig = hasPackageConfig;" +`; diff --git a/src/testonly.js b/src/testonly.js new file mode 100644 index 0000000..8826fd5 --- /dev/null +++ b/src/testonly.js @@ -0,0 +1,3 @@ +export function logx() { + console.log('this is only required for the module resolver test') +}