Skip to content

Commit

Permalink
createCookieStore fix
Browse files Browse the repository at this point in the history
- createCookieStore now handles empty cookie value correctly
- unit tests added for src/utils
-  build fix for except test files
  • Loading branch information
krutoo committed Nov 8, 2023
1 parent eee8a90 commit 80c6836
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
node-version: 20
registry-url: https://registry.npmjs.org/

- name: Dependencies
Expand All @@ -22,6 +22,10 @@ jobs:
run: |
npm run type-check
- name: Test
run: |
node --require ts-node/register --test src/**/*.test.ts
- name: Build
run: |
npm --no-git-tag-version version ${{ github.event.release.tag_name }}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"scripts": {
"type-check": "tsc -p . --noEmit",
"build:clean": "rm -rf dist",
"build:esm": "babel src --config-file ./babel.config.esm.js -d dist/esm --source-maps --extensions '.ts,.tsx' && echo '{ \"type\": \"module\" }' > dist/esm/package.json",
"build:cjs": "babel src --config-file ./babel.config.cjs.js -d dist/cjs --source-maps --extensions '.ts,.tsx' && echo '{ \"type\": \"commonjs\" }' > dist/cjs/package.json",
"build:esm": "babel src --config-file ./babel.config.esm.js -d dist/esm --source-maps --extensions '.ts,.tsx' --ignore='**/__test__/**/*' && echo '{ \"type\": \"module\" }' > dist/esm/package.json",
"build:cjs": "babel src --config-file ./babel.config.cjs.js -d dist/cjs --source-maps --extensions '.ts,.tsx' --ignore='**/__test__/**/*' && echo '{ \"type\": \"commonjs\" }' > dist/cjs/package.json",
"build:types": "tsc --project tsconfig.types.json",
"build": "npm run build:clean && npm run build:esm && npm run build:cjs && npm run build:types"
},
Expand Down
26 changes: 26 additions & 0 deletions src/__test__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import assert from 'node:assert';
import { describe, test } from 'node:test';
import { createCookieStore } from '../utils';

describe('createCookieStore', () => {
test('regular initial cookies should not throw exceptions', () => {
assert.equal(createCookieStore('aaa=; bbb=222').getCookies(), 'aaa=; bbb=222');
});

test('initial cookies with empty values should not throw exceptions', () => {
assert.equal(createCookieStore('aaa=; bbb=222').getCookies(), 'aaa=; bbb=222');
});

test('store should save initial cookies', () => {
const store = createCookieStore('aaa=111; bbb=222; ccc=333');
assert.equal(store.getCookies(), 'aaa=111; bbb=222; ccc=333');
});

test('store should save new cookies', () => {
const store = createCookieStore('aaa=111; bbb=222; ccc=333');
assert.equal(store.getCookies(), 'aaa=111; bbb=222; ccc=333');

store.setCookie('ddd=444');
assert.equal(store.getCookies(), 'aaa=111; bbb=222; ccc=333; ddd=444');
});
});
10 changes: 5 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export function createCookieStore(initialCookies?: string): CookieStore {
const [cookiePart] = cookie.split('; ');

if (!cookiePart) {
// @todo log instead throwing error
throw Error('Failed to parse cookie');
// @todo log?
return;
}

const [cookieName, cookieValue] = cookiePart.split('=');

if (!cookieName || !cookieValue) {
// @todo log instead throwing error
throw Error('Failed to parse cookie');
if (!cookieName || typeof cookieValue !== 'string') {
// @todo log?
return;
}

items.set(cookieName, cookieValue);
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.types.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"emitDeclarationOnly": true
},
"include": ["./src/**/*"],
"exclude": ["node_modules"]
"exclude": ["node_modules", "./src/**/__test__/*", "./src/**/*.test.ts"]
}

0 comments on commit 80c6836

Please sign in to comment.