feat(testrunner): removeEnvironment (#1650)

This commit is contained in:
Dmitry Gozman 2020-04-03 09:48:01 -07:00 committed by GitHub
parent ea16e55ba3
commit 1f2803bbc6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 6 deletions

View file

@ -155,7 +155,7 @@ class Test {
return this._hooks.filter(hook => !name || hook.name === name);
}
environment(environment) {
addEnvironment(environment) {
const parents = new Set();
for (let parent = environment; !(parent instanceof Suite); parent = parent.parentEnvironment())
parents.add(parent);
@ -174,6 +174,14 @@ class Test {
}
throw new Error(`Cannot use environment "${environment.name()}" from suite "${environment.parentSuite().fullName()}" in unrelated test "${this.fullName()}"`);
}
removeEnvironment(environment) {
const index = this._environments.indexOf(environment);
if (index === -1)
throw new Error(`Environment "${environment.name()}" cannot be removed because it was not added to the test "${test.fullName()}"`);
this._environments.splice(index, 1);
return this;
}
}
class Environment {

View file

@ -275,13 +275,13 @@ module.exports.addTests = function({testRunner, expect}) {
t.afterEach(() => log.push('suite:afterEach2'));
t.afterAll(() => log.push('suite:afterAll'));
});
t.it('cuatro', () => log.push('test #4')).environment(e2);
t.it('cuatro', () => log.push('test #4')).addEnvironment(e2);
t.describe('no hooks suite', () => {
t.describe('suite2', () => {
t.beforeAll(() => log.push('suite2:beforeAll'));
t.afterAll(() => log.push('suite2:afterAll'));
t.describe('no hooks suite 2', () => {
t.it('cinco', () => log.push('test #5')).environment(e2);
t.it('cinco', () => log.push('test #5')).addEnvironment(e2);
});
});
});
@ -351,6 +351,31 @@ module.exports.addTests = function({testRunner, expect}) {
'root:afterAll2',
]);
});
it('should remove environment', async() => {
const log = [];
const t = newTestRunner();
const e = t.environment('env', () => {
t.beforeAll(() => log.push('env:beforeAll'));
t.afterAll(() => log.push('env:afterAll'));
t.beforeEach(() => log.push('env:beforeEach'));
t.afterEach(() => log.push('env:afterEach'));
});
const e2 = t.environment('env2', () => {
t.beforeAll(() => log.push('env2:beforeAll'));
t.afterAll(() => log.push('env2:afterAll'));
t.beforeEach(() => log.push('env2:beforeEach'));
t.afterEach(() => log.push('env2:afterEach'));
});
t.it('uno', () => log.push('test #1')).addEnvironment(e).addEnvironment(e2).removeEnvironment(e);
await t.run();
expect(log).toEqual([
'env2:beforeAll',
'env2:beforeEach',
'test #1',
'env2:afterEach',
'env2:afterAll',
]);
});
it('environment restrictions', async () => {
const t = newTestRunner();
let env;
@ -372,20 +397,20 @@ module.exports.addTests = function({testRunner, expect}) {
}
});
try {
t.it('test', () => {}).environment(env).environment(env2);
t.it('test', () => {}).addEnvironment(env).addEnvironment(env2);
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Cannot use environments "env2" and "env" that share a parent environment "suite1 env" in test "suite1 test"');
}
try {
t.it('test', () => {}).environment(env2).environment(env);
t.it('test', () => {}).addEnvironment(env2).addEnvironment(env);
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Cannot use environments "env" and "env2" that share a parent environment "suite1 env" in test "suite1 test"');
}
});
try {
t.it('test', () => {}).environment(env);
t.it('test', () => {}).addEnvironment(env);
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Cannot use environment "env" from suite "suite1" in unrelated test "test"');