Since I need to have different beforeAll() and beforeEach() implementations for different tests within the same spec file, I have decided to have two describe() within the spec file. However, there are some methods originally implemented within the first describe(), which they will also be used in the second describe(). I would like to know if it is possible to put those methods out of the describe() scope and place them in the outermost scope of the spec file. I have tested this approach and it is working fine without any errors. However, I would like to know if this approach follows code style standard?
Here is an example. Supposedly I have a spec file example.spec.ts
describe("Example", function() {
// Declare variables
beforeAll(function() {
// Implementation of beforeAll()
// ...
// ...
});
beforeEach(function() {
// Implementation of beforeEach()
// ...
// ...
});
it("test one", function() {
// Implementation of test one
// ...
helperMethod()
// ...
});
helperMethod() {
// Implementation of helperMethod()
// ...
// ...
});
});
Now that I have another describe() inside the spec file and the helper method is shared between the two describe():
describe("Example1", function() {
// Declare variables
beforeAll(function() {
// Implementation of beforeAll()
// ...
// ...
});
beforeEach(function() {
// Implementation of beforeEach()
// ...
// ...
});
it("test one", function() {
// Implementation of test one
// ...
helperMethod()
// ...
});
});
describe("Example2", function() {
// Declare variables
beforeAll(function() {
// Implementation of beforeAll()
// ...
// ...
});
beforeEach(function() {
// Implementation of beforeEach()
// ...
// ...
});
it("test two", function() {
// Implementation of test one
// ...
helperMethod()
// ...
});
});
helperMethod() {
// Implementation of helperMethod()
// ...
// ...
});
I have tested this approach and no errors are returned. The tests are performed successfully. However, I am not sure if this follows the code style standard. Is this a good approach to resolve the problem? Or do I have to instead place the helperMethod() into a page object file?