Skip to content

Commit

Permalink
Merge pull request #92 from iambumblehead/add-failing-test-per-re-91
Browse files Browse the repository at this point in the history
reproduce object is not extensible error
  • Loading branch information
iambumblehead authored Jul 25, 2022
2 parents 7480cd4 + 7b4cbcd commit f9a6eb3
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# changelog

* 1.8.9 _Jul.25.2022_
* resolve error, do not define default on extensible objects, credit @tripodsan
* 1.8.8 _Jul.24.2022_
* add node v14 test pipeline, credit @aladdin-add
* resolve node v14 loader-originating runtime error
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esmock",
"version": "1.8.8",
"version": "1.8.9",
"license": "ISC",
"readmeFilename": "README.md",
"description": "provides native ESM import mocking for unit tests",
Expand Down
4 changes: 1 addition & 3 deletions src/esmockModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {
} from './esmockCache.js';

const isObj = o => typeof o === 'object' && o;
const isObjOrFnRe = /^(object|function)$/;
const isObjOrFn = o => isObjOrFnRe.test(typeof o) && o;
const isDefaultDefined = o => isObj(o) && 'default' in o;

const FILE_PROTOCOL = 'file:///';
Expand Down Expand Up @@ -59,7 +57,7 @@ const esmockModuleApply = (definitionLive, definitionMock, definitionPath) => {
// note: core modules do not define "default.default"
// import package from 'package';
// package.default(); <- extra default definition
if (!isCorePath && isObjOrFn(definition.default))
if (!isCorePath && Object.isExtensible(definition.default))
definition.default.default = definition.default;

return definition;
Expand Down
15 changes: 15 additions & 0 deletions tests/local/exportsNonDefaultClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { basename } from './pathWrap.js';

class NotificationsSupport {
async publish () {
return basename('/published/file');
}
}

export class getNotifier {
async publish () {
const note = new NotificationsSupport;

return await note.publish();
}
}
5 changes: 5 additions & 0 deletions tests/local/importsNonDefaultClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {getNotifier} from './exportsNonDefaultClass.js';

export async function callNotifier () {
return (new getNotifier()).publish();
}
13 changes: 13 additions & 0 deletions tests/tests-ava/spec/esmock.ava.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ import test from 'ava';
import esmock from 'esmock';
import sinon from 'sinon';

test('should not error when handling non-extnsible object', async t => {
const mockedIndex = await esmock.px('../../local/importsNonDefaultClass.js', {
'../../local/exportsNonDefaultClass.js' : await esmock.px(
'../../local/exportsNonDefaultClass.js', {
'../../local/pathWrap.js' : {
basename : () => 'mocked basename'
}
})
});

t.is(await mockedIndex.callNotifier(), 'mocked basename');
});

test('should return un-mocked file', async t => {
const main = await esmock('../../local/main.js');
const mainqs = [
Expand Down

0 comments on commit f9a6eb3

Please sign in to comment.