Skip to content

Commit 84eb324

Browse files
Do not throw errors if the methods are called in node
Be advised that the actual behavior of the methods in this case is undefined Closes js-cookiegh-165. Closes js-cookiegh-164.
1 parent 3c85aee commit 84eb324

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/js.cookie.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
function init (converter) {
3535
function api (key, value, attributes) {
3636
var result;
37+
if (typeof document === 'undefined') {
38+
return;
39+
}
3740

3841
// Write
3942

test/node.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,25 @@ exports.node = {
55
var Cookies = require('../src/js.cookie');
66
test.ok(!!Cookies.get, 'should load the Cookies API');
77
test.done();
8+
},
9+
should_not_throw_error_for_set_call_in_node: function (test) {
10+
test.expect(0);
11+
var Cookies = require('../src/js.cookie');
12+
Cookies.set('anything');
13+
Cookies.set('anything', { path: '' });
14+
test.done();
15+
},
16+
should_not_throw_error_for_get_call_in_node: function (test) {
17+
test.expect(0);
18+
var Cookies = require('../src/js.cookie');
19+
Cookies.get('anything');
20+
test.done();
21+
},
22+
should_not_throw_error_for_remove_call_in_node: function (test) {
23+
test.expect(0);
24+
var Cookies = require('../src/js.cookie');
25+
Cookies.remove('anything');
26+
Cookies.remove('anything', { path: '' });
27+
test.done();
828
}
929
};

0 commit comments

Comments
 (0)