$('optgroup:not([label="Knowledge Base"])')
returns a jQuery wrapper object, you need to test its length property to check whether it contains any elements
You need to test
if($('optgroup[label="Knowledge Base"]').length == 0) {
alert('test');
}