Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support both default esmodule and direct commonjs require
- Loading branch information
Showing
6 changed files
with
89 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters