1 | let cartesianProduct = require('cartesian-product')
|
2 |
|
3 | class TestCombo {
|
4 |
|
5 | filter(combination) {
|
6 | return true
|
7 | }
|
8 |
|
9 | getCombinations() {
|
10 | const extractedTypeArrays = this.args.map(arg => this.argTypes[arg])
|
11 |
|
12 | const combinations = [
|
13 | ...cartesianProduct(extractedTypeArrays).filter(this.filter),
|
14 | ...this.extraCombinations()
|
15 | ]
|
16 |
|
17 | return combinations.length ? combinations : [[]]
|
18 | }
|
19 |
|
20 | async runTest(test, combination) {
|
21 | const argValues = this.args.map((arg, idx) => {
|
22 | return this.getArgValues(test, combination, arg, combination[idx])
|
23 | })
|
24 |
|
25 | test.args = argValues
|
26 |
|
27 | let res
|
28 | try {
|
29 | res = await this.testMethod(test, combination, argValues)
|
30 | res = res || res === false ? res : null
|
31 | }catch(e) {
|
32 | res = e
|
33 | }
|
34 |
|
35 | test.res = res
|
36 |
|
37 | return
|
38 | }
|
39 |
|
40 | run() {
|
41 | const combinations = this.getCombinations()
|
42 |
|
43 | describe(this.title, () => {
|
44 | const context = {}
|
45 |
|
46 | combinations.forEach( (combination) => {
|
47 | const shouldSuccess = this.shouldSuccess(combination)
|
48 | const testTitle = this.testTitle(shouldSuccess,combination)
|
49 |
|
50 | describe(testTitle, () => {
|
51 | const phaseHandler = key => () => this[key](context, combination)
|
52 |
|
53 | const phases = [
|
54 | 'beforeAll',
|
55 | 'beforeEach',
|
56 | 'afterAll',
|
57 | 'afterEach',
|
58 | ]
|
59 |
|
60 | phases.forEach( (phase) => {
|
61 | global[phase](phaseHandler(phase))
|
62 | })
|
63 |
|
64 | if (shouldSuccess) {
|
65 | this.successAssert(context, combination)
|
66 | }else {
|
67 | this.failureAssert(context, combination)
|
68 | }
|
69 | })
|
70 | })
|
71 | })
|
72 | }
|
73 |
|
74 | testTitle(shouldSuccess, combination) {
|
75 | const state = shouldSuccess ? 'success' : 'failure';
|
76 | const tmp = combination.map(str => `'${str}'`);
|
77 | const combinationStr = combination.join(', ');
|
78 |
|
79 | return `${state} - [${combinationStr}]`;
|
80 | }
|
81 | }
|
82 |
|
83 | module.exports = TestCombo
|