5.3.3 • Published 3 years ago

comment-to-assert v5.3.3

Weekly downloads
17
License
MIT
Repository
github
Last release
3 years ago

comment-to-assert

Convert comment to assert function.

const foo = 1;
foo;// => 1

Convert this to:

const foo = 1;
assert.strictEqual(foo, 1);

Syntax

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

Installation

npm install comment-to-assert

CLI Installation

npm install -g comment-to-assert
comment-to-assert target.js > modify.js

Usage

toAssertFromSource(source : string, options: toAssertFromSourceOptions): string

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:

interface toAssertFromSourceOptions {
    babel?: {
        plugins: string[];
    };
}

toAssertFromAST(AST : object, options: toAssertFromASTOptions): object

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 assertion
  • assertAfterCallbackName: 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

Example

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);'

Tests

npm test

Update snapshots if you need.

npm run updateSnapshot

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

MIT

5.3.3

3 years ago

6.0.0-beta.1

3 years ago

5.3.1

4 years ago

5.3.0

5 years ago

5.2.0

6 years ago

5.0.1

7 years ago

5.0.0

7 years ago

4.0.0

7 years ago

3.3.0

7 years ago

3.0.0

7 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.5.1

9 years ago

1.5.0

9 years ago

1.4.0

10 years ago

1.3.0

10 years ago

1.2.0

10 years ago

1.1.0

10 years ago

1.0.4

11 years ago

1.0.3

11 years ago

1.0.2

11 years ago

1.0.1

11 years ago