Repository navigation
power-assert make assert() of assert module silent #43
Copy link
Copy link
Closed
Description
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?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
twada commentedon Apr 12, 2016
@azu Thank you for reporting.
Ah, that's a problem... One thing I can do is, to convert
require('assert')torequire('power-assert').I'll create a small babel plugin to do the transpilation.
Thanks!
twada commentedon Apr 12, 2016
@azu Just created and published babel-plugin-empower-assert to solve this problem. Would you give it a try?
twada commentedon Apr 12, 2016
Made a pull-req for you.
azu commentedon 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 commentedon Apr 12, 2016
Yeah exactly!
twada commentedon 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
assertwith.babelrcsettings below.twada commentedon Apr 12, 2016
Added to FAQ as How to deal with
assertcalls in production.azu commentedon Apr 12, 2016
Thanks again.
twada commentedon Apr 12, 2016
You're welcome ;)