MithrilJS / ospec

Noiseless testing framework
MIT License
48 stars 13 forks source link

`o.before()` within a `o.spec` still runs even when no tests are running #61

Open gilbert opened 1 year ago

gilbert commented 1 year ago
const o = require('ospec')

o.spec('Example', () => {
  o.before(() => {
    console.log("RUNNING BEFORE")
  })

  o('test1', () => {
    o(true).equals(true)
  })
})

o.only('test2', () => {
  o(true).equals(true)
})

In this example, the console log runs even though no test in that spec is running (due to the .only on test2). In my own project this is causing extra debug statements to show up in the console even though they're not relevant to the test I'm focusing on.

pygy commented 1 year ago

Hi @gilbert thanks for the report. This should be fixable, I'll look into a solution.

jonahx commented 1 year ago

I ran into this myself. Has there been any progress? Not familiar with ospec codebase, but I might be able to find time to look into this if you don't have time @pygy. Let me know if you have any ideas for the solution already.

jonahx commented 1 year ago

May be other considerations I am missing, but updating this line to the following seems to work:

only.length === 0 ||
    Object.values(spec.children).some(
        x => x instanceof Task && only.includes(x.fn)
    ) ? spec.before : [],

Could use Set for performance but since the number of only is typically 1 or small, it shouldn't matter for most use cases.

gilbert commented 10 months ago

Any chance we can get @jonahx's fix into a new release? 👀