最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamically generate tests in Jest - Stack Overflow

programmeradmin7浏览0评论

I am looking for a way to generate tests in Jest and run them. I am testing an API, and the test cases are imported from a CSV file, from which the tests are generated.

Currently what I've got is an array of parameters, from which I'd like to generate the tests and run them. This is how I see it working:

let testFn =  (testName, request, expected) => it(test.testName, (request, expected) => {
    return new Promise((resolve, reject) => {
        http.post(request, (err, res) => {
            if (err) {
                reject(err);
            } else {
                resolve(res);
            }
        })
    }).then((res) => {
        expect(res.result).toBe(expected['result']);
        return response;
    })
});

The code above is my test funtion. As I see it, this should be called to execute a test. It generates a test based on the 3 parameters:

  • testName is he name of the test, to show in the console
  • request is the request to send to the API
  • expected is the expected result.

The structure is always the same: res.result should be whatever is in the parameter expect.

An example of an element from the array with tests that I get from the CSV would be:

const oneTest = {
    testName: 'the request should return 10',
    request: {
        input: 10
    },
    expected: {
        result: 10
    }
}

Here we go through the tests array and run all the tests. This is the part I can't make work.

for (let test in tests) {
    testFn.apply(this, [test.testName, test.request, test.expected])
}

The expected result from this is that I'd have all tests generated from the array, run and results shown in the console.

I hope I explained what my problem is clear enough, but if that's not the case, please don't hesitate to ask (you're helping me after all!)

PS: This is my first time using Jest, so I might be assuming many things in a wrong way. If you see some plete stupidity, please note it, I'm happy to learn! :)

I am looking for a way to generate tests in Jest and run them. I am testing an API, and the test cases are imported from a CSV file, from which the tests are generated.

Currently what I've got is an array of parameters, from which I'd like to generate the tests and run them. This is how I see it working:

let testFn =  (testName, request, expected) => it(test.testName, (request, expected) => {
    return new Promise((resolve, reject) => {
        http.post(request, (err, res) => {
            if (err) {
                reject(err);
            } else {
                resolve(res);
            }
        })
    }).then((res) => {
        expect(res.result).toBe(expected['result']);
        return response;
    })
});

The code above is my test funtion. As I see it, this should be called to execute a test. It generates a test based on the 3 parameters:

  • testName is he name of the test, to show in the console
  • request is the request to send to the API
  • expected is the expected result.

The structure is always the same: res.result should be whatever is in the parameter expect.

An example of an element from the array with tests that I get from the CSV would be:

const oneTest = {
    testName: 'the request should return 10',
    request: {
        input: 10
    },
    expected: {
        result: 10
    }
}

Here we go through the tests array and run all the tests. This is the part I can't make work.

for (let test in tests) {
    testFn.apply(this, [test.testName, test.request, test.expected])
}

The expected result from this is that I'd have all tests generated from the array, run and results shown in the console.

I hope I explained what my problem is clear enough, but if that's not the case, please don't hesitate to ask (you're helping me after all!)

PS: This is my first time using Jest, so I might be assuming many things in a wrong way. If you see some plete stupidity, please note it, I'm happy to learn! :)

Share Improve this question edited Jan 11, 2018 at 17:18 Mike Strobel 25.6k60 silver badges70 bronze badges asked Jan 10, 2018 at 16:21 bitstreambitstream 1,1162 gold badges19 silver badges30 bronze badges 1
  • Thanks for the edit, Mike! – bitstream Commented Jan 13, 2018 at 11:26
Add a ment  | 

2 Answers 2

Reset to default 4

I don't think this requires Mocha. Perhaps the trick is that you don't have to nest the test generating function within a test, you just run it at the top level:

Here's a plete file with two failing tests and a passing one.

// demo.test.js
const tests = [1, 2, 3];

tests.forEach(value =>
  it(`Expects ${value} to equal 2`, () => expect(value).toEqual(2))
);

And its output:

Test Suites: 1 failed, 1 total
Tests:       2 failed, 1 passed, 3 total
Snapshots:   0 total
Time:        0.712s
Ran all test suites related to changed files.

Turns out the default test runner in Jest is Jasmine 2, which does not support nested it() statements. The solution for me was to switch to Mocha, which does.

发布评论

评论列表(0)

  1. 暂无评论