* Returns a wrapped version of Mocha's describe(), it() and only() methods * that also sets up the provided fixtures and returns the corresponding * environment objects of each fixture to the test method. * @param {function(!Object):!Array<?Fixture>} factory
(factory)
| 295 | * @param {function(!Object):!Array<?Fixture>} factory |
| 296 | */ |
| 297 | function describeEnv(factory) { |
| 298 | /** |
| 299 | * @param {string} name |
| 300 | * @param {!Object} spec |
| 301 | * @param {function(!Object)} fn |
| 302 | * @param {function(string, function())} describeFunc |
| 303 | * @param {?boolean} configured |
| 304 | */ |
| 305 | const templateFunc = function (name, spec, fn, describeFunc, configured) { |
| 306 | const fixtures = [new SandboxFixture(spec)].concat( |
| 307 | factory(spec).filter((fixture) => fixture && fixture.isOn()) |
| 308 | ); |
| 309 | |
| 310 | return describeFunc(name, () => { |
| 311 | const env = Object.create(null); |
| 312 | |
| 313 | // Note: If this `beforeEach` function is made async/always returns a |
| 314 | // Promise, even if it resolves immediately, tests start failing. It's |
| 315 | // not entirely clear why. Don't refactor this to be an async for-loop |
| 316 | // like the `afterEach` below. |
| 317 | beforeEach(() => { |
| 318 | let totalPromise = undefined; |
| 319 | // Set up all fixtures. |
| 320 | fixtures.forEach((fixture) => { |
| 321 | if (totalPromise) { |
| 322 | totalPromise = totalPromise.then(() => fixture.setup(env)); |
| 323 | } else { |
| 324 | const res = fixture.setup(env); |
| 325 | if (res && typeof res.then == 'function') { |
| 326 | totalPromise = res; |
| 327 | } |
| 328 | } |
| 329 | }); |
| 330 | return totalPromise; |
| 331 | }); |
| 332 | |
| 333 | afterEach(async () => { |
| 334 | // Tear down all fixtures in reverse order. |
| 335 | for (let i = fixtures.length - 1; i >= 0; --i) { |
| 336 | await fixtures[i].teardown(env); |
| 337 | } |
| 338 | |
| 339 | // Delete all other keys. |
| 340 | for (const key in env) { |
| 341 | delete env[key]; |
| 342 | } |
| 343 | }); |
| 344 | |
| 345 | function execute() { |
| 346 | if (spec.timeout) { |
| 347 | this.timeout(spec.timeout); |
| 348 | } |
| 349 | fn.call(this, env); |
| 350 | } |
| 351 | |
| 352 | // Don't re-configure the inner describe() if the outer describes() was |
| 353 | // already configured. |
| 354 | if (configured) { |
no test coverage detected