Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
azu opened this issue Apr 12, 2016 · 9 comments
Closed

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

azu opened this issue Apr 12, 2016 · 9 comments

Comments

@azu
Copy link
Member

azu commented Apr 12, 2016

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?

@twada
Copy link
Member

twada commented Apr 12, 2016

@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!

@twada
Copy link
Member

twada commented Apr 12, 2016

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

@twada
Copy link
Member

twada commented Apr 12, 2016

Made a pull-req for you.

@azu
Copy link
Member Author

azu commented Apr 12, 2016

@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.

@twada
Copy link
Member

twada commented Apr 12, 2016

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!

@twada
Copy link
Member

twada commented Apr 12, 2016

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"
      ]
    }
  }
}

@twada
Copy link
Member

twada commented Apr 12, 2016

Added to FAQ as How to deal with assert calls in production.

@azu
Copy link
Member Author

azu commented Apr 12, 2016

Thanks again.

@azu azu closed this as completed Apr 12, 2016
@twada
Copy link
Member

twada commented Apr 12, 2016

You're welcome ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants