Skip to content

Commit 1f2803b

Browse files
authored
feat(testrunner): removeEnvironment (#1650)
1 parent ea16e55 commit 1f2803b

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

utils/testrunner/TestRunner.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class Test {
155155
return this._hooks.filter(hook => !name || hook.name === name);
156156
}
157157

158-
environment(environment) {
158+
addEnvironment(environment) {
159159
const parents = new Set();
160160
for (let parent = environment; !(parent instanceof Suite); parent = parent.parentEnvironment())
161161
parents.add(parent);
@@ -174,6 +174,14 @@ class Test {
174174
}
175175
throw new Error(`Cannot use environment "${environment.name()}" from suite "${environment.parentSuite().fullName()}" in unrelated test "${this.fullName()}"`);
176176
}
177+
178+
removeEnvironment(environment) {
179+
const index = this._environments.indexOf(environment);
180+
if (index === -1)
181+
throw new Error(`Environment "${environment.name()}" cannot be removed because it was not added to the test "${test.fullName()}"`);
182+
this._environments.splice(index, 1);
183+
return this;
184+
}
177185
}
178186

179187
class Environment {

utils/testrunner/test/testrunner.spec.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,13 @@ module.exports.addTests = function({testRunner, expect}) {
275275
t.afterEach(() => log.push('suite:afterEach2'));
276276
t.afterAll(() => log.push('suite:afterAll'));
277277
});
278-
t.it('cuatro', () => log.push('test #4')).environment(e2);
278+
t.it('cuatro', () => log.push('test #4')).addEnvironment(e2);
279279
t.describe('no hooks suite', () => {
280280
t.describe('suite2', () => {
281281
t.beforeAll(() => log.push('suite2:beforeAll'));
282282
t.afterAll(() => log.push('suite2:afterAll'));
283283
t.describe('no hooks suite 2', () => {
284-
t.it('cinco', () => log.push('test #5')).environment(e2);
284+
t.it('cinco', () => log.push('test #5')).addEnvironment(e2);
285285
});
286286
});
287287
});
@@ -351,6 +351,31 @@ module.exports.addTests = function({testRunner, expect}) {
351351
'root:afterAll2',
352352
]);
353353
});
354+
it('should remove environment', async() => {
355+
const log = [];
356+
const t = newTestRunner();
357+
const e = t.environment('env', () => {
358+
t.beforeAll(() => log.push('env:beforeAll'));
359+
t.afterAll(() => log.push('env:afterAll'));
360+
t.beforeEach(() => log.push('env:beforeEach'));
361+
t.afterEach(() => log.push('env:afterEach'));
362+
});
363+
const e2 = t.environment('env2', () => {
364+
t.beforeAll(() => log.push('env2:beforeAll'));
365+
t.afterAll(() => log.push('env2:afterAll'));
366+
t.beforeEach(() => log.push('env2:beforeEach'));
367+
t.afterEach(() => log.push('env2:afterEach'));
368+
});
369+
t.it('uno', () => log.push('test #1')).addEnvironment(e).addEnvironment(e2).removeEnvironment(e);
370+
await t.run();
371+
expect(log).toEqual([
372+
'env2:beforeAll',
373+
'env2:beforeEach',
374+
'test #1',
375+
'env2:afterEach',
376+
'env2:afterAll',
377+
]);
378+
});
354379
it('environment restrictions', async () => {
355380
const t = newTestRunner();
356381
let env;
@@ -372,20 +397,20 @@ module.exports.addTests = function({testRunner, expect}) {
372397
}
373398
});
374399
try {
375-
t.it('test', () => {}).environment(env).environment(env2);
400+
t.it('test', () => {}).addEnvironment(env).addEnvironment(env2);
376401
expect(true).toBe(false);
377402
} catch (e) {
378403
expect(e.message).toBe('Cannot use environments "env2" and "env" that share a parent environment "suite1 env" in test "suite1 test"');
379404
}
380405
try {
381-
t.it('test', () => {}).environment(env2).environment(env);
406+
t.it('test', () => {}).addEnvironment(env2).addEnvironment(env);
382407
expect(true).toBe(false);
383408
} catch (e) {
384409
expect(e.message).toBe('Cannot use environments "env" and "env2" that share a parent environment "suite1 env" in test "suite1 test"');
385410
}
386411
});
387412
try {
388-
t.it('test', () => {}).environment(env);
413+
t.it('test', () => {}).addEnvironment(env);
389414
expect(true).toBe(false);
390415
} catch (e) {
391416
expect(e.message).toBe('Cannot use environment "env" from suite "suite1" in unrelated test "test"');

0 commit comments

Comments
 (0)