fleet/frontend/test/envSetup.js
Zach Wasserman 0670db66c4
Migrate JS tests to Jest and update libraries (#74)
- Move from Mocha to Jest for JS testing (Jest seems to have better support for
 'watching' tests and a more active community these days).
- Codemod existing tests to Jest syntax (using https://github.com/skovhus/jest-codemods)
- Fix some errors in tests that were previously hidden.
- Update Babel.
2020-12-01 10:15:12 -08:00

76 lines
1.7 KiB
JavaScript

import 'regenerator-runtime/runtime';
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import nock from 'nock';
// Uncomment for verbose unhandled promise rejection warnings
// process.on('unhandledRejection', (reason) => {
// console.error('REJECTION', reason);
// });
nock.disableNetConnect();
// Many tests will output unhandled promise rejection warnings if this is not
// included to mock the common HTTP request.
beforeEach(() => {
nock('http://localhost:8080')
.post('/api/v1/kolide/targets', () => true)
.reply(200, {
targets_count: 1234,
targets: [
{
id: 3,
label: 'OS X El Capitan 10.11',
name: 'osx-10.11',
platform: 'darwin',
target_type: 'hosts',
},
],
});
});
afterEach(nock.cleanAll);
configure({ adapter: new Adapter() });
global.document.queryCommandEnabled = () => { return true; };
global.document.execCommand = () => { return true; };
global.window.getSelection = () => {
return {
removeAllRanges: () => { return true; },
};
};
global.window.URL = new URL('http://localhost:8080');
global.navigator = global.window.navigator;
window.URL.createObjectURL = () => undefined;
function mockStorage() {
let storage = {};
return {
setItem(key, value = '') {
storage[key] = value;
},
getItem(key) {
return storage[key];
},
removeItem(key) {
delete storage[key];
},
get length() {
return Object.keys(storage).length;
},
key(i) {
return Object.keys(storage)[i] || null;
},
clear () {
storage = {};
},
};
}
global.localStorage = mockStorage();
window.localStorage = global.localStorage;
afterEach(nock.cleanAll);