Skip to content

Strict ES module semantics require using Object.defineProperty instead of Object.assign for module objects #26

@jgonggrijp

Description

@jgonggrijp

Thanks for making power-assert possible!

I'm working on a project that uses power-assert together with karma, mocha, rollup and coffeescript. To make it all work together, I needed to make rollup the only preprocessor to karma, use the coffeescript plugin for rollup, and add power-assert support through the babel preset, which in turn I enabled through the babel plugin for rollup. Since everything needs to run in the browser, I'm also using rollup-plugin-node-polyfills, which ends up providing its own version of the Node.js standard assert module.

A consequence of this setup is that assert.ok et al end up being defined as getters of the assert module object. This means that these properties cannot simply be reassigned, even if you try to do this on an intermediate object that inherits the module through the prototype chain. Unfortunately, this is exactly what empower-core/index.js is trying to do on line 50. Fortunately, there is a straightforward alternative: use core-js's define helper instead, which was already imported in the module, anyway.

Using patch-package, I generated the following diff that solved my problem:

diff --git a/node_modules/empower-core/index.js b/node_modules/empower-core/index.js
index e9118c7..8074a81 100644
--- a/node_modules/empower-core/index.js
+++ b/node_modules/empower-core/index.js
@@ -47,7 +47,8 @@ function empowerAssertObject (assertObject, options) {
     var config = assign(defaultOptions(), options);
     var target = config.destructive ? assertObject : create(assertObject);
     var decorator = new Decorator(target, config);
-    return assign(target, decorator.enhancement());
+    define(target, decorator.enhancement());
+    return target;
 }
 
 function empowerAssertFunction (assertFunction, options) {

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions