workshopper/workshopper

How does this work?

alayek opened this issue · 1 comments

Hi, I would like to know how workshopper module works. I checked the promise-it-won't-hurt nodeschool exercise, and it seems to me that workshopper takes the student's submission, and solution provided - then compares both.

Is there any way for someone to use this workshopper module without providing solutions directly, and rather just provide test cases for expected solution in, say, Mocha and Chai JS?

I know it's not an issue, but I could not find any mailing list. Apologies for posting it under issues.

Hey @alayek,

I only understand enough of workshopper to get by, but in that case that's enough. I'm writing a D3 workshopper, using mocha and chai to test the solutions (+ ES6, jsdom and D3, but that's less relevant).

You can have a look at the relevant code here: https://github.com/ThibWeb/testdrived3/blob/master/src/lib/runner.js#L47

This comes down to:

  1. Running the test in a addSetup hook, and capturing the failures:
    exercise.addSetup(function(mode, callback) {
        const mocha = new Mocha();
        mocha.addFile(path.join(this.dir, 'spec.js'));

        runFunction(global.submission);

        mocha.run((failures) => {
            this.failures = failures;
            callback(null, true);
        });
    });
  1. Checking if there are failures in the addVerifyProcessor hook, and emit pass or fail accordingly.
    exercise.addVerifyProcessor(function(callback) {
        const i18n = exercise.__.bind(exercise);
        const passed = this.failures === 0;

        if (passed) {
            this.emit('pass', i18n('test.pass'));
        } else {
            this.emit('fail', i18n('test.fail'));
        }

        callback(null, passed);
    });

I have no idea if those are the proper hooks for this scenario, and I'm passing a global variable around that contains the user's solution, but hey it works! I stumbled upon this pattern while creating a PR for a React workshopper: https://github.com/asbjornenge/thinking-in-react.

I intend to extract a "jsdom ES6 mocha workshopper" boilerplate from this code once I'm done with it.

Edit: done! The boilerplate is available at https://github.com/ThibWeb/workshopper-mocha-boilerplate.