Skip to content

Commit

Permalink
Support both default esmodule and direct commonjs require
Browse files Browse the repository at this point in the history
  • Loading branch information
zertosh committed Sep 9, 2018
1 parent bed21a1 commit c41fdc0
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 32 deletions.
2 changes: 1 addition & 1 deletion __tests__/__fixtures__/flow-package/commonjs.js
@@ -1,6 +1,6 @@
/* @flow */

var nullthrows = require('../../..').default;
var nullthrows = require('../../..');

var obj = ({}: ?Object);

Expand Down
92 changes: 67 additions & 25 deletions __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'));
});
});
11 changes: 7 additions & 4 deletions 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});
2 changes: 1 addition & 1 deletion nullthrows.js.flow
@@ -1,3 +1,3 @@
/* @flow */

declare export default function nullthrows<T>(x: ?T, message?: string): T;
declare module.exports: <T>(x: ?T, message?: string) => T;
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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"
Expand Down
13 changes: 12 additions & 1 deletion yarn.lock
Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit c41fdc0

Please sign in to comment.