ec.cookie-mock

Mock implementation for document.cookie.

Usage no npm install needed!

<script type="module">
  import ecCookieMock from 'https://cdn.skypack.dev/ec.cookie-mock';
</script>

README

ec.cookie-mock

Mock implementation for document.cookie. By entrecode.

npm version Build Status

You can use this for testing cookie handling in your project. Simple add it like shown in the usage example.

Installation

npm i --save cookie-mock

Usage

document = new CookieMock();

const now = new Date();
const future = new Date(now.now() + 60000) // in 60 seconds
const past = new Date(now.now() - 60000) // before 60 seconds

// add a new cookie
document.cookie = `cookie=hazelnut; expires=${future.toUTCString()}; path=/mouth; secure`;
console.log(document.cookie); // cookie=hazelnut; expires=Fri, 26 Jan 2017 12:01:00 GMT; path=/mouth; secure

// You can update the cookie
document.cookie = `cookie=triplechoc; expires=${future.toUTCString()}; path=/mouth; HttpOnly`;
console.log(document.cookie); // cookie=triplechoc; expires=Fri, 26 Jan 2017 12:01:00 GMT; path=/mouth; HttpOnly

// Delete the cookie by setting expires to be in the past
document.cookie = `cookie=triplechoc; expires=${past.toUTCString()}; path=/mouth; HttpOnly`;
console.log(document.cookie); // empty