Skip to content

Commit

Permalink
fix: support await tests and add auto-complete .test and .expect
Browse files Browse the repository at this point in the history
  • Loading branch information
ihexxa committed Feb 25, 2025
1 parent 28816ed commit 69fa119
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 13 deletions.
33 changes: 21 additions & 12 deletions packages/insomnia-sdk/src/objects/insomnia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ export class InsomniaObject {

this.requestTestResults = new Array<RequestTestResult>();
this.parentFolders = rawObj.parentFolders;

return new Proxy(this, {
get: (target, prop, receiver) => {
if (prop === 'test') {
const testHandler: TestHandler = async (msg: string, fn: () => Promise<void>) => {
await this._test(msg, fn, this.pushRequestTestResult);
};
testHandler.skip = async (msg: string, fn: () => Promise<void>) => {
await this._skip(msg, fn, this.pushRequestTestResult);
};

return testHandler;
}
return Reflect.get(target, prop, receiver);
},
});
}

sendRequest(
Expand All @@ -91,24 +107,17 @@ export class InsomniaObject {
return sendRequest(request, cb, this._settings);
}

get test() {
const testHandler: TestHandler = async (msg: string, fn: () => Promise<void>) => {
await this._test(msg, fn, this.pushRequestTestResult);
};
testHandler.skip = async (msg: string, fn: () => Promise<void>) => {
await this._skip(msg, fn, this.pushRequestTestResult);
};

return testHandler;
}
test = () => {
// this method is intercepted by the proxy above
};

private pushRequestTestResult = (testResult: RequestTestResult) => {
this.requestTestResults = [...this.requestTestResults, testResult];
};

expect(exp: boolean | number | string | object) {
expect = (exp: boolean | number | string | object) => {
return this._expect(exp);
}
};

get settings() {
return undefined;
Expand Down
4 changes: 3 additions & 1 deletion packages/insomnia-sdk/src/objects/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export async function test(
}
};

startTestObserver(wrapFn());
const testPromise = wrapFn();
startTestObserver(testPromise);
return testPromise;
}

let testPromises = new Array<Promise<void>>();
Expand Down
34 changes: 34 additions & 0 deletions packages/insomnia-smoke-test/fixtures/runner-collection.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,40 @@ collection:
send: true
store: true
rebuildPath: true
- url: http://127.0.0.1:4010/echo
name: await-test
meta:
id: req_823443a369a74ac48cb9baa27ed8245c
created: 1636141014550
modified: 1636707449230
isPrivate: false
sortKey: -1636141014552
method: POST
scripts:
afterResponse: |-
await insomnia.test('t1', async () => {
await new Promise((resolve) => setTimeout(resolve, 50));
insomnia.expect(201).to.eql(200);
});
async function myTest() {
await insomnia.test('t2', () => {
insomnia.expect(201).to.eql(200);
});
}
await myTest();
insomnia.test('t3', () => {
insomnia.expect(201).to.eql(200);
});
settings:
renderRequestBody: true
encodeUrl: true
followRedirects: global
cookies:
send: true
store: true
rebuildPath: true
- url: http://127.0.0.1:4010/echo
name: req01
meta:
Expand Down
20 changes: 20 additions & 0 deletions packages/insomnia-smoke-test/tests/smoke/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,26 @@ test.describe('runner features tests', async () => {
await verifyResultRows(page, 3, 0, 3, expectedTestOrder, 1);
});

test('await test works', async ({ page }) => {
await page.getByTestId('run-collection-btn-quick').click();

await page.locator('.runner-request-list-await-test').click();

// send
await page.getByRole('button', { name: 'Run', exact: true }).click();

// check result
await page.getByText('0 / 3').first().click();

const expectedTestOrder = [
't1',
't2',
't3',
];

await verifyResultRows(page, 0, 0, 3, expectedTestOrder, 1);
});

test('run req5 3 times with setNextRequest in the after-response script', async ({ page }) => {
await page.getByTestId('run-collection-btn-quick').click();

Expand Down

0 comments on commit 69fa119

Please sign in to comment.