New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
before/beforeEach/after/afterEach #59
Comments
var test = require("tape")
var before = test
var after = test
var test = beforeEach(test, function before(assert) {
// called before each thing
// when done call
assert.end()
})
test = afterEach(test, function after(assert) {
// called after each thing
// when done call
assert.end()
})
before("before all tests", function (assert) {
// before logic
// when done call
assert.end()
})
test("name of test", function (assert) {
// before logic & beforeEach has run
// when done with test call
assert.end()
// afterEach logic will run
})
after("after all tests", function (assert) {
// after logic
// when done call
assert.end()
})
function beforeEach(test, handler) {
return function tapish(name, listener) {
test(name, function (assert) {
var _end = assert.end
assert.end = function () {
assert.end = _end
listener(assert)
}
handler(assert)
})
}
}
function afterEach(test, handler) {
return function tapish(name, listener) {
test(name, function (assert) {
var _end = assert.end
assert.end = function () {
assert.end = _end
handler(assert)
}
listener(assert)
})
}
} We should put |
For
to perform some setup around each test, you can just write a function inline that takes a callback. |
Thanks guys for this very quick follow up. I'll give a try this way. 🍰 |
@substack I'm having trouble setting up asynchronous beforeEach/afterEach. edit: nvm figured it out |
@MayhemYDG |
For others that are interested, there is a package called redtape that extends |
So simple… :heart: |
@Raynos I just wanna say that, that is some clever piece of code there. My mind was figuratively blown when I finally understood what was going on. 👏 |
@oncletom @Raynos @sinkingshriek you can also listen to the |
Any ideas about merging this functionality? From tapes, for instance... |
Also ran into this. Thanks for the excellent idea @Raynos. I'm still not 100% satisfied with this, as it prevents accessing the Test object to set eg. tape.onFinished(), so that has to be done before you ever call beforeEach (afaik?), f.ex. I ended up re-implementing it slightly, so that the beforeEach/afterEach methods can take either a function, or an array of functions, as follows (beware, ES6): export const beforeEach = (_test, handlers) => {
if (typeof handlers === 'function') return _beforeEach(_test, handlers)
return handlers.reduce((prev, cur, curIdx, arr) => {
if (curIdx === 0) return prev
return _beforeEach(prev, cur)
}, _beforeEach(_test, handlers[0]))
}
export const afterEach = (_test, handlers) => {
if (typeof handlers === 'function') return _afterEach(_test, handlers)
return handlers.reduce((prev, cur, curIdx, arr) => {
if (curIdx === 0) return prev
return _afterEach(prev, cur)
}, _afterEach(_test, handlers[0]))
}
const _beforeEach = (_test, handler) => {
return function tapish (name, listener) {
_test(name, function (assert) {
let _end = assert.end
assert.end = function () {
assert.end = _end
listener(assert)
}
handler(assert)
})
}
}
const _afterEach = (_test, handler) => {
return function tapish (name, listener) {
_test(name, function (assert) {
let _end = assert.end
assert.end = function () {
assert.end = _end
handler(assert)
}
listener(assert)
})
}
} Use is f.ex: import tapeLib from 'tape'
import { beforeEach, someMethodA, someMethodB, someMethodC } from './helpers/hooks'
let tape = beforeEach(tapeLib, dropTables) // dropTables will run before each test.
tape = beforeEach(tape, [someMethodB, someMethodC]) // dropTables -> someMethodB -> someMethodC will run before each test This allows you to separate the functionality into smaller methods that each do a certain thing, such as resetting your DB, or whatever else you need to do before tests. |
@alathon I've since written This allows you to build a generic class for your test suite, useful if your testing an app and want to setup & teardown the app before every test. Bonus feature is that |
@substack your code is clear, but not fit with |
The above solutions looks can only be used for the whole testing. It couldn't set |
Set alias from |
Hello :-)
I'm considering using
tape
for a project requiring IE8 testing, in order to replacechai
/mocha
.Is there any way to achieve doing
before
/beforeEach
/after
/afterEach
to setup fixtures and stubs before running assertions?Thanks very much :-)
The text was updated successfully, but these errors were encountered: