'Trying to make a function out of a test

I have some code as part of a test to call a graphql endpoint. It is using super test, so

it('should test stuff', () => done {
    const someGraphQL = `mutation {.....}`
    
    let the_id = ""

    request
    .post("/graphql") 
    .send({
        query: someGraphQL
    })
    .set("Accept", "application/json")
    .expect(200)
    .end((err, res) => {
        if (err) return done(err);
        the_id = res.body.data.id;
        done();
    })
    done()
})

and that works fine. I wanted to create a function which did the same and could be callable. So I created this in a different file

function addThing (title) {
    const someGraphQL = `mutation {.....}`
    
    let the_id = ""

    request
    .post("/graphql") 
    .send({
        query: someGraphQL
    })
    .set("Accept", "application/json")
    .expect(200)
    .end((err, res) => {
        if (err) return done(err);
        the_id = res.body.data.id;
    })
}

Now the code runs, but without a callback, it will error as taking too long. I've never tried this before, so unsure how to add a callback to a function (like this) - I did try adding => done { but got a syntax error.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source