Skip to content

power-assert make assert() of assert module silent #43

Closed

Description

@azu

I've created reproduce repository.

Application code use assert module of Node Core.

index.js

// application code use `assert` module
const assert = require("assert");
export function hello(name) {
    assert(typeof name === "string");
    return "Hello " + name;
}

Test code:

const assert = require("power-assert");
import {hello} from "../src/index";
describe("hello", function () {
    it("should accept string", function () {
        hello(42);  // Should throw error via assert and fail tests.
    });
});

These code are transformed by babel + babel-plugin-espower.

.babelrc

{
  "presets": [
    "es2015"
  ],
  "env": {
    "development": {
      "plugins": [
        "babel-plugin-espower"
      ]
    }
  }
}

When I do this:

npm test

Expected

Fail test

Actual

Pass test :(


Reason

Application code is transformed to

"use strict";

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

var _powerAssertRecorder = function () { function PowerAssertRecorder() { this.captured = []; } PowerAssertRecorder.prototype._capt = function _capt(value, espath) { this.captured.push({ value: value, espath: espath }); return value; }; PowerAssertRecorder.prototype._expr = function _expr(value, source) { return { powerAssertContext: { value: value, events: this.captured }, source: source }; }; return PowerAssertRecorder; }();

exports.hello = hello;
// application code use `assert` module
var assert = require("assert");
function hello(name) {
    var _rec = new _powerAssertRecorder();

    assert(_rec._expr(_rec._capt(_rec._capt(typeof name === "undefined" ? "undefined" : _typeof(name), "arguments/0/left") === "string", "arguments/0"), {
        content: "assert(typeof name === \"string\")",
        filepath: "src/index.js",
        line: 4
    }));
    return "Hello " + name;
}
//# sourceMappingURL=index.js.map

_rec._expr return object.

    var assert = require("assert");
    var object = _rec._expr(_rec._capt(_rec._capt(typeof name === "undefined" ? "undefined" : _typeof(name), "arguments/0/left") === "string", "arguments/0"), {
        content: "assert(typeof name === \"string\")",
        filepath: "src/index.js",
        line: 4
    });
    assert(object);

So, assert(Object) never throw error. It causes this issue.

Do you have any solution?

Activity

  1. twada commented on Apr 12, 2016

    @twada
    Member

    @azu Thank you for reporting.
    Ah, that's a problem... One thing I can do is, to convert require('assert') to require('power-assert').
    I'll create a small babel plugin to do the transpilation.
    Thanks!

  2. twada commented on Apr 12, 2016

    @twada
    Member

    @azu Just created and published babel-plugin-empower-assert to solve this problem. Would you give it a try?

  3. twada commented on Apr 12, 2016

    @twada
    Member

    Made a pull-req for you.

  4. azu commented on Apr 12, 2016

    @azu
    MemberAuthor

    @twada Thanks. work fine.

    I think that this issue should be added to FAQ, because this solution is opt-in.
    If a user encounter this issue, the user see FAQ at first, I think.

  5. twada commented on Apr 12, 2016

    @twada
    Member

    I think that this issue should be added to FAQ, because this solution is opt-in.
    If a user encounter this issue, the use see FAQ at first, I think.

    Yeah exactly!

  6. twada commented on Apr 12, 2016

    @twada
    Member

    FYI: if you are using Babel, you can use babel-plugin-empower-assert to solve this problem.

    With babel-plugin-empower-assert, you can enhance normal assert with .babelrc settings below.

    {
      "presets": [
        . . .
      ],
      "env": {
        "development": {
          "plugins": [
            "babel-plugin-empower-assert",
            "babel-plugin-espower"
          ]
        }
      }
    }
  7. twada commented on Apr 12, 2016

    @twada
    Member
  8. azu commented on Apr 12, 2016

    @azu
    MemberAuthor

    Thanks again.

  9. twada commented on Apr 12, 2016

    @twada
    Member

    You're welcome ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions