Skip to content

Commit

Permalink
feat: migrate cypress requests (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
seren5240 authored Apr 11, 2024
1 parent 427d0c7 commit bb6f67d
Showing 1 changed file with 54 additions and 8 deletions.
62 changes: 54 additions & 8 deletions .grit/patterns/js/cypress_to_playwright.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,52 @@ pattern convert_cypress_assertions() {
or {
`expect($arg).to.not.be.null` => `expect($arg).not.toBeNull()`,
`expect($arg).to.not.be.undefined` => `expect($arg).not.toBeUndefined()`,
`$locator.should($cond1, $cond2)` as $should where {
$pw_cond = "",
$cond1 <: `'contain'` where {
$pw_cond += `toContainText($cond2)`,
},
$should => `await expect($locator).$pw_cond`,
},
`$locator.should($condition)` as $should where {
$condition <: bubble or {
`'exist'` => `toBeAttached()`,
`'not.exist'` => `not.toBeAttached()`,
},
$should => `await expect($locator).$condition`,
},
`$locator.should($cond1, $cond2)` as $should where {
$pw_cond = "",
$cond1 <: `'contain'` where {
$pw_cond += `toContainText($cond2)`,
},
$should => `await expect($locator).$pw_cond`,
}
}
}
pattern convert_cypress_queries() {
or {
`cy.visit($loc)` => `await page.goto($loc)`,
`cy.get($locator)` => `page.locator($locator)`,
`cy.contains($text, $options)` => `await expect(page.getByText($text)).toBeVisible($options)`,
`cy.contains($text)` => `await expect(page.getByText($text)).toBeVisible()`,
`cy.log($log)` => `console.log($log)`,
`Cypress.env('$var')` => `process.env.$var`,
`cy.onlyOn($var === $cond)` => `if ($var !== $cond) {
test.skip();
}`,
`cy.request({ $opts })` as $req where {
or {
$opts <: contains pair(key=`method`, value=`"$method"`),
$method = `get`,
},
$opts <: contains pair(key=`url`, value=$url),
$method = lowercase($method),
$other_opts = [],
$opts <: some bubble($other_opts) $opt where {
$opt <: not contains or {
`method`,
`url`,
},
$other_opts += $opt,
},
$other_opts = join($other_opts, `,`),
$req => `await request.$method($url, { $other_opts })`
}
}
}
Expand All @@ -58,7 +78,7 @@ contains bubble or {
convert_cypress_assertions(),
convert_cypress_queries(),
} where {
$program <: contains bubble convert_cypress_test(),
$program <: maybe contains bubble convert_cypress_test(),
$expect = `expect`,
$expect <: ensure_import_from(source=`"@playwright/test"`),
$test = `test`,
Expand Down Expand Up @@ -93,3 +113,29 @@ test.describe('A mock test', () => {
});
});
```

## Converts requests

```js
cy.request({
method: 'POST',
url: '/submit',
body: JSON.stringify({
content: 'Hello world',
}),
failOnStatusCode: false,
});
cy.contains('Submitted', { timeout: 10000 });
```

```ts
import { expect, test } from '@playwright/test';

await request.post('/submit', {
body: JSON.stringify({
content: 'Hello world',
}),
failOnStatusCode: false,
});
await expect(page.getByText('Submitted')).toBeVisible({ timeout: 10000 });
```

0 comments on commit bb6f67d

Please sign in to comment.