Skip to content

Instantly share code, notes, and snippets.

@aslushnikov
Last active August 12, 2020 22:47
Show Gist options
  • Save aslushnikov/bc1a02d42734ffb8c639cd3fbe2e2aa4 to your computer and use it in GitHub Desktop.
Save aslushnikov/bc1a02d42734ffb8c639cd3fbe2e2aa4 to your computer and use it in GitHub Desktop.
function callMethod(methods, attributes, callback, methodName, methodValue) {
methods = new Map(methods);
methods.set(methodName, methodValue);
return createProxy({methods, attributes}, callback);
}
function setAttribute(methods, attributes, callback, attributeName) {
attributes = new Map(attributes);
attributes.set(attributeName, true);
return createProxy({methods, attributes}, callback);
}
function createProxy({methods, attributes}, callback) {
// We return a proxy over a function that will yield its state into callback, when
// invoked.
return new Proxy((...args) => callback(methods, attributes, args), {
get: (target, key) => {
// If someone accessed a method on the function, generate it and record
// arguments it's been called with.
if (methods.has(key))
return methodValue => callMethod(methods, attributes, callback, key, methodValue);
if (attributes.has(key) !== -1)
return setAttribute(methods, attributes, callback, key);
// Otherwise return whatever.
return target.get(key);
},
});
}
const it = createProxy({
methods: new Map([
['skip', false],
['fail', false],
['slow', false],
]),
attributes: new Map([['only', false]]),
}, (methodsCalled, attributesSet, [name, callback]) => {
console.log(methodsCalled);
console.log(attributesSet);
console.log(`name: ${name} callback: ${callback}`);
// Creating a test with the given settings:
//
// test = new Test(name, callback);
// test.setSkipped(methodsCalled.get('skip'));
// test.setFailed(methodsCalled.get('fail'));
// test.setFocused(attributesSet.has('only'));
});
it.skip(true).slow().fail(false).only('foo', () => {});
it.fail(true)('bar', () => { });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment