Back to the documentation index
This port follows the behavior of github/scientist as closely as JavaScript allows. This page lists every intentional difference.
Ruby's snake_case becomes camelCase, and predicate methods (?) lose their question mark.
| Ruby | JavaScript / TypeScript |
|---|---|
include Scientist / science "name" do |e| ... end |
class X extends Scientist (or withScience(Base)) / this.science("name", (e) => { ... }) |
Scientist.run "name" do ... end |
Scientist.run("name", (e) => { ... }), or the run / science functions |
include Scientist::Experiment |
class X extends Experiment |
Scientist::Default |
DefaultExperiment (alias Default) |
Scientist::Experiment.set_default(klass) |
Experiment.setDefault(klass); null restores the default |
Scientist::Experiment.new(name) (factory) |
Experiment.create(name) |
enabled? |
enabled() |
e.use { } / e.try("name") { } |
e.use(() => ...) / e.try("name", () => ...) |
before_run, after_run, run_if |
beforeRun, afterRun, runIf |
compare_errors |
compareErrors |
raise_with |
raiseWith |
raise_on_mismatches (class attribute) |
static Klass.raiseOnMismatches and instance raiseOnMismatches |
raise_on_mismatches? |
shouldRaiseOnMismatches() |
should_experiment_run? |
shouldExperimentRun() |
run_if_block_allows? |
runIfBlockAllows() |
observations_are_equivalent?(a, b) |
observationsAreEquivalent(a, b) |
ignore_mismatched_observation?(c, cand) |
ignoreMismatchedObservation(c, cand) |
generate_result(name) |
generateResult(name) |
clean_value(v) |
cleanValue(v) |
fabricate_durations_for_testing_purposes |
fabricateDurationsForTestingPurposes |
default_scientist_context |
defaultScientistContext() |
science "name", run: "x" |
science("name", configure, { run: "x" }) |
result.matched? / mismatched? / ignored? |
result.matched() / mismatched() / ignored() |
result.mismatched / result.ignored (arrays) |
result.mismatchedObservations / result.ignoredObservations |
result.experiment_name |
result.experimentName |
observation.raised? |
observation.raised (a boolean property) |
observation.cpu_time |
observation.cpuTime |
observation.cleaned_value |
observation.cleanedValue (a getter) |
observation.equivalent_to?(other, ...) |
observation.equivalentTo(other, ...) |
Scientist::Observation::RESCUES |
Observation.rescues (a predicate function) |
Scientist::Experiment::MismatchError#name |
MismatchError#experimentName |
Scientist::BadBehavior#name |
BadBehaviorError#behaviorName (also #experiment) |
BadBehavior, BehaviorMissing, BehaviorNotUnique, NoValue |
...Error classes, with the Ruby names exported as aliases |
raised(:publish, error) symbols |
raised("publish", error) strings: "clean", "compare", "enabled", "ignore", "publish", "run_if" |
- Behaviors must be functions. Ruby's
try/usetake a block. Passing anything other than a function touseortrythrows aTypeError. - No adding behaviors after a run. Ruby freezes the behaviors and raises
FrozenError. Hereuse/tryafter the experiment has run throw aTypeError. - No modifying the context after a run.
context(data)after the experiment has run throws aTypeError. Reading it withcontext()still works. - No auto-registration. Including
Scientist::Experimentin a Ruby class callsset_defaultautomatically. SubclassingExperimentdoes nothing: callExperiment.setDefault(MyExperiment)yourself. - No constructor block. Ruby's
MyExperiment.new("name") { |e| ... }doesn't exist; create the experiment, configure it, then callrun(name). See Multiple candidates. MismatchErroris a normalError. In Ruby it inherits fromException, so a barerescuedoesn't catch it. JavaScript has no such split, so acatchin the code under test will catch it.- Static
raiseOnMismatchesis inherited. SettingMyExperiment.raiseOnMismatches = truealso affects subclasses ofMyExperimentthat don't set their own value. There is also an instance property that overrides the class value. - Fabricated durations. An entry without a CPU time reports
cpuTimeas0(Ruby:nil). BothcpuTimeandcpu_timekeys are accepted, and a bare number sets the duration. - No
Exception/StandardErrorsplit. Anything can be thrown in JavaScript, and everything is captured by default.Observation.rescuesis a predicate rather than a list of classes. - Default comparison. Ruby uses
==; this port usesdefaultEquals, a structural comparison of arrays, plain objects,Map,Set,Date,RegExpand typed arrays, anequals()method when present, and identity for other objects. - Error comparison. Two thrown values are equivalent by default when they have the same prototype and the same
message, the closest analogue of Ruby's class-and-message comparison. - Truthiness. Results of
enabled,runIf,compare,compareErrorsandignoreare interpreted with JavaScript truthiness:0,""andNaNare false here, but true in Ruby. - CPU time. Measured with
process.cpuUsage()where available (Node.js). It is0in browsers and other runtimes without it. Because it is process CPU time, in async experiments it also includes other work interleaved on the event loop while a behavior awaits. - Async support. Everything about promises is new in this port:
runAsync,scienceAsync,Scientist.runAsync, and the rules for promises in synchronous runs. See Async. NoValueErroris exported for parity, but never thrown.