Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

baddsert

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

baddsert - npm Package Compare versions

Comparing version 0.3.0 to 0.4.1

.baselines/dogfood.js

119

badd.ts

@@ -7,2 +7,4 @@ 'use strict';

import * as debug from 'debug';
import {get as stackGet} from 'stack-trace';
import {sep as pathSeparator} from 'path';

@@ -27,70 +29,79 @@ let sillyLog = debug('baddsert:silly');

// Some init thing
// Gets a label, creates a file named that in the above folder
// returns baddsert
// DI lets us inject mock stored results in tests
export let baddsertInject = getStoredResults => {
return (filename: string): Function => {
let stored = getStoredResults(filename);
let baselines = {};
return (label: string, data: any, comparator?: IComparator): void => {
let keys = getKey();
keys.push(label);
return (label: string, data: any, comparator?: IComparator): void => {
sillyLog(`Label: ${label}, data: ${data}`);
let filename = stackGet()[1]
.getFileName()
.split(pathSeparator)
.pop()
.slice(0, -3);
// Mreh. TODO: Better way to handle symbols
if (typeof data === 'symbol') {
data = data.toString();
console.warn(`Symbol matching is only supported via .toString(): ${data}`);
}
if (!baselines[filename]) {
baselines[filename] = getStoredResults(filename);
}
let refObj = <IReference>get(stored, keys);
let stored = baselines[filename];
let keys = getKey();
keys.push(label);
if (refObj && refObj.hasOwnProperty('reference')) {
sillyLog(`Asserting ${refObj.reference} is equal to ${data}`);
// We have a previous result
if (comparator) {
// Use the provided comparison tool
let result;
try {
result = comparator(refObj.reference, data);
} catch (e) {
result = false;
}
// Mreh. TODO: Better way to handle symbols
if (typeof data === 'symbol') {
data = data.toString();
console.warn(`Symbol matching is only supported via .toString(): ${data}`);
}
if (!result) {
set(stored, [...keys, 'current'], data);
throw new Error(`${label}: Expected '${maybeToString(data)}' to equal '${refObj.reference}'.`);
}
} else {
// Use default
try {
deepStrictEqual(refObj.reference, data);
} catch (e) {
refObj.current = data;
throw new Error(`${label}: Expected '${maybeToString(data)}' to equal '${refObj.reference}'.`);
}
let refObj = <IReference>get(stored, keys);
sillyLog('Got refObj:', refObj);
if (refObj && refObj.hasOwnProperty('reference')) {
sillyLog(`Asserting ${refObj.reference} is equal to ${data}`);
// We have a previous result
if (comparator) {
// Use the provided comparison tool
let result;
try {
result = comparator(refObj.reference, data);
} catch (e) {
result = false;
}
} else if (refObj && refObj.hasOwnProperty('current')) {
// User has not given us a reference value but is trying to assert against the current value
// TODO: have two modes, strict and lax
console.warn('Attempting to assert value without setting reference, using previous value:', refObj.current);
if (!result) {
set(stored, [...keys, 'current'], data);
throw new Error(`${label}: Expected '${maybeToString(data)}' to equal '${refObj.reference}'.`);
}
} else {
// Use default
try {
deepStrictEqual(refObj.current, data);
deepStrictEqual(refObj.reference, data);
} catch (e) {
let prevCurrent = refObj.current;
refObj.current = data;
throw new Error(`${label}: Expected '${maybeToString(data)}' to equal '${prevCurrent}'.`);
throw new Error(`${label}: Expected '${maybeToString(data)}' to equal '${refObj.reference}'.`);
}
} else {
// We don't have it, assume correct
infoLog(`Making new entry for ${label}, populated with ${data}`);
set(stored, keys, {
_meta: {
type: typeof data
},
current: data
});
}
};
} else if (refObj && refObj.hasOwnProperty('current')) {
// User has not given us a reference value but is trying to assert against the current value
// TODO: have two modes, strict and lax
console.warn('Attempting to assert value without setting reference, using previous value:', refObj.current);
try {
deepStrictEqual(refObj.current, data);
} catch (e) {
let prevCurrent = refObj.current;
refObj.current = data;
throw new Error(`${label}: Expected '${maybeToString(data)}' to equal '${prevCurrent}'.`);
}
} else {
// We don't have it, assume correct
infoLog(`Making new entry for ${label}, populated with ${data}`);
set(stored, keys, {
_meta: {
type: typeof data
},
current: data
});
}
};
};

@@ -11,2 +11,3 @@ 'use strict';

import * as colors from 'colors';
import {spawn} from 'child_process';

@@ -17,3 +18,3 @@ // Can I use left-pad here? Absolutely!

let leftLog = (spaces: number, ...str: string[]): void => {
let spacing = new Array((spaces * 2) + 1).join(' ');
let spacing = new Array((spaces * 2)).join(' ');
console.log(spacing, ...str);

@@ -62,3 +63,3 @@ };

} catch (e) {
leftLog(depth, colors.red(`X ${subKey}: AGH THEY DON'T MATCH ${colors.trap('DOOOOOOOM')}`));
leftLog(depth, colors.red(`X ${subKey}: AGH THEY DON'T MATCH ${colors.zalgo('DOOOOOOOM')}`));
// Not using template functions here so we're not calling .toString on objects

@@ -75,12 +76,16 @@ leftLog(depth + 1, 'Reference value:', resultSet.reference);

let tests = spawn('npm', ['test', '-s'], {stdio: 'inherit'});
Object.keys(allResults)
.forEach(key => {
tests.on('close', (code) => {
console.log('--- BADD baselines ---');
Object.keys(allResults)
.forEach(key => {
let resultSet = allResults[key];
check(resultSet, key);
let resultSet = allResults[key];
check(resultSet, key);
// Always save
// Can figure out dirty check if it's worth it.
save(key, resultSet);
// Always save
// Can figure out dirty check if it's worth it.
save(key, resultSet);
});
});

@@ -9,2 +9,2 @@ export interface IComparator {

}
export declare let baddsertInject: (getStoredResults: any) => (filename: string) => Function;
export declare let baddsertInject: (getStoredResults: any) => (label: string, data: any, comparator?: IComparator) => void;

@@ -6,2 +6,4 @@ 'use strict';

var debug = require('debug');
var stack_trace_1 = require('stack-trace');
var path_1 = require('path');
var sillyLog = debug('baddsert:silly');

@@ -15,70 +17,77 @@ var infoLog = debug('baddsert:info');

};
// Some init thing
// Gets a label, creates a file named that in the above folder
// returns baddsert
// DI lets us inject mock stored results in tests
exports.baddsertInject = function (getStoredResults) {
return function (filename) {
var stored = getStoredResults(filename);
return function (label, data, comparator) {
var keys = mochaInject_1.getKey();
keys.push(label);
// Mreh. TODO: Better way to handle symbols
if (typeof data === 'symbol') {
data = data.toString();
console.warn("Symbol matching is only supported via .toString(): " + data);
}
var refObj = object_path_1.get(stored, keys);
if (refObj && refObj.hasOwnProperty('reference')) {
sillyLog("Asserting " + refObj.reference + " is equal to " + data);
// We have a previous result
if (comparator) {
// Use the provided comparison tool
var result = void 0;
try {
result = comparator(refObj.reference, data);
}
catch (e) {
result = false;
}
if (!result) {
object_path_1.set(stored, keys.concat(['current']), data);
throw new Error(label + ": Expected '" + maybeToString(data) + "' to equal '" + refObj.reference + "'.");
}
var baselines = {};
return function (label, data, comparator) {
sillyLog("Label: " + label + ", data: " + data);
var filename = stack_trace_1.get()[1]
.getFileName()
.split(path_1.sep)
.pop()
.slice(0, -3);
if (!baselines[filename]) {
baselines[filename] = getStoredResults(filename);
}
var stored = baselines[filename];
var keys = mochaInject_1.getKey();
keys.push(label);
// Mreh. TODO: Better way to handle symbols
if (typeof data === 'symbol') {
data = data.toString();
console.warn("Symbol matching is only supported via .toString(): " + data);
}
var refObj = object_path_1.get(stored, keys);
sillyLog('Got refObj:', refObj);
if (refObj && refObj.hasOwnProperty('reference')) {
sillyLog("Asserting " + refObj.reference + " is equal to " + data);
// We have a previous result
if (comparator) {
// Use the provided comparison tool
var result = void 0;
try {
result = comparator(refObj.reference, data);
}
else {
// Use default
try {
assert_1.deepStrictEqual(refObj.reference, data);
}
catch (e) {
refObj.current = data;
throw new Error(label + ": Expected '" + maybeToString(data) + "' to equal '" + refObj.reference + "'.");
}
catch (e) {
result = false;
}
if (!result) {
object_path_1.set(stored, keys.concat(['current']), data);
throw new Error(label + ": Expected '" + maybeToString(data) + "' to equal '" + refObj.reference + "'.");
}
}
else if (refObj && refObj.hasOwnProperty('current')) {
// User has not given us a reference value but is trying to assert against the current value
// TODO: have two modes, strict and lax
console.warn('Attempting to assert value without setting reference, using previous value:', refObj.current);
else {
// Use default
try {
assert_1.deepStrictEqual(refObj.current, data);
assert_1.deepStrictEqual(refObj.reference, data);
}
catch (e) {
var prevCurrent = refObj.current;
refObj.current = data;
throw new Error(label + ": Expected '" + maybeToString(data) + "' to equal '" + prevCurrent + "'.");
throw new Error(label + ": Expected '" + maybeToString(data) + "' to equal '" + refObj.reference + "'.");
}
}
else {
// We don't have it, assume correct
infoLog("Making new entry for " + label + ", populated with " + data);
object_path_1.set(stored, keys, {
_meta: {
type: typeof data
},
current: data
});
}
else if (refObj && refObj.hasOwnProperty('current')) {
// User has not given us a reference value but is trying to assert against the current value
// TODO: have two modes, strict and lax
console.warn('Attempting to assert value without setting reference, using previous value:', refObj.current);
try {
assert_1.deepStrictEqual(refObj.current, data);
}
};
catch (e) {
var prevCurrent = refObj.current;
refObj.current = data;
throw new Error(label + ": Expected '" + maybeToString(data) + "' to equal '" + prevCurrent + "'.");
}
}
else {
// We don't have it, assume correct
infoLog("Making new entry for " + label + ", populated with " + data);
object_path_1.set(stored, keys, {
_meta: {
type: typeof data
},
current: data
});
}
};
};

@@ -9,2 +9,3 @@ 'use strict';

var colors = require('colors');
var child_process_1 = require('child_process');
// Can I use left-pad here? Absolutely!

@@ -18,3 +19,3 @@ // Would that be a smarter idea in general? Yes!

}
var spacing = new Array((spaces * 2) + 1).join(' ');
var spacing = new Array((spaces * 2)).join(' ');
console.log.apply(console, [spacing].concat(str));

@@ -58,3 +59,3 @@ };

catch (e) {
leftLog(depth, colors.red("X " + subKey + ": AGH THEY DON'T MATCH " + colors.trap('DOOOOOOOM')));
leftLog(depth, colors.red("X " + subKey + ": AGH THEY DON'T MATCH " + colors.zalgo('DOOOOOOOM')));
// Not using template functions here so we're not calling .toString on objects

@@ -70,9 +71,13 @@ leftLog(depth + 1, 'Reference value:', resultSet.reference);

};
Object.keys(allResults)
.forEach(function (key) {
var resultSet = allResults[key];
check(resultSet, key);
// Always save
// Can figure out dirty check if it's worth it.
badd_fs_1.save(key, resultSet);
var tests = child_process_1.spawn('npm', ['test', '-s'], { stdio: 'inherit' });
tests.on('close', function (code) {
console.log('--- BADD baselines ---');
Object.keys(allResults)
.forEach(function (key) {
var resultSet = allResults[key];
check(resultSet, key);
// Always save
// Can figure out dirty check if it's worth it.
badd_fs_1.save(key, resultSet);
});
});

@@ -1,1 +0,2 @@

export declare let baddsert: (filename: string) => Function;
import * as badd from './badd';
export declare let baddsert: (label: string, data: any, comparator?: badd.IComparator) => void;
'use strict';
var badd_1 = require('./badd');
var badd = require('./badd');
var badd_fs_1 = require('./badd-fs');
exports.baddsert = badd_1.baddsertInject(badd_fs_1.getStoredResults);
exports.baddsert = badd.baddsertInject(badd_fs_1.getStoredResults);

@@ -7,3 +7,2 @@ 'use strict';

var index_1 = require('../index');
var realB = index_1.baddsert('dogfood-basic');
describe('dogfooding', function () {

@@ -14,7 +13,7 @@ var mockStore;

mockStore = {};
baddsert = badd_1.baddsertInject(function () { return mockStore; })('test');
baddsert = badd_1.baddsertInject(function () { return mockStore; });
});
it('creates new items if not found', function () {
baddsert('some pants', 'llama');
realB('basic assert', mockStore);
index_1.baddsert('basic assert', mockStore);
});

@@ -30,2 +29,3 @@ it('checks against old data and does fine', function () {

baddsert('pants', 'pants');
index_1.baddsert('no mutate', mockStore);
});

@@ -46,5 +46,5 @@ it('should throw if it finds something that does not match', function () {

// pants: Expected pants to equal super llama.
realB('throw check', e.message);
index_1.baddsert('throw check', e.message);
}
realB('throw check did throw', didThrow);
index_1.baddsert('throw check did throw', didThrow);
});

@@ -59,10 +59,10 @@ it('should reject if passed a different falsy value', function () {

catch (e) {
// pants: Expected pants to equal super llama.
realB('falsy reject', e.message);
// falsy reject: Expected 'undefined' to equal 'false'
index_1.baddsert('falsy reject', e.message);
}
realB('falsy reject did throw', didThrow);
index_1.baddsert('falsy reject did throw', didThrow);
});
it('should persist an undefined item', function () {
// No check, just reading test output, yeaaaahhh...
realB('undef', undefined);
index_1.baddsert('undef', undefined);
});

@@ -74,4 +74,4 @@ it('allows the user to pass a different comparator', function () {

baddsert('diff comparator', 'moosle', function (a, b) {
realB('comparator a', a);
realB('comparator b', b);
index_1.baddsert('comparator a', a);
index_1.baddsert('comparator b', b);
return false;

@@ -82,6 +82,6 @@ });

catch (e) {
realB('comparator reject', e.message);
index_1.baddsert('comparator reject', e.message);
}
realB('comparator didThrow', didThrow);
index_1.baddsert('comparator didThrow', didThrow);
});
});

@@ -7,3 +7,2 @@ 'use strict';

var mochaInject_1 = require('../mochaInject');
var injected = index_1.baddsert('injected');
mochaInject_1.inject();

@@ -13,8 +12,8 @@ describe('root describe', function () {

it('works inside nesting', function () {
injected('nest', true);
index_1.baddsert('nest', true);
});
});
it('works with a single layer of nesting', function () {
injected('single', true);
index_1.baddsert('single', true);
});
});

@@ -13,3 +13,3 @@ 'use strict';

mockStore = {};
baddsert = badd_1.baddsertInject(function () { return mockStore; })('test');
baddsert = badd_1.baddsertInject(function () { return mockStore; });
});

@@ -16,0 +16,0 @@ it('creates new items if not found', function () {

'use strict';
import {baddsertInject} from './badd';
import * as badd from './badd';
import {getStoredResults} from './badd-fs';
export let baddsert = baddsertInject(getStoredResults);
export let baddsert = badd.baddsertInject(getStoredResults);
{
"name": "baddsert",
"version": "0.3.0",
"version": "0.4.1",
"description": "Baseline Acceptance Driven Development for JavaScript",

@@ -35,2 +35,3 @@ "main": "dist/index.js",

"readline-sync": "^1.4.1",
"stack-trace": "0.0.9",
"typescript": "^1.8.9"

@@ -37,0 +38,0 @@ },

@@ -15,13 +15,13 @@ # baddsert

baddsert replaces your old assert library. Write your tests as usual, using baddsert as the assert. Step one is to init the whole shebang:
baddsert replaces your old assert library. Write your tests as usual, using baddsert as the assert. Step one is to import the whole shebang:
```typescript
// docTests.js
import {baddsert} from 'baddsert';
let docTests = baddsert('docTests');
```
You'll need to init once per file. Try and make it descriptive of that particular test battery.
`baddsert` will group the baselines by the filename of the calling file, so make it descriptive!
Now that everything's all init-ifyed, let's use baddsert:
Now that everything's all set up, let's use baddsert:

@@ -31,3 +31,3 @@ ```typescript

let result = hammertime(`can't touch this`);
docTests('I am a steg-o-saurus', result);
baddsert('I am a steg-o-saurus', result);
});

@@ -38,19 +38,52 @@ ```

The second is the thing you want asserted.
The second is the value you want checked against the stored baseline value. If there isn't a stored baseline value, you'll need to approve one (see [CLI section](#CLI) below).
There's an optional third param that allows you to define your own equality. Otherwise baddsert will just use `deepStrictEqual` from Node's assert package.
There's an optional third param that allows you to define your own equality. Otherwise baddsert will just use [`deepStrictEqual`](https://nodejs.org/api/assert.html#assert_assert_deepequal_actual_expected_message) from Node's assert package.
And now, the magic happens. When you run your tests, baddsert will take the result from the first test and save it under the `badd-baseline` directory (in this case, as the file `docTests`). Future runs will throw if the value passed in is not `deepStrictEqual` to the original one.
And now, the magic happens. When you run your tests, baddsert will take the result from the first test run and save it under the `.baselines` directory (in this case, as the file `docTests.js`). Future runs will throw if the value passed in is not `deepStrictEqual` to the original one.
When you inevitably change something that makes the tests fail (because your function is correctly returning a new value) run `baddsert` in the same dir as your `badd-baseline` directory. This will run through all of your asserts, letting you replace the old data with the data that was passed in during the failing test.
### CLI
When you inevitably change something that makes the tests fail (because your function is correctly returning a new value) run `baddsert` in your root directory. This will run through all of your asserts, letting you replace the old data with the data that was passed in during the failing test.
The CLI will run `npm test` before cycling through the baselines. There are three cases:
##### The provided value matched the baseline value
All good, no user input required.
```
--- Checking docTests ---
I am a steg-o-sarus: AGH THEY DON'T MATCH DOOOOOOOM
Stored result: result
Latest: NEW SWEET DATAS
--- BADD baselines ---
- dogfood.js
✓ I am a steg-o-sarus
```
##### The provided value did not match
The CLI will allow you to overwrite the baseline value with the new value.
```
--- BADD baselines ---
- dogfood.js
X I am a steg-o-sarus: AGH THEY DON'T MATCH D̸̛̟͈̻̥̦̝͚̬̤͉̪͎̬̺͚̩ͭO̷̸̢̪̞̺̝͈̪̱̭͚̩̭̩̪̗͇̪O̡̮͍͖̞͙͓͔̟̺̬͖̳͍̟̬̮̮̰Ȍͫ̆̔͌̿ͥͣ̀̾̚ ҉͔͔̘͖̪͙̀O̵͚̥͉̠̱̱̱̥̥̹̳͈̼̞͚̖̟̝O̷̼̟͉̜͔̥͉̞̲̠̼̩̳̠̖̝̺͔O̠̳̤̹̟͓̦̖͔̰͍̳͎͕͈̣̻͖̻Ǫ̩͙̦̹̖̫̣̣̩̣͈͇͈̗̜̙̦̙̘M̸̤̯̬̗̳̼̰͖͉̯͚̜̠̱̘͕͙͔
Reference value: pants: Expected 'pants' to equal 'super duper llama'.
Latest result: pants: Expected 'pants' to equal 'super llama'.
Should I replace this? [y/n] :
```
##### A value was provided but there was no baseline to compare it to
In order to avoid accidentally using an incorrect value as the baseline, all new values must be approved by the operator.
```
--- BADD baselines ---
- dogfood.js
X throw check: No reference value found
Latest result: pants: Expected 'pants' to equal 'super llama'.
Use this value? [y/n] :
```
### Installation
Install the baddsert cli globally:

@@ -57,0 +90,0 @@

@@ -10,4 +10,2 @@ 'use strict';

let realB = realBadd('dogfood-basic');
describe('dogfooding', () => {

@@ -18,3 +16,3 @@ let mockStore;

mockStore = {};
baddsert = baddsertInject(() => mockStore)('test');
baddsert = baddsertInject(() => mockStore);
});

@@ -24,3 +22,3 @@

baddsert('some pants', 'llama');
realB('basic assert', mockStore);
realBadd('basic assert', mockStore);
});

@@ -37,2 +35,3 @@

baddsert('pants', 'pants');
realBadd('no mutate', mockStore);
});

@@ -53,5 +52,5 @@

// pants: Expected pants to equal super llama.
realB('throw check', e.message);
realBadd('throw check', e.message);
}
realB('throw check did throw', didThrow);
realBadd('throw check did throw', didThrow);
});

@@ -66,6 +65,6 @@

} catch (e) {
// pants: Expected pants to equal super llama.
realB('falsy reject', e.message);
// falsy reject: Expected 'undefined' to equal 'false'
realBadd('falsy reject', e.message);
}
realB('falsy reject did throw', didThrow);
realBadd('falsy reject did throw', didThrow);
});

@@ -75,3 +74,3 @@

// No check, just reading test output, yeaaaahhh...
realB('undef', undefined);
realBadd('undef', undefined);
});

@@ -84,4 +83,4 @@

baddsert('diff comparator', 'moosle', (a, b) => {
realB('comparator a', a);
realB('comparator b', b);
realBadd('comparator a', a);
realBadd('comparator b', b);

@@ -92,6 +91,6 @@ return false;

} catch (e) {
realB('comparator reject', e.message);
realBadd('comparator reject', e.message);
}
realB('comparator didThrow', didThrow);
realBadd('comparator didThrow', didThrow);
});
});

@@ -10,4 +10,2 @@ 'use strict';

let injected = baddsert('injected');
inject();

@@ -18,3 +16,3 @@

it('works inside nesting', () => {
injected('nest', true);
baddsert('nest', true);
});

@@ -24,4 +22,4 @@ });

it('works with a single layer of nesting', () => {
injected('single', true);
baddsert('single', true);
});
});

@@ -17,3 +17,3 @@ 'use strict';

mockStore = {};
baddsert = baddsertInject(() => mockStore)('test');
baddsert = baddsertInject(() => mockStore);
});

@@ -20,0 +20,0 @@

@@ -12,4 +12,5 @@ {

"node": "registry:dt/node#4.0.0+20160412142033",
"object-path": "registry:dt/object-path#0.9.0+20160317120654"
"object-path": "registry:dt/object-path#0.9.0+20160317120654",
"stack-trace": "registry:dt/stack-trace#0.0.0+20160316155526"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc