From c41fdc01b651ca13c2751bb66cc84233453c1add Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Sat, 8 Sep 2018 23:32:28 -0400 Subject: [PATCH] Support both default esmodule and direct commonjs require --- .../__fixtures__/flow-package/commonjs.js | 2 +- __tests__/nullthrows-test.js | 92 ++++++++++++++----- nullthrows.js | 11 ++- nullthrows.js.flow | 2 +- package.json | 1 + yarn.lock | 13 ++- 6 files changed, 89 insertions(+), 32 deletions(-) diff --git a/__tests__/__fixtures__/flow-package/commonjs.js b/__tests__/__fixtures__/flow-package/commonjs.js index da4b304..5f57e2a 100644 --- a/__tests__/__fixtures__/flow-package/commonjs.js +++ b/__tests__/__fixtures__/flow-package/commonjs.js @@ -1,6 +1,6 @@ /* @flow */ -var nullthrows = require('../../..').default; +var nullthrows = require('../../..'); var obj = ({}: ?Object); diff --git a/__tests__/nullthrows-test.js b/__tests__/nullthrows-test.js index e815808..492c26c 100644 --- a/__tests__/nullthrows-test.js +++ b/__tests__/nullthrows-test.js @@ -1,36 +1,78 @@ 'use strict'; -const nullthrows = require('../').default; +describe('commonjs', () => { + const nullthrows = require('../'); -test('return value', () => { - const obj = {}; - expect(nullthrows(obj)).toBe(obj); -}); + test('return value', () => { + const obj = {}; + expect(nullthrows(obj)).toBe(obj); + }); + + test('it does not throw', () => { + expect(() => { + nullthrows(''); + }).not.toThrow(); + + expect(() => { + nullthrows(NaN); + }).not.toThrow(); -test('it does not throw', () => { - expect(() => { - nullthrows(''); - }).not.toThrow(); + expect(() => { + nullthrows(false); + }).not.toThrow(); + }); - expect(() => { - nullthrows(NaN); - }).not.toThrow(); + test('it throws', () => { + expect(() => { + nullthrows(null); + }).toThrow(new Error('Got unexpected null')); - expect(() => { - nullthrows(false); - }).not.toThrow(); + expect(() => { + nullthrows(undefined); + }).toThrow(new Error('Got unexpected undefined')); + + expect(() => { + nullthrows(undefined, 'My error message'); + }).toThrow(new Error('My error message')); + }); }); -test('it throws', () => { - expect(() => { - nullthrows(null); - }).toThrow(new Error('Got unexpected null')); +describe('import interop', () => { + const interopRequireDefault = + require('@babel/runtime-corejs2/helpers/interopRequireDefault.js'); + + const nullthrows = interopRequireDefault(require('../')); + + test('return value', () => { + const obj = {}; + expect(nullthrows(obj)).toBe(obj); + }); + + test('it does not throw', () => { + expect(() => { + nullthrows(''); + }).not.toThrow(); + + expect(() => { + nullthrows(NaN); + }).not.toThrow(); + + expect(() => { + nullthrows(false); + }).not.toThrow(); + }); + + test('it throws', () => { + expect(() => { + nullthrows(null); + }).toThrow(new Error('Got unexpected null')); - expect(() => { - nullthrows(undefined); - }).toThrow(new Error('Got unexpected undefined')); + expect(() => { + nullthrows(undefined); + }).toThrow(new Error('Got unexpected undefined')); - expect(() => { - nullthrows(undefined, 'My error message'); - }).toThrow(new Error('My error message')); + expect(() => { + nullthrows(undefined, 'My error message'); + }).toThrow(new Error('My error message')); + }); }); diff --git a/nullthrows.js b/nullthrows.js index 9e367e2..a25764b 100644 --- a/nullthrows.js +++ b/nullthrows.js @@ -1,12 +1,15 @@ 'use strict'; -Object.defineProperty(exports, '__esModule', {value: true}); - -exports.default = function nullthrows(x, message) { +function nullthrows(x, message) { if (x != null) { return x; } var error = new Error(message !== undefined ? message : 'Got unexpected ' + x); error.framesToPop = 1; // Skip nullthrows's own stack frame. throw error; -}; +} + +module.exports = nullthrows; +module.exports.default = nullthrows; + +Object.defineProperty(module.exports, '__esModule', {value: true}); diff --git a/nullthrows.js.flow b/nullthrows.js.flow index f0a424f..9e72c29 100644 --- a/nullthrows.js.flow +++ b/nullthrows.js.flow @@ -1,3 +1,3 @@ /* @flow */ -declare export default function nullthrows(x: ?T, message?: string): T; +declare module.exports: (x: ?T, message?: string) => T; diff --git a/package.json b/package.json index 8aa770b..8c4a620 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ }, "dependencies": {}, "devDependencies": { + "@babel/runtime-corejs2": "^7.0.0", "flow-bin": "0.80.0", "jest": "^23.5.0", "typescript": "3.0.3" diff --git a/yarn.lock b/yarn.lock index 9e55458..3aaeb35 100644 --- a/yarn.lock +++ b/yarn.lock @@ -16,6 +16,13 @@ esutils "^2.0.2" js-tokens "^4.0.0" +"@babel/runtime-corejs2@^7.0.0": + version "7.0.0" + resolved "https://registry.yarnpkg.com/@babel/runtime-corejs2/-/runtime-corejs2-7.0.0.tgz#786711ee099c2c2af7875638866c1259eff30a8c" + dependencies: + core-js "^2.5.7" + regenerator-runtime "^0.12.0" + abab@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.0.tgz#aba0ab4c5eee2d4c79d3487d85450fb2376ebb0f" @@ -526,7 +533,7 @@ copy-descriptor@^0.1.0: version "0.1.1" resolved "https://registry.yarnpkg.com/copy-descriptor/-/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d" -core-js@^2.4.0, core-js@^2.5.0: +core-js@^2.4.0, core-js@^2.5.0, core-js@^2.5.7: version "2.5.7" resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.5.7.tgz#f972608ff0cead68b841a16a932d0b183791814e" @@ -2415,6 +2422,10 @@ regenerator-runtime@^0.11.0: version "0.11.1" resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9" +regenerator-runtime@^0.12.0: + version "0.12.1" + resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.12.1.tgz#fa1a71544764c036f8c49b13a08b2594c9f8a0de" + regex-cache@^0.4.2: version "0.4.4" resolved "https://registry.yarnpkg.com/regex-cache/-/regex-cache-0.4.4.tgz#75bdc58a2a1496cec48a12835bc54c8d562336dd"