I am trying to extend Jest to easier get API integration tests to play along between different APIs.
I have followed this tutorial and I have it working somewhat. Here's my setup:
jest.setup.api.ts
test.apiOne = async (
name: string,
fn?: jest.ProvidesCallback,
timeout?: number
) => {
console.log(`Running test "${name}" against API 1`)
pactum.request.setBaseUrl(`${process.env.API_TEST_HOST}/one`)
test(name, fn, timeout)
}
test.apiTwo = async (
name: string,
fn?: jest.ProvidesCallback,
timeout?: number
) => {
console.log(`Running test "${name}" against API 2`)
pactum.request.setBaseUrl(`${process.env.API_TEST_HOST}/two`)
test(name, fn, timeout)
}
And I use it like this:
test.apiOne("1", async () => {
console.log("1");
})
test.apiTwo("2", async () => {
console.log("2");
})
test.apiOne("3", async () => {
console.log("3");
})
test.apiOne("4", async () => {
console.log("4");
})
When I run these tests, this is the output
console.log jest.setup.api.ts:26
Running test "1" against API 1
console.log jest.setup.api.ts:38
Running test "2" against API 2
console.log jest.setup.api.ts:26
Running test "3" against API 1
console.log jest.setup.api.ts:38
Running test "4" against API 2
console.log mytest.ts:511
1
console.log jest.setup.api.ts:26
Running test "1" against API 1
console.log jest.setup.api.ts:38
Running test "2" against API 2
console.log jest.setup.api.ts:26
Running test "3" against API 1
console.log jest.setup.api.ts:38
Running test "4" against API 2
console.log mytest.ts:511
2
console.log jest.setup.api.ts:26
Running test "1" against API 1
console.log jest.setup.api.ts:38
Running test "2" against API 2
console.log jest.setup.api.ts:26
Running test "3" against API 1
console.log jest.setup.api.ts:38
Running test "4" against API 2
console.log mytest.ts:511
3
console.log jest.setup.api.ts:26
Running test "1" against API 1
console.log jest.setup.api.ts:38
Running test "2" against API 2
console.log jest.setup.api.ts:26
Running test "3" against API 1
console.log jest.setup.api.ts:38
Running test "4" against API 2
console.log mytest.ts:511
4
So for all tests it is always the conditions for the last test within the describe that gets set. So test 1 should run against API path /one but actually gets run against /two.
Is there any way to achieve what I am trying do with Jest?