Convert comment to assert
function.
const foo = 1;
foo;// => 1
Convert this to:
const foo = 1;
assert.strictEqual(foo, 1);
This library support following format.
expression; // => expected value
or
console.log(expression); // => expected value
Special handling:
Error:
throw new Error("message"); // Error: "message"
Promise:
Promise.resolve(1); // => Resolve: 1
Promise.reject(new Error("message")); // => Reject: message
npm install comment-to-assert
npm install -g comment-to-assert
comment-to-assert target.js > modify.js
Return string that transformed source string of arguments.
import {
toAssertFromSource,
toAssertFromAST
} from "comment-to-assert"
toAssertFromSource("1;// => 1");// => "assert.equal(1, 1)"
toAssertFromSource
only support transform source code.
if want to source map, should use toAssertFromAST
with own parser and generator.
Options:
babel
: @babel/core option
interface toAssertFromSourceOptions {
babel?: {
plugins: string[];
};
}
Return AST object that transformed AST of arguments.
var AST = parse(`var a = [1];
a;// => [1]`);
var resultOfAST = toAssertFromAST(AST);
generate(resultOfAST);
/*
var a = [1];
assert.deepEqual(a, [1]);
*/
Options:
assertBeforeCallbackName
: callback name before assertionassertAfterCallbackName
: callback name after assertion
export interface toAssertFromASTOptions {
assertBeforeCallbackName?: string;
assertAfterCallbackName?: string;
}
1; // => 1
"str"; // => "str"
[1, 2, 3]; // => [1,2,3]
Promise.resolve(1); // => Resolve: 1
to be
beforeCallback("id:0");
assert.strictEqual(1, 1);
afterCallback("id:0");
// => 1
beforeCallback("id:1");
assert.strictEqual("str", "str");
afterCallback("id:1");
// => "str"
beforeCallback("id:2");
assert.deepStrictEqual([1, 2, 3], [1, 2, 3]);
afterCallback("id:2");
// => [1,2,3]
Promise.resolve(Promise.resolve(1)).then(v => {
beforeCallback("id:3");
assert.strictEqual(v, 1);
afterCallback("id:3");
return v;
}); // => Resolve: 1
See example/
"use strict";
var assert = require("assert");
var toAssertFromSource = require("comment-to-assert").toAssertFromSource;
toAssertFromSource("1;// => 1");// => 'assert.equal(1, 1);'
toAssertFromSource("[1];// => [1]");// => 'assert.deepEqual([1], [1]);'
toAssertFromSource("var foo=1;foo;// => 1");// => 'var foo = 1;\nassert.equal(foo, 1);'
npm test
Update snapshots if you need.
npm run updateSnapshot
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
MIT