-
Notifications
You must be signed in to change notification settings - Fork 2k
/
setup-test-framework.js
68 lines (56 loc) · 2.03 KB
/
setup-test-framework.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* External dependencies
*/
const nock = require( 'nock' );
// Disables all network requests for all tests.
nock.disableNetConnect();
beforeAll( () => {
// reactivate nock on test start
if ( ! nock.isActive() ) {
nock.activate();
}
} );
afterAll( () => {
// helps clean up nock after each test run and avoid memory leaks
nock.restore();
nock.cleanAll();
} );
// It "mocks" enzyme, so that we can delay loading of
// the utility functions until enzyme is imported in tests.
// Props to @gdborton for sharing this technique in his article:
// https://medium.com/airbnb-engineering/unlocking-test-performance-migrating-from-mocha-to-jest-2796c508ec50.
let mockEnzymeSetup = false;
jest.mock( 'enzyme', () => {
const actualEnzyme = jest.requireActual( 'enzyme' );
if ( ! mockEnzymeSetup ) {
mockEnzymeSetup = true;
// configure custom enzyme matchers for chai
const chai = jest.requireActual( 'chai' );
const chaiEnzyme = jest.requireActual( 'chai-enzyme' );
chai.use( chaiEnzyme() );
// configure custom Enzyme matchers for Jest
jest.requireActual( 'jest-enzyme' );
// configure enzyme 3 for React, from docs: http://airbnb.io/enzyme/docs/installation/index.html
const Adapter = jest.requireActual( 'enzyme-adapter-react-16' );
actualEnzyme.configure( { adapter: new Adapter() } );
// configure snapshot serializer for enzyme
const { createSerializer } = jest.requireActual( 'enzyme-to-json' );
expect.addSnapshotSerializer( createSerializer( { mode: 'deep' } ) );
}
return actualEnzyme;
} );
// It "mocks" sinon, so that we can delay loading of
// the utility functions until sinon is imported in tests.
let mockSinonSetup = false;
jest.mock( 'sinon', () => {
const actualSinon = jest.requireActual( 'sinon' );
if ( ! mockSinonSetup ) {
mockSinonSetup = true;
// configure custom sinon matchers for chai
const chai = jest.requireActual( 'chai' );
const sinonChai = jest.requireActual( 'sinon-chai' );
chai.use( sinonChai );
actualSinon.assert.expose( chai.assert, { prefix: '' } );
}
return actualSinon;
} );