-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for post-request scripts and post-request scripts - I…
…NS-3786 (#7331) * test: add some tests for post-req script and script engine * fix: incorrect script type
- Loading branch information
Showing
4 changed files
with
205 additions
and
2 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
packages/insomnia-smoke-test/fixtures/post-request-collection.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
_type: export | ||
__export_format: 4 | ||
__export_date: 2024-02-13T07:27:17.322Z | ||
__export_source: insomnia.desktop.app:v8.6.1 | ||
resources: | ||
- _id: wrk_6b9b8455fd784462ae19cd51d7156f86 | ||
parentId: null | ||
modified: 1707808692801 | ||
created: 1707808692801 | ||
name: Post-request Scripts | ||
description: "" | ||
scope: collection | ||
_type: workspace | ||
- _id: req_244fe815da6c4342a17f0cfd98cf648c | ||
parentId: wrk_6b9b8455fd784462ae19cd51d7156f86 | ||
modified: 1707809218855 | ||
created: 1707808697304 | ||
url: http://127.0.0.1:4010/echo | ||
name: tests with expect and test | ||
description: "" | ||
method: POST | ||
postRequestScript: |- | ||
insomnia.test('happy tests', () => { | ||
insomnia.expect(200).to.eql(200); | ||
insomnia.expect('uname').to.be.a('string'); | ||
insomnia.expect('a').to.have.lengthOf(1); | ||
insomnia.expect('xxx_customer_id_yyy').to.include("customer_id"); | ||
insomnia.expect(201).to.be.oneOf([201,202]); | ||
insomnia.expect(199).to.be.below(200); | ||
// test objects | ||
insomnia.expect({a: 1, b: 2}).to.have.all.keys('a', 'b'); | ||
insomnia.expect({a: 1, b: 2}).to.have.any.keys('a', 'b'); | ||
insomnia.expect({a: 1, b: 2}).to.not.have.any.keys('c', 'd'); | ||
insomnia.expect({a: 1}).to.have.property('a'); | ||
insomnia.expect({a: 1, b: 2}).to.be.a('object') | ||
.that.has.all.keys('a', 'b'); | ||
}); | ||
insomnia.test('unhappy tests', () => { | ||
insomnia.expect(199).to.eql(200); | ||
insomnia.expect(199).to.be.oneOf([201,202]); | ||
}); | ||
body: | ||
mimeType: "application/json" | ||
text: |- | ||
{} | ||
parameters: [] | ||
headers: | ||
- name: 'Content-Type' | ||
value: 'application/json' | ||
authentication: {} | ||
metaSortKey: -1707809028499 | ||
isPrivate: false | ||
pathParameters: [] | ||
settingStoreCookies: true | ||
settingSendCookies: true | ||
settingDisableRenderRequestBody: false | ||
settingEncodeUrl: true | ||
settingRebuildPath: true | ||
settingFollowRedirects: global | ||
_type: request | ||
- _id: req_244fe815da6c4342a17f0cfd98cf6401 | ||
parentId: wrk_6b9b8455fd784462ae19cd51d7156f86 | ||
modified: 1707809218855 | ||
created: 1707808697304 | ||
url: http://127.0.0.1:4010/echo | ||
name: persist environments | ||
description: "" | ||
method: POST | ||
postRequestScript: |- | ||
insomnia.environment.set('__fromPostScript', 'environment'); | ||
insomnia.baseEnvironment.set('__fromPostScript1', 'baseEnvironment'); | ||
insomnia.collectionVariables.set('__fromPostScript2', 'collection'); | ||
body: | ||
mimeType: "application/json" | ||
text: |- | ||
{} | ||
parameters: [] | ||
headers: | ||
- name: 'Content-Type' | ||
value: 'application/json' | ||
authentication: {} | ||
metaSortKey: -1707809028499 | ||
isPrivate: false | ||
pathParameters: [] | ||
settingStoreCookies: true | ||
settingSendCookies: true | ||
settingDisableRenderRequestBody: false | ||
settingEncodeUrl: true | ||
settingRebuildPath: true | ||
settingFollowRedirects: global | ||
_type: request | ||
- _id: env_f9ef1d097c5e00986051fcb4f7a921eea1a86916 | ||
parentId: wrk_6b9b8455fd784462ae19cd51d7156f86 | ||
modified: 1707808692805 | ||
created: 1707808692805 | ||
name: Base Environment | ||
data: {} | ||
dataPropertyOrder: null | ||
color: null | ||
isPrivate: false | ||
metaSortKey: 1707808692805 | ||
_type: environment | ||
- _id: jar_f9ef1d097c5e00986051fcb4f7a921eea1a86916 | ||
parentId: wrk_6b9b8455fd784462ae19cd51d7156f86 | ||
modified: 1707808692807 | ||
created: 1707808692807 | ||
name: Default Jar | ||
cookies: [] | ||
_type: cookie_jar |
60 changes: 60 additions & 0 deletions
60
packages/insomnia-smoke-test/tests/smoke/post-request-script-features.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { expect } from '@playwright/test'; | ||
|
||
import { loadFixture } from '../../playwright/paths'; | ||
import { test } from '../../playwright/test';; | ||
|
||
test.describe('post-request features tests', async () => { | ||
test.slow(process.platform === 'darwin' || process.platform === 'win32', 'Slow app start on these platforms'); | ||
|
||
test.beforeEach(async ({ app, page }) => { | ||
const text = await loadFixture('post-request-collection.yaml'); | ||
await app.evaluate(async ({ clipboard }, text) => clipboard.writeText(text), text); | ||
|
||
await page.getByRole('button', { name: 'Create in project' }).click(); | ||
await page.getByRole('menuitemradio', { name: 'Import' }).click(); | ||
await page.locator('[data-test-id="import-from-clipboard"]').click(); | ||
await page.getByRole('button', { name: 'Scan' }).click(); | ||
await page.getByRole('dialog').getByRole('button', { name: 'Import' }).click(); | ||
|
||
await page.getByLabel('Post-request Scripts').click(); | ||
}); | ||
|
||
test('insomnia.test and insomnia.expect can work together', async ({ page }) => { | ||
const responsePane = page.getByTestId('response-pane'); | ||
|
||
await page.getByLabel('Request Collection').getByTestId('tests with expect and test').press('Enter'); | ||
|
||
// send | ||
await page.getByTestId('request-pane').getByRole('button', { name: 'Send' }).click(); | ||
|
||
// verify | ||
await page.getByRole('tab', { name: 'Timeline' }).click(); | ||
|
||
await expect(responsePane).toContainText('✓ happy tests'); | ||
await expect(responsePane).toContainText('✕ unhappy tests: AssertionError: expected 199 to deeply equal 200'); | ||
}); | ||
|
||
test('environment and baseEnvironment can be persisted', async ({ page }) => { | ||
const statusTag = page.locator('[data-testid="response-status-tag"]:visible'); | ||
await page.getByLabel('Request Collection').getByTestId('persist environments').press('Enter'); | ||
|
||
// send | ||
await page.getByTestId('request-pane').getByRole('button', { name: 'Send' }).click(); | ||
|
||
// verify response | ||
await page.waitForSelector('[data-testid="response-status-tag"]:visible'); | ||
await expect(statusTag).toContainText('200 OK'); | ||
|
||
// verify persisted environment | ||
await page.getByLabel('Manage Environments').click(); | ||
const responseBody = page.getByRole('dialog').getByTestId('CodeEditor').locator('.CodeMirror-line'); | ||
const rows = await responseBody.allInnerTexts(); | ||
const bodyJson = JSON.parse(rows.join(' ')); | ||
|
||
expect(bodyJson).toEqual({ | ||
// no environment is selected so the environment value is not persisted | ||
'__fromPostScript1': 'baseEnvironment', | ||
'__fromPostScript2': 'collection', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters