Skip to content
On this page


Craft-pest simulates a browser's cookie storage throughout a single test. That means that cookies are retained through multiple requests in a single test.

For example, the following may set a logged in cookie on the first request. That cookie will then be retained through the subsequent requests.

it ('logs a user in and navigates their dashboard', function () {
    ->fill('username', 'michael')
    ->fill('password', '***')


Note This is a verbose and slow way to manage login. It's better to use the ->actingAs() method on a test to log a user in.


If you need to clear the stored cookies mid-test you can call test()->clearCookieCollection().

storeCookieCollection(?yii\web\CookieCollection $cookies)

Stores cookies from the passed cookie collection in the test state so they can be re-sent to subsequent requests in the same test.

Warning This is automatically called after every response so that cookies may be retained through a test. Expired cookies that come back from a test are automatically pruned, mimicing the functionality of a browser.


Get the stored cookie collection