I have some test cases that can be shared between test suites
Lets say suite x and suite y share the same set of test cases (it functions).
i have made a separate .js file that has the shared code which looks something like this.
module.exports = function(a,b){
//...
test cases..
//....
}
I am trying to use this module in both x and y
this is how x looks
var common = require('./module');
describe("description", module(a,b);
can this be done? is there any other way?
The common js in my code looks like
module.exports = function(a,b) {
beforeAll(function(){
//some code
}
afterAll(function(){
//some code
}
It(‘ads’, function(){
code
}
it(‘ads’, function(){
code
}
it(‘ads’, function(){
code
}
}
i want to use this as the function argument for the describe function with passable parameters in two other suites.
Suite1
var common = ('./common');
describe('this is a test case', common(a,b);
is this possible?
If you have your common.js file similar to...
module.exports = function(a,b){
//...
test cases..
//....
}
And your test.js file:
var common = require('./common'); // <-- note the change
describe("description", common); // <-- you were calling module*
This is assuming your common.js exported function is a properly formatted describe function.
You could also export individual test cases like (other.js)...
module.exports = {
testOne: function(something) { return false; },
testTwo: function(whatever) { return true; }
}
And your test...
var other = require('./other');
describe("description", function() {
it('should pass', function() {
expect(other.testOne()).toEqual(false);
});
});
As far as I know, you can't directly run an "it" from another file. However you can run functions, and a function can do everything an "it" can do. For example:
Helper.js (This is your function file)
export class helper{
static itFunctionOne(){
//Test code goes here
}
static itFuncitonTwo(){
//Test code goes here
}
}
And then in your tests
Test1:
const helper = require('relative/path/to/file/helper.js');
describe('Doing test 1 stuff',function(){
it('Should run test',function(){
helper.itFunctionOne();
helper.itFunctionTwo();
}
}
Test2:
const helper = require('relative/path/to/file/helper.js');
describe('Doing test 2 stuff',function(){
it('Should run test',function(){
helper.itFunctionOne();
helper.itFunctionTwo();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.