Skip to content

Commit

Permalink
fix 84
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzofox3 committed Apr 11, 2021
1 parent 6adfba2 commit 1cd84ac
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
43 changes: 24 additions & 19 deletions src/assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ const aliasMethodHook = (methodName: string) => function (...args) {
return this[methodName](...args);
};

const unbindAssert = (target: Assert): { [p: string]: (...args) => any } => Object.fromEntries(
Object
.keys(AssertPrototype)
.map((methodName) => [methodName, (...args) => target[methodName](...args)])
);

export const AssertPrototype = {
equal: assertMethodHook((actual, expected, description = 'should be equivalent') => ({
pass: equal(actual, expected),
Expand Down Expand Up @@ -162,24 +168,23 @@ export const assert = (collect, offset: number, runOnly = false): Assert => {
return test(description, spec, Object.assign({}, opts, {skip: true}));
};

return Object.assign(
Object.create(AssertPrototype, {collect: {value: actualCollect}}),
{
test(description, spec, opts = {}) {
if (runOnly) {
return skip(description, spec, opts);
}
return test(description, spec, opts);
},
skip(description: string, spec = noop, opts = {}) {
return skip(description, spec, opts);
},
only(description: string, spec, opts = {}) {
const specFn = runOnly === false ? _ => {
throw new Error(`Can not use "only" method when not in run only mode`);
} : spec;
return test(description, specFn, opts);
}
// @ts-ignore
return {
...unbindAssert(Object.create(AssertPrototype, {collect: {value: actualCollect}})),
test(description, spec, opts = {}) {
if (runOnly) {
return skip(description, spec, opts);
}
);
return test(description, spec, opts);
},
skip(description: string, spec = noop, opts = {}) {
return skip(description, spec, opts);
},
only(description: string, spec, opts = {}) {
const specFn = runOnly === false ? _ => {
throw new Error(`Can not use "only" method when not in run only mode`);
} : spec;
return test(description, specFn, opts);
}
};
};
15 changes: 13 additions & 2 deletions test/unit/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,6 @@ test('doesNotThrow operator: expected (ignored)', t => {
});

test('extend assertion library', t => {
const result = [];
const a = assert(item => result.push(item), 0);

AssertPrototype.isFoo = function (value, description = 'should be "foo"') {
const result = {
Expand All @@ -409,6 +407,9 @@ test('extend assertion library', t => {
return this.collect(result);
};

const result = [];
const a = assert(item => result.push(item), 0);

const r = a.isFoo('foo');

t.equal(result.length, 1, 'should have collected the assertion');
Expand Down Expand Up @@ -447,4 +448,14 @@ test('extend assertion library with failing assertion: should set the "at" prope
t.end();
});

test('destructure assertion object', (t) =>{
const result = [];
const {eq} = assert(item => result.push(item), 0);
const r = eq('foo','bar');
t.equal(result.length, 1, 'should have collected the assertion result');
t.equal(r.pass, false);
t.ok(r.at);
t.end()
});


0 comments on commit 1cd84ac

Please sign in to comment.