diff --git a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
index 44304499d..db5419a17 100644
--- a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
+++ b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
@@ -3153,6 +3153,15 @@ describeWithDOM('mount', () => {
expect(wrapper.at(1).key()).to.equal('bar');
expect(wrapper.at(2).key()).to.equal('');
});
+
+ it('should return null when no key is specified', () => {
+ const wrapper = mount((
+
+ )).find('li');
+ expect(wrapper.key()).to.equal(null);
+ });
});
describe('.matchesElement(node)', () => {
diff --git a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
index 7d1a6508e..22e14d897 100644
--- a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
+++ b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
@@ -4014,6 +4014,15 @@ describe('shallow', () => {
expect(wrapper.at(1).key()).to.equal('bar');
expect(wrapper.at(2).key()).to.equal('');
});
+
+ it('should return null when no key is specified', () => {
+ const wrapper = shallow((
+
+ )).find('li');
+ expect(wrapper.key()).to.equal(null);
+ });
});
describe('.matchesElement(node)', () => {
diff --git a/packages/enzyme/src/ReactWrapper.js b/packages/enzyme/src/ReactWrapper.js
index aac1e5701..d14a3fc0a 100644
--- a/packages/enzyme/src/ReactWrapper.js
+++ b/packages/enzyme/src/ReactWrapper.js
@@ -703,7 +703,7 @@ class ReactWrapper {
* @returns {String}
*/
key() {
- return this.single('key', n => n.key);
+ return this.single('key', n => (n.key === undefined ? null : n.key));
}
/**
diff --git a/packages/enzyme/src/ShallowWrapper.js b/packages/enzyme/src/ShallowWrapper.js
index 0d905a26f..2144bc630 100644
--- a/packages/enzyme/src/ShallowWrapper.js
+++ b/packages/enzyme/src/ShallowWrapper.js
@@ -845,7 +845,7 @@ class ShallowWrapper {
* @returns {String}
*/
key() {
- return this.single('key', n => n.key);
+ return this.single('key', n => (n.key === undefined ? null : n.key));
}
/**